8000 [Validator] Simplified testing of violations · symfony/symfony@2848f63 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2848f63

Browse files
committed
[Validator] Simplified testing of violations
1 parent 02c2cd2 commit 2848f63

35 files changed

+434
-293
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
134134

135135
$this->validator->validate($dirtyValue, $constraint);
136136

137-
$this->assertViolation('Constraint Message', array(
138-
'{{ value }}' => $dirtyValueAsString,
139-
'{{ compared_value }}' => $comparedValueString,
140-
'{{ compared_value_type }}' => $comparedValueType,
141-
));
137+
$this->assertViolation('Constraint Message')
138+
->withParameter('{{ value }}', $dirtyValueAsString)
139+
->withParameter('{{ compared_value }}', $comparedValueString)
140+
->withParameter('{{ compared_value_type }}', $comparedValueType)
141+
->assertNow();
142142
}
143143

144144
/**

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

Lines changed: 134 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\Validator\Constraint;
1415
use Symfony\Component\Validator\Constraints\NotNull;
1516
use Symfony\Component\Validator\ConstraintValidatorInterface;
1617
use Symfony\Component\Validator\ConstraintViolation;
1718
use Symfony\Component\Validator\Context\ExecutionContext;
1819
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1920
use Symfony\Component\Validator\Context\LegacyExecutionContext;
21+
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
2022
use Symfony\Component\Validator\Mapping\ClassMetadata;
2123
use Symfony\Component\Validator\Mapping\PropertyMetadata;
2224
use Symfony\Component\Validator\Tests\Fixtures\StubGlobalExecutionContext;
@@ -334,28 +336,148 @@ protected function assertNoViolation()
334336
$this->assertCount(0, $this->context->getViolations());
335337
}
336338

337-
protected function assertViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null)
339+
/**
340+
* @param $message
341+
*
342+
* @return ConstraintViolationAssertion
343+
*/
344+
protected function assertViolation($message)
345+
{
346+
return new ConstraintViolationAssertion($this, $this->context, $message, $this->constraint);
347+
}
348+
349+
abstract protected function getApiVersion();
350+
351+
abstract protected function createValidator();
352+
}
353+
354+
/**
355+
* @internal
356+
*/
357+
class ConstraintViolationAssertion
358+
{
359+
/**
360+
* @var \PHPUnit_Framework_TestCase
361+
*/
362+
protected $testCase;
363+
364+
/**
365+
* @var LegacyExecutionContextInterface
366+
*/
367+
protected $context;
368+
369+
/**
370+
* @var ConstraintViolationAssertion[]
371+
*/
372+
protected $assertions;
373+
374+
protected $message;
375+
protected $parameters = array();
376+
protected $invalidValue = 'InvalidValue';
377+
protected $propertyPath = 'property.path';
378+
protected $translationDomain;
379+
protected $plural;
380+
protected $code;
381+
protected $constraint;
382+
383+
public function __construct(\PHPUnit_Framework_TestCase $testCase, LegacyExecutionContextInterface $context, $message, Constraint $constraint = null, array $assertions = array())
384+
{
385+
$this->testCase = $testCase;
386+
$this->context = $context;
387+
$this->message = $message;
388+
$this->constraint = $constraint;
389+
$this->assertions = $assertions;
390+
}
391+
392+
public function withPath($path)
393+
{
394+
$this->propertyPath = $path;
395+
396+
return $this;
397+
}
398+
399+
public function withParameter($key, $value)
400+
{
401+
$this->parameters[$key] = $value;
402+
403+
return $this;
404+
}
405+
406+
public function withParameters(array $parameters)
338407
{
339-
$violations = $this->context->getViolations();
408+
$this->parameters = $parameters;
340409

341-
$this->assertCount(1, $violations);
342-
$this->assertEquals($this->createViolation($message, $parameters, $propertyPath, $invalidValue, $plural, $code), current(iterator_to_array($violations)));
410+
return $this;
343411
}
344412

345-
protected function assertViolations(array $expected)
413+
public function withTranslationDomain($translationDomain)
346414
{
347-
$violations = $this->context->getViolations();
415+
$this->translationDomain = $translationDomain;
348416

349-
$this->assertCount(count($expected), $violations);
417+
return $this;
418+
}
350419

351-
$i = 0;
420+
public function withInvalidValue($invalidValue)
421+
{
422+
$this->invalidValue = $invalidValue;
423+
424+
return $this;
425+
}
426+
427+
public function withPlural($number)
428+
{
429+
$this->plural = $number;
430+
431+
return $this;
432+
}
433+
434+
public function withCode($code)
435+
{
436+
$this->code = $code;
437+
438+
return $this;
439+
}
440+
441+
public function andViolation($message)
442+
{
443+
$assertions = $this->assertions;
444+
$assertions[] = $this;
445+
446+
return new self($this->testCase, $this->context, $message, $this->constraint, $assertions);
447+
}
448+
449+
public function assertNow()
450+
{
451+
$expected = array();
452+
foreach ($this->assertions as $assertion) {
453+
$expected[] = $assertion->getViolation();
454+
}
455+
$expected[] = $this->getViolation();
456+
457+
$violations = iterator_to_array($this->context->getViolations());
458+
459+
$this->testCase->assertCount(count($expected), $violations);
460+
461+
reset($violations);
352462

353463
foreach ($expected as $violation) {
354-
$this->assertEquals($violation, $violations[$i++]);
464+
$this->testCase->assertEquals($violation, current($violations));
465+
next($violations);
355466
}
356467
}
357468

358-
abstract protected function getApiVersion();
359-
360-
abstract protected function createValidator();
469+
private function getViolation()
470+
{
471+
return new ConstraintViolation(
472+
null,
473+
$this->message,
474+
$this->parameters,
475+
$this->context->getRoot(),
476+
$this->propertyPath,
477+
$this->invalidValue,
478+
$this->plural,
479+
$this->code,
480+
$this->constraint
481+
);
482+
}
361483
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ public function testInvalidValues($value, $valueAsString)
5252

5353
$this->validator->validate($value, $constraint);
5454

55-
$this->assertViolation(
56-
'myMessage',
57-
array('{{ value }}' => $valueAsString)
58-
);
55+
$this->assertViolation('myMessage')
56+
->withParameter('{{ value }}', $valueAsString)
57+
->assertNow();
5958
}
6059

