Skip to content

Commit

Permalink
Merge pull request #13 from fusioned/fix-validator-step-line-counting
Browse files Browse the repository at this point in the history
Fix the line counter tracking when validation exception are thrown
  • Loading branch information
fusioned authored and Baachi committed May 11, 2018
1 parent 0dcc6d5 commit a8e4c54
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Write your code and add tests. Then run the tests:

```bash
$ vendor/bin/phpunit
$ vendor/bin/phpspec run
```

Commit your changes and push them to GitHub:
Expand Down
48 changes: 48 additions & 0 deletions spec/Step/ValidatorStepSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,52 @@ function it_validates_an_item_from_metadata(

$this->getViolations()->shouldReturn([1 => $list]);
}

function it_validates_multiple_items_from_metadata(
ValidatorInterface $validator,
ConstraintViolationListInterface $list
)
{
$numberOfCalls = 3;
$next = function() {};
$list->count()->willReturn(1);
$item = new \stdClass();
$validator->validate($item)->willReturn($list);

for ($i = 0; $i < $numberOfCalls; $i++) {
$this->process($item, $next);
}

$this->getViolations()->shouldReturn(array_fill(1, $numberOfCalls, $list));
}

function it_tracks_lines_when_exceptions_are_thrown_during_process(
Step $step,
ValidatorInterface $validator,
Constraint $constraint,
ConstraintViolation $violation
)
{
$numberOfCalls = 3;
$next = function() {};
$errorList = new ConstraintViolationList([$violation->getWrappedObject()]);
$stepFunc = function($item) use ($step, $next) {
return $step->process($item, $next);
};
$item = ['foo' => 10];

$validator->validate($item, Argument::type('Symfony\Component\Validator\Constraints\Collection'))
->willReturn($errorList);

$step->process($item, $next)->shouldNotBeCalled();

$this->throwExceptions();
$this->add('foo', $constraint)->shouldReturn($this);

for ($i = 0; $i < $numberOfCalls; $i++) {
$this->shouldThrow('Port\Exception\ValidationException')->duringProcess($item, $stepFunc);
}

$this->getViolations()->shouldReturn(array_fill(1, $numberOfCalls, $errorList));
}
}
6 changes: 3 additions & 3 deletions src/Step/ValidatorStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ValidatorStep implements PriorityStep
/**
* @var integer
*/
private $line = 1;
private $line = 0;

/**
* @var ValidatorInterface
Expand Down Expand Up @@ -98,6 +98,8 @@ public function addOption($option, $optionValue)
*/
public function process($item, callable $next)
{
$this->line++;

if (count($this->constraints) > 0) {
$constraints = new Constraints\Collection($this->constraints);
$list = $this->validator->validate($item, $constraints);
Expand All @@ -113,8 +115,6 @@ public function process($item, callable $next)
}
}

$this->line++;

if (0 === count($list)) {
return $next($item);
}
Expand Down

0 comments on commit a8e4c54

Please sign in to comment.