|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraints\NotEqualTo; |
| 15 | +use Symfony\Component\Validator\Constraints\Range; |
| 16 | +use Symfony\Component\Validator\Constraints\Regex; |
| 17 | +use Symfony\Component\Validator\Constraints\Sequentially; |
| 18 | +use Symfony\Component\Validator\Constraints\SequentiallyValidator; |
| 19 | +use Symfony\Component\Validator\Constraints\Type; |
| 20 | +use Symfony\Component\Validator\ConstraintViolation; |
| 21 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 22 | + |
| 23 | +class SequentiallyValidatorTest extends ConstraintValidatorTestCase |
| 24 | +{ |
| 25 | + protected function createValidator() |
| 26 | + { |
| 27 | + return new SequentiallyValidator(); |
| 28 | + } |
| 29 | + |
| 30 | + public function testWalkThroughConstraints() |
| 31 | + { |
| 32 | + $constraints = [ |
| 33 | + new Type('number'), |
| 34 | + new Range(['min' => 4]), |
| 35 | + ]; |
| 36 | + |
| 37 | + $value = 6; |
| 38 | + |
| 39 | + $contextualValidator = $this->context->getValidator()->inContext($this->context); |
| 40 | + $contextualValidator->expects($this->any())->method('getViolations')->willReturn($this->context->getViolations()); |
| 41 | + $contextualValidator->expects($this->exactly(2)) |
| 42 | + ->method('validate') |
| 43 | + ->withConsecutive( |
| 44 | + [$value, $constraints[0]], |
| 45 | + [$value, $constraints[1]] |
| 46 | + ) |
| 47 | + ->willReturn($contextualValidator); |
| 48 | + |
| 49 | + $this->validator->validate($value, new Sequentially($constraints)); |
| 50 | + |
| 51 | + $this->assertNoViolation(); |
| 52 | + } |
| 53 | + |
| 54 | + public function testStopsAtFirstConstraintWithViolations() |
| 55 | + { |
| 56 | + $constraints = [ |
| 57 | + new Type('string'), |
| 58 | + new Regex(['pattern' => '[a-z]']), |
| 59 | + new NotEqualTo('Foo'), |
| 60 | + ]; |
| 61 | + |
| 62 | + $value = 'Foo'; |
| 63 | + |
| 64 | + $contextualValidator = $this->context->getValidator()->inContext($this->context); |
| 65 | + $contextualValidator->expects($this->any())->method('getViolations')->willReturn($this->context->getViolations()); |
| 66 | + $contextualValidator->expects($this->exactly(2)) |
| 67 | + ->method('validate') |
| 68 | + ->withConsecutive( |
| 69 | + [$value, $constraints[0]], |
| 70 | + [$value, $constraints[1]] |
| 71 | + ) |
| 72 | + ->will($this->onConsecutiveCalls( |
| 73 | + // Noop, just return the validator: |
| 74 | + $this->returnValue($contextualValidator), |
| 75 | + // Add violation on second call: |
| 76 | + $this->returnCallback(function () use ($contextualValidator) { |
| 77 | + $this->context->getViolations()->add($violation = new ConstraintViolation('regex error', null, [], null, '', null, null, 'regex')); |
| 78 | + |
| 79 | + return $contextualValidator; |
| 80 | + } |
| 81 | + ))); |
| 82 | + |
| 83 | + $this->validator->validate($value, new Sequentially($constraints)); |
| 84 | + |
| 85 | + $this->assertCount(1, $this->context->getViolations()); |
| 86 | + } |
| 87 | +} |
0 commit comments