|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Test; |
| 13 | + |
| 14 | +use PHPUnit\Framework\ExpectationFailedException; |
| 15 | +use Symfony\Component\Validator\Constraint; |
| 16 | +use Symfony\Component\Validator\Constraints\DateTime; |
| 17 | +use Symfony\Component\Validator\Constraints\NotNull; |
| 18 | +use Symfony\Component\Validator\ConstraintValidator; |
| 19 | +use Symfony\Component\Validator\ConstraintValidatorInterface; |
| 20 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 21 | + |
| 22 | +class ConstraintValidatorTestCaseTest extends ConstraintValidatorTestCase |
| 23 | +{ |
| 24 | + protected function createValidator(): ConstraintValidatorInterface |
| 25 | + { |
| 26 | + return new TestCustomValidator(); |
| 27 | + } |
| 28 | + |
| 29 | + public function testAssertingContextualValidatorRemainingExpectationsThrow() |
| 30 | + { |
| 31 | + $this->expectValidateValueAt(0, 'k1', 'ccc', [ |
| 32 | + new NotNull(), |
| 33 | + ]); |
| 34 | + $this->expectValidateValueAt(1, 'k2', 'ccc', [ |
| 35 | + new DateTime(), |
| 36 | + ]); |
| 37 | + |
| 38 | + $this->validator->validate('ccc', $this->constraint); |
| 39 | + |
| 40 | + $contextualValidator = $this->context->getValidator()->inContext($this->context); |
| 41 | + // Simulate __destruct to assert it throws |
| 42 | + try { |
| 43 | + $contextualValidator->__destruct(); |
| 44 | + $this->fail(); |
| 45 | + } catch (ExpectationFailedException $e) { |
| 46 | + } |
| 47 | + |
| 48 | + // Actually fulfill expectations so real __destruct doesn't throw |
| 49 | + $contextualValidator |
| 50 | + ->atPath('k2') |
| 51 | + ->validate('ccc', [ |
| 52 | + new DateTime(), |
| 53 | + ]); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +class TestCustomValidator extends ConstraintValidator |
| 58 | +{ |
| 59 | + public function validate($value, Constraint $constraint) |
| 60 | + { |
| 61 | + $validator = $this->context |
| 62 | + ->getValidator() |
| 63 | + ->inContext($this->context); |
| 64 | + |
| 65 | + $validator |
| 66 | + ->atPath('k1') |
| 67 | + ->validate($value, [ |
| 68 | + new NotNull(), |
| 69 | + ]); |
| 70 | + } |
| 71 | +} |
0 commit comments