|
| 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\Violation; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Translation\IdentityTranslator; |
| 16 | +use Symfony\Component\Validator\Constraints\Valid; |
| 17 | +use Symfony\Component\Validator\ConstraintViolation; |
| 18 | +use Symfony\Component\Validator\ConstraintViolationList; |
| 19 | +use Symfony\Component\Validator\Test\ForwardCompatTestTrait; |
| 20 | +use Symfony\Component\Validator\Violation\ConstraintViolationBuilder; |
| 21 | + |
| 22 | +class ConstraintViolationBuilderTest extends TestCase |
| 23 | +{ |
| 24 | + use ForwardCompatTestTrait; |
| 25 | + |
| 26 | + private $root; |
| 27 | + private $violations; |
| 28 | + private $messageTemplate = '%value% is invalid'; |
| 29 | + private $builder; |
| 30 | + |
| 31 | + private function doSetUp() |
| 32 | + { |
| 33 | + $this->root = [ |
| 34 | + 'data' => [ |
| 35 | + 'foo' => 'bar', |
| 36 | + 'baz' => 'foobar', |
| 37 | + ], |
| 38 | + ]; |
| 39 | + $this->violations = new ConstraintViolationList(); |
| 40 | + $this->builder = new ConstraintViolationBuilder($this->violations, new Valid(), $this->messageTemplate, [], $this->root, 'data', 'foo', new IdentityTranslator()); |
| 41 | + } |
| 42 | + |
| 43 | + public function testAddViolation() |
| 44 | + { |
| 45 | + $this->builder->addViolation(); |
| 46 | + |
| 47 | + $this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, null, new Valid())); |
| 48 | + } |
| 49 | + |
| 50 | + public function testAppendPropertyPath() |
| 51 | + { |
| 52 | + $this->builder |
| 53 | + ->atPath('foo') |
| 54 | + ->addViolation(); |
| 55 | + |
| 56 | + $this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data.foo', 'foo', null, null, new Valid())); |
| 57 | + } |
| 58 | + |
| 59 | + public function testAppendMultiplePropertyPaths() |
| 60 | + { |
| 61 | + $this->builder |
| 62 | + ->atPath('foo') |
| 63 | + ->atPath('bar') |
| 64 | + ->addViolation(); |
| 65 | + |
| 66 | + $this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data.foo.bar', 'foo', null, null, new Valid())); |
| 67 | + } |
| 68 | + |
| 69 | + public function testCodeCanBeSet() |
| 70 | + { |
| 71 | + $this->builder |
| 72 | + ->setCode(5) |
| 73 | + ->addViolation(); |
| 74 | + |
| 75 | + $this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, 5, new Valid())); |
| 76 | + } |
| 77 | + |
| 78 | + public function testCauseCanBeSet() |
| 79 | + { |
| 80 | + $cause = new \LogicException(); |
| 81 | + |
| 82 | + $this->builder |
| 83 | + ->setCause($cause) |
| 84 | + ->addViolation(); |
| 85 | + |
| 86 | + $this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, null, new Valid(), $cause)); |
| 87 | + } |
| 88 | + |
| 89 | + private function assertViolationEquals(ConstraintViolation $expectedViolation) |
| 90 | + { |
| 91 | + $this->assertCount(1, $this->violations); |
| 92 | + |
| 93 | + $violation = $this->violations->get(0); |
| 94 | + |
| 95 | + $this->assertSame($expectedViolation->getMessage(), $violation->getMessage()); |
| 96 | + $this->assertSame($expectedViolation->getMessageTemplate(), $violation->getMessageTemplate()); |
| 97 | + $this->assertSame($expectedViolation->getParameters(), $violation->getParameters()); |
| 98 | + $this->assertSame($expectedViolation->getPlural(), $violation->getPlural()); |
| 99 | + $this->assertSame($expectedViolation->getRoot(), $violation->getRoot()); |
| 100 | + $this->assertSame($expectedViolation->getPropertyPath(), $violation->getPropertyPath()); |
| 101 | + $this->assertSame($expectedViolation->getInvalidValue(), $violation->getInvalidValue()); |
| 102 | + $this->assertSame($expectedViolation->getCode(), $violation->getCode()); |
| 103 | + $this->assertEquals($expectedViolation->getConstraint(), $violation->getConstraint()); |
| 104 | + $this->assertSame($expectedViolation->getCause(), $violation->getCause()); |
| 105 | + } |
| 106 | +} |
0 commit comments