Skip to content

Commit b829f1a

Browse files
Merge branch '3.4' into 4.4
* 3.4: updated VERSION for 3.4.40 update CONTRIBUTORS for 3.4.40 updated CHANGELOG for 3.4.40 [WebProfilerBundle] changed label of peak memory usage in the time & memory panels (MB into MiB) add tests for the ConstraintViolationBuilder class Improve dirname usage [PhpUnitBridge] Use COMPOSER_BINARY env var if available [YAML] escape DEL(\x7f) fix compatibility with phpunit 9 [Cache] skip APCu in chains when the backend is disabled [Form] apply automatically step=1 for datetime-local input
2 parents 5a0337d + 0d4d26b commit b829f1a

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

Tests/Violation/ConstraintViolationBuilderTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,98 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Translation\IdentityTranslator;
16+
use Symfony\Component\Validator\Constraints\Valid;
17+
use Symfony\Component\Validator\ConstraintViolation;
1618
use Symfony\Component\Validator\ConstraintViolationList;
19+
use Symfony\Component\Validator\Test\ForwardCompatTestTrait;
1720
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
1821
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
1922

2023
class ConstraintViolationBuilderTest extends TestCase
2124
{
25+
use ForwardCompatTestTrait;
26+
27+
private $root;
28+
private $violations;
29+
private $messageTemplate = '%value% is invalid';
30+
private $builder;
31+
32+
private function doSetUp()
33+
{
34+
$this->root = [
35+
'data' => [
36+
'foo' => 'bar',
37+
'baz' => 'foobar',
38+
],
39+
];
40+
$this->violations = new ConstraintViolationList();
41+
$this->builder = new ConstraintViolationBuilder($this->violations, new Valid(), $this->messageTemplate, [], $this->root, 'data', 'foo', new IdentityTranslator());
42+
}
43+
44+
public function testAddViolation()
45+
{
46+
$this->builder->addViolation();
47+
48+
$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, null, new Valid()));
49+
}
50+
51+
public function testAppendPropertyPath()
52+
{
53+
$this->builder
54+
->atPath('foo')
55+
->addViolation();
56+
57+
$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data.foo', 'foo', null, null, new Valid()));
58+
}
59+
60+
public function testAppendMultiplePropertyPaths()
61+
{
62+
$this->builder
63+
->atPath('foo')
64+
->atPath('bar')
65+
->addViolation();
66+
67+
$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data.foo.bar', 'foo', null, null, new Valid()));
68+
}
69+
70+
public function testCodeCanBeSet()
71+
{
72+
$this->builder
73+
->setCode(5)
74+
->addViolation();
75+
76+
$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, 5, new Valid()));
77+
}
78+
79+
public function testCauseCanBeSet()
80+
{
81+
$cause = new \LogicException();
82+
83+
$this->builder
84+
->setCause($cause)
85+
->addViolation();
86+
87+
$this->assertViolationEquals(new ConstraintViolation($this->messageTemplate, $this->messageTemplate, [], $this->root, 'data', 'foo', null, null, new Valid(), $cause));
88+
}
89+
90+
private function assertViolationEquals(ConstraintViolation $expectedViolation)
91+
{
92+
$this->assertCount(1, $this->violations);
93+
94+
$violation = $this->violations->get(0);
95+
96+
$this->assertSame($expectedViolation->getMessage(), $violation->getMessage());
97+
$this->assertSame($expectedViolation->getMessageTemplate(), $violation->getMessageTemplate());
98+
$this->assertSame($expectedViolation->getParameters(), $violation->getParameters());
99+
$this->assertSame($expectedViolation->getPlural(), $violation->getPlural());
100+
$this->assertSame($expectedViolation->getRoot(), $violation->getRoot());
101+
$this->assertSame($expectedViolation->getPropertyPath(), $violation->getPropertyPath());
102+
$this->assertSame($expectedViolation->getInvalidValue(), $violation->getInvalidValue());
103+
$this->assertSame($expectedViolation->getCode(), $violation->getCode());
104+
$this->assertEquals($expectedViolation->getConstraint(), $violation->getConstraint());
105+
$this->assertSame($expectedViolation->getCause(), $violation->getCause());
106+
}
107+
22108
/**
23109
* @group legacy
24110
* @expectedDeprecation Not using a string as the error code in Symfony\Component\Validator\Violation\ConstraintViolationBuilder::setCode() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.

0 commit comments

Comments
 (0)