Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 53b5033

Browse filesBrowse files
committed
[Validator] Simplified testing of violations
1 parent d0537e0 commit 53b5033
Copy full SHA for 53b5033
Expand file treeCollapse file tree

36 files changed

+456
-311
lines changed

‎src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php
+16-4Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ public function testValidateUniqueness()
159159

160160
$this->validator->validate($entity2, $constraint);
161161

162-
$this->assertViolation('myMessage', array(), 'property.path.name', 'Foo');
162+
$this->assertViolation('myMessage')
163+
->withPath('property.path.name')
164+
->withInvalidValue('Foo')
165+
->assertNow();
163166
}
164167

165168
public function testValidateCustomErrorPath()
@@ -179,7 +182,10 @@ public function testValidateCustomErrorPath()
179182

180183
$this->validator->validate($entity2, $constraint);
181184

182-
$this->assertViolation('myMessage', array(), 'property.path.bar', 'Foo');
185+
$this->assertViolation('myMessage')
186+
->withPath('property.path.bar')
187+
->withInvalidValue('Foo')
188+
->assertNow();
183189
}
184190

185191
public function testValidateUniquenessWithNull()
@@ -227,7 +233,10 @@ public function testValidateUniquenessWithIgnoreNull()
227233

228234
$this->validator->validate($entity2, $constraint);
229235

230-
$this->assertViolation('myMessage', array(), 'property.path.name', 'Foo');
236+
$this->assertViolation('myMessage')
237+
->withPath('property.path.name')
238+
->withInvalidValue('Foo')
239+
->assertNow();
231240
}
232241

233242
public function testValidateUniquenessUsingCustomRepositoryMethod()
@@ -321,7 +330,10 @@ public function testAssociatedEntity()
321330

322331
$this->validator->validate($associated2, $constraint);
323332

324-
$this->assertViolation('myMessage', array(), 'property.path.single', 1);
333+
$this->assertViolation('myMessage')
334+
->withPath('property.path.single')
335+
->withInvalidValue(1)
336+
->assertNow();
325337
}
326338

327339
public function testAssociatedEntityWithNull()

‎src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php
+21-14Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,12 @@ function () { throw new TransformationFailedException(); }
219219

220220
$this->validator->validate($form, new Form());
221221

222-
$this->assertViolation('invalid_message_key', array(
223-
'{{ value }}' => 'foo',
224-
'{{ foo }}' => 'bar',
225-
), 'property.path', 'foo', null, Form::ERR_INVALID);
222+
$this->assertViolation('invalid_message_key')
223+
->withParameter('{{ value }}', 'foo')
224+
->withParameter('{{ foo }}', 'bar')
225+
->withInvalidValue('foo')
226+
->withCode(Form::ERR_INVALID)
227+
->assertNow();
226228
}
227229

228230
public function testAddInvalidErrorEvenIfNoValidationGroups()
@@ -251,10 +253,12 @@ function () { throw new TransformationFailedException(); }
251253

252254
$this->validator->validate($form, new Form());
253255

254-
$this->assertViolation('invalid_message_key', array(
255-
'{{ value }}' => 'foo',
256-
'{{ foo }}' => 'bar',
257-
), 'property.path', 'foo', null, Form::ERR_INVALID);
256+
$this->assertViolation('invalid_message_key')
257+
->withParameter('{{ value }}', 'foo')
258+
->withParameter('{{ foo }}', 'bar')
259+
->withInvalidValue('foo')
260+
->withCode(Form::ERR_INVALID)
261+
->assertNow();
258262
}
259263

260264
public function testDontValidateConstraintsIfNotSynchronized()
@@ -283,9 +287,11 @@ function () { throw new TransformationFailedException(); }
283287

284288
$this->validator->validate($form, new Form());
285289

286-
$this->assertViolation('invalid_message_key', array(
287-
'{{ value }}' => 'foo',
288-
), 'property.path','foo', null, Form::ERR_INVALID);
290+
$this->assertViolation('invalid_message_key')
291+
->withParameter('{{ value }}', 'foo')
292+
->withInvalidValue('foo')
293+
->withCode(Form::ERR_INVALID)
294+
->assertNow();
289295
}
290296

291297
// https://github.com/symfony/symfony/issues/4359
@@ -537,9 +543,10 @@ public function testViolationIfExtraData()
537543

538544
$this->validator->validate($form, new Form());
539545