6160
public function getInvalidValues()

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

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public function testSingleMethod()
7070

7171
$this->validator->validate($object, $constraint);
7272

73-
$this->assertViolation('My message', array(
74-
'{{ value }}' => 'foobar',
75-
));
73+
$this->assertViolation('My message')
74+
->withParameter('{{ value }}', 'foobar')
75+
->assertNow();
7676
}
7777

7878
public function testSingleMethodExplicitName()
@@ -206,9 +206,9 @@ public function testSingleMethodBcExplicitName()
206206

207207
$this->validator->validate($object, $constraint);
208208

209-
$this->assertViolation('My message', array(
210-
'{{ value }}' => 'foobar',
211-
));
209+
$this->assertViolation('My message')
210+
->withParameter('{{ value }}', 'foobar')
211+
->assertNow();
212212
}
213213

214214
// BC with Symfony < 2.4
@@ -219,14 +219,11 @@ public function testMultipleMethodsBc()
219219

220220
$this->validator->validate($object, $constraint);
221221

222-
$this->assertViolations(array(
223-
$this->createViolation('My message', array(
224-
'{{ value }}' => 'foobar',
225-
)),
226-
$this->createViolation('Static message', array(
227-
'{{ value }}' => 'baz',
228-
)),
229-
));
222+
$this->assertViolation('My message')
223+
->withParameter('{{ value }}', 'foobar')
224+
->andViolation('Static message')
225+
->withParameter('{{ value }}', 'baz')
226+
->assertNow();
230227
}
231228

232229
// BC with Symfony < 2.4
@@ -239,14 +236,11 @@ public function testMultipleMethodsBcExplicitName()
239236

240237
$this->validator->validate($object, $constraint);
241238

242-
$this->assertViolations(array(
243-
$this->createViolation('My message', array(
244-
'{{ value }}' => 'foobar',
245-
)),
246-
$this->createViolation('Static message', array(
247-
'{{ value }}' => 'baz',
248-
)),
249-
));
239+
$this->assertViolation('My message')
240+
->withParameter('{{ value }}', 'foobar')
241+
->andViolation('Static message')
242+
->withParameter('{{ value }}', 'baz')
243+
->assertNow();
250244
}
251245