540-
$this->assertViolation('Extra!', array(
541-
'{{ extra_fields }}' => 'foo',
542-
), 'property.path', array('foo' => 'bar'));
546+
$this->assertViolation('Extra!')
547+
->withParameter('{{ extra_fields }}', 'foo')
548+
->withInvalidValue(array('foo' => 'bar'))
549+
->assertNow();
543550
}
544551

545552
/**

‎src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
8484

8585
$this->validator->validate($dirtyValue, $constraint);
8686

87-
$this->assertViolation('Constraint Message', array(
88-
'{{ value }}' => $dirtyValueAsString,
89-
'{{ compared_value }}' => $comparedValueString,
90-
'{{ compared_value_type }}' => $comparedValueType,
91-
));
87+
$this->assertViolation('Constraint Message')
88+
->withParameter('{{ value }}', $dirtyValueAsString)
89+
->withParameter('{{ compared_value }}', $comparedValueString)
90+
->withParameter('{{ compared_value_type }}', $comparedValueType)
91+
->assertNow();
9292
}
9393

9494
/**

‎src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php
+122-12Lines changed: 122 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
use Symfony\Component\Validator\ConstraintValidatorInterface;
1515
use Symfony\Component\Validator\ConstraintViolation;
16-
use Symfony\Component\Validator\Context\ExecutionContext;
17-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
16+
use Symfony\Component\Validator\ExecutionContextInterface;
1817
use Symfony\Component\Validator\Mapping\ClassMetadata;
1918
use Symfony\Component\Validator\Mapping\PropertyMetadata;
2019
use Symfony\Component\Validator\Tests\Fixtures\StubGlobalExecutionContext;
@@ -169,26 +168,137 @@ protected function assertNoViolation()
169168
$this->assertCount(0, $this->context->getViolations());
170169
}
171170

172-
protected function assertViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null)
171+
/**
172+
* @param $message
173+
*
174+
* @return ConstraintViolationAssertion
175+
*/
176+
protected function assertViolation($message)
177+
{
178+
return new ConstraintViolationAssertion($this->context, $message);
179+
}
180+
181+
abstract protected function createValidator();
182+
}
183+
184+
/**
185+
* @internal
186+
*/
187+
class ConstraintViolationAssertion
188+
{
189+
/**
190+
* @var ExecutionContextInterface
191+
*/
192+
private $context;
193+
194+
/**
195+
* @var ConstraintViolationAssertion[]
196+
*/
197+
private $assertions;
198+
199+
private $message;
200+
private $parameters = array();
201+
private $invalidValue = 'InvalidValue';
202+
private $propertyPath = 'property.path';
203+
private $translationDomain;
204+
private $plural;
205+
private $code;
206+
207+
public function __construct(ExecutionContextInterface $context, $message, array $assertions = array())
208+
{
209+
$this->context = $context;
210+
$this->message = $message;
211+
$this->assertions = $assertions;
212+
}
213+
214+
public function withPath($path)
215+
{
216+
$this->propertyPath = $path;
217+
218+
return $this;
219+
}
220+
221+
public function withParameter($key, $value)
222+
{
223+
$this->parameters[$key] = $value;
224+
225+
return $this;
226+
}
227+
228+
public function withParameters(array $parameters)
229+
{
230+
$this->parameters = $parameters;
231+
232+
return $this;
233+
}
234+
235+
public function withTranslationDomain($translationDomain)
173236
{
174-
$violations = $this->context->getViolations();
237+
$this->translationDomain = $translationDomain;
175238

176-
$this->assertCount(1, $violations);
177-
$this->assertEquals($this->createViolation($message, $parameters, $propertyPath, $invalidValue, $plural, $code), $violations[0]);
239+
return $this;
178240
}
179241

180-
protected function assertViolations(array $expected)
242+
public function withInvalidValue($invalidValue)
181243
{
182-
$violations = $this->context->getViolations();
244+
$this->invalidValue = $invalidValue;
245+
246+
return $this;
247+
}
183248

184-
$this->assertCount(count($expected), $violations);
249+
public function withPlural($number)
250+
{
251+
$this->plural = $number;
185252

186-
$i = 0;
253+
return $this;
254+
}
255+
256+
public function withCode($code)
257+
{
258+
$this->code = $code;
259+
260+
return $this;
261+
}
262+
263+
public function andViolation($message)
264+
{
265+
$assertions = $this->assertions;
266+
$assertions[] = $this;
267+
268+
return new self($this->context, $message, $assertions);
269+
}
270+
271+
public function assertNow()
272+
{
273+
$expected = array();
274+
foreach ($this->assertions as $assertion) {
275+
$expected[] = $assertion->getViolation();
276+
}
277+
$expected[] = $this->getViolation();
278+
279+
$violations = iterator_to_array($this->context->getViolations());
280+
281+
\PHPUnit_Framework_Assert::assertCount(count($expected), $violations);
282+
283+
reset($violations);
187284

188285
foreach ($expected as $violation) {
189-
$this->assertEquals($violation, $violations[$i++]);
286+
\PHPUnit_Framework_Assert::assertEquals($violation, current($violations));
287+
next($violations);
190288
}
191289
}
192290

193-
abstract protected function createValidator();
291+
private function getViolation()
292+
{
293+
return new ConstraintViolation(
294+
null,
295+
$this->message,
296+
$this->parameters,
297+
$this->context->getRoot(),
298+
$this->propertyPath,
299+
$this->invalidValue,
300+
$this->plural,
301+
$this->code
302+
);
303+
}
194304
}

‎src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ public function testInvalidValues($value, $valueAsString)
4646

4747
$this->validator->validate($value, $constraint);
4848

49-
$this->assertViolation(
50-
'myMessage',
51-
array('{{ value }}' => $valueAsString)
52-
);
49+
$this->assertViolation('myMessage')
50+
->withParameter('{{ value }}', $valueAsString)
51+
->assertNow();
5352
}
5453

5554
public function getInvalidValues()

‎src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php
+22-28Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public function testSingleMethod()
6464

6565
$this->validator->validate($object, $constraint);
6666

67-
$this->assertViolation('My message', array(
68-
'{{ value }}' => 'foobar',
69-
));
67+
$this->assertViolation('My message')
68+
->withParameter('{{ value }}', 'foobar')
69+
->assertNow();
7070
}
7171

7272
public function testSingleMethodExplicitName()
@@ -76,9 +76,9 @@ public function testSingleMethodExplicitName()
7676

7777
$this->validator->validate($object, $constraint);
7878

79-
$this->assertViolation('My message', array(
80-
'{{ value }}' => 'foobar',
81-
));
79+
$this->assertViolation('My message')
80+
->withParameter('{{ value }}', 'foobar')
81+
->assertNow();
8282
}
8383

8484
public function testMultipleMethods()
@@ -88,14 +88,11 @@ public function testMultipleMethods()
8888

8989
$this->validator->validate($object, $constraint);
9090

91-
$this->assertViolations(array(
92-
$this->createViolation('My message', array(
93-
'{{ value }}' => 'foobar',
94-
)),
95-
$this->createViolation('Static message', array(
96-
'{{ value }}' => 'baz',
97-
)),
98-
));
91+
$this->assertViolation('My message')
92+
->withParameter('{{ value }}', 'foobar')
93+
->andViolation('Static message')
94+
->withParameter('{{ value }}', 'baz')
95+
->assertNow();
9996
}
10097

10198
public function testMultipleMethodsExplicitName()
@@ -107,14 +104,11 @@ public function testMultipleMethodsExplicitName()
107104

108105
$this->validator->validate($object, $constraint);
109106

110-
$this->assertViolations(array(
111-
$this->createViolation('My message', array(
112-
'{{ value }}' => 'foobar',
113-
)),
114-
$this->createViolation('Static message', array(
115-
'{{ value }}' => 'baz',
116-
)),
117-
));
107+
$this->assertViolation('My message')
108+
->withParameter('{{ value }}', 'foobar')
109+
->andViolation('Static message')
110+
->withParameter('{{ value }}', 'baz')
111+
->assertNow();
118112
}
119113

120114
public function testSingleStaticMethod()
@@ -126,9 +120,9 @@ public function testSingleStaticMethod()
126120

127121
$this->validator->validate($object, $constraint);
128122

129-
$this->assertViolation('Callback message', array(
130-
'{{ value }}' => 'foobar',
131-
));
123+
$this->assertViolation('Callback message')
124+
->withParameter('{{ value }}', 'foobar')
125+
->assertNow();
132126
}
133127

134128
public function testSingleStaticMethodExplicitName()
@@ -140,9 +134,9 @@ public function testSingleStaticMethodExplicitName()
140134

141135
$this->validator->validate($object, $constraint);
142136

143-
$this->assertViolation('Callback message', array(
144-
'{{ value }}' => 'foobar',
145-
));
137+
$this->assertViolation('Callback message')
138+
->withParameter('{{ value }}', 'foobar')
139+
->assertNow();
146140
}
147141

148142
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.