252246
// BC with Symfony < 2.4
@@ -259,9 +253,9 @@ public function testSingleStaticMethodBc()
259253

260254
$this->validator->validate($object, $constraint);
261255

262-
$this->assertViolation('Callback message', array(
263-
'{{ value }}' => 'foobar',
264-
));
256+
$this->assertViolation('Callback message')
257+
->withParameter('{{ value }}', 'foobar')
258+
->assertNow();
265259
}
266260

267261
// BC with Symfony < 2.4
@@ -274,9 +268,9 @@ public function testSingleStaticMethodBcExplicitName()
274268

275269
$this->validator->validate($object, $constraint);
276270

277-
$this->assertViolation('Callback message', array(
278-
'{{ value }}' => 'foobar',
279-
));
271+
$this->assertViolation('Callback message')
272+
->withParameter('{{ value }}', 'foobar')
273+
->assertNow();
280274
}
281275

282276
/**

src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function testInvalidNumbers($scheme, $number)
6363

6464
$this->validator->validate($number, $constraint);
6565

66-
$this->assertViolation('myMessage', array(
67-
'{{ value }}' => is_string($number) ? '"'.$number.'"' : $number,
68-
));
66+
$this->assertViolation('myMessage')
67+
->withParameter('{{ value }}', is_string($number) ? '"'.$number.'"' : $number)
68+
->assertNow();
6969
}
7070

7171
public function getValidNumbers()

src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ public function testInvalidChoice()
144144

145145
$this->validator->validate('baz', $constraint);
146146

147-
$this->assertViolation('myMessage', array(
148-
'{{ value }}' => '"baz"',
149-
));
147+
$this->assertViolation('myMessage')
148+
->withParameter('{{ value }}', '"baz"')
149+
->assertNow();
150150
}
151151

152152
public function testInvalidChoiceMultiple()
@@ -159,9 +159,10 @@ public function testInvalidChoiceMultiple()
159159

160160
$this->validator->validate(array('foo', 'baz'), $constraint);
161161

162-
$this->assertViolation('myMessage', array(
163-
'{{ value }}' => '"baz"',
164-
), 'property.path', 'baz');
162+
$this->assertViolation('myMessage')
163+
->withParameter('{{ value }}', '"baz"')
164+
->withInvalidValue('baz')
165+
->assertNow();
165166
}
166167

167168
public function testTooFewChoices()
@@ -179,9 +180,11 @@ public function testTooFewChoices()
179180

180181
$this->validator->validate($value, $constraint);
181182

182-
$this->assertViolation('myMessage', array(
183-
'{{ limit }}' => 2,
184-
), 'property.path', $value, 2);
183+
$this->assertViolation('myMessage')
184+
->withParameter('{{ limit }}', 2)
185+
->withInvalidValue($value)
186+
->withPlural(2)
187+
->assertNow();
185188
}
186189

187190
public function testTooManyChoices()
@@ -199,9 +202,11 @@ public function testTooManyChoices()
199202

200203
$this->validator->validate($value, $constraint);
201204

202-
$this->assertViolation('myMessage', array(
203-
'{{ limit }}' => 2,
204-
), 'property.path', $value, 2);
205+
$this->assertViolation('myMessage')
206+
->withParameter('{{ limit }}', 2)
207+
->withInvalidValue($value)
208+
->withPlural(2)
209+
->assertNow();
205210
}
206211

207212
public function testNonStrict()
@@ -239,9 +244,9 @@ public function testStrictDisallowsDifferentType()
239244

240245
$this->validator->validate('2', $constraint);
241246

242-
$this->assertViolation('myMessage', array(
243-
'{{ value }}' => '"2"',
244-
));
247+
$this->assertViolation('myMessage')
248+
->withParameter('{{ value }}', '"2"')
249+
->assertNow();
245250
}
246251

247252
public function testNonStrictWithMultipleChoices()
@@ -268,8 +273,9 @@ public function testStrictWithMultipleChoices()
268273

269274
$this->validator->validate(array(2, '3'), $constraint);
270275

271-
$this->assertViolation('myMessage', array(
272-
'{{ value }}' => '"3"',
273-
), 'property.path', '3');
276+
$this->assertViolation('myMessage')
277+
->withParameter('{{ value }}', '"3"')
278+
->withInvalidValue('3')
279+
->assertNow();
274280
}
275281
}

0 commit comments

Comments
 (0)
0