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 7264f61

Browse filesBrowse files
committed
[Validator] Simplified testing of violations
1 parent d0537e0 commit 7264f61
Copy full SHA for 7264f61

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

46 files changed

+462
-325
lines changed

‎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
+128-12Lines changed: 128 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,143 @@ 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, $this->context, $message);
179+
}
180+
181+
abstract protected function createValidator();
182+
}
183+
184+
/**
185+
* @internal
186+
*/
187+
class ConstraintViolationAssertion
188+
{
189+
/**
190+
* @var \PHPUnit_Framework_TestCase
191+
*/
192+
protected $testCase;
193+
194+
/**
195+
* @var ExecutionContextInterface
196+
*/
197+
protected $context;
198+
199+
/**
200+
* @var ConstraintViolationAssertion[]
201+
*/
202+
protected $assertions;
203+
204+
protected $message;
205+
protected $parameters = array();
206+
protected $invalidValue = 'InvalidValue';
207+
protected $propertyPath = 'property.path';
208+
protected $translationDomain;
209+
protected $plural;
210+
protected $code;
211+
212+
public function __construct(\PHPUnit_Framework_TestCase $testCase, ExecutionContextInterface $context, $message, array $assertions = array())
213+
{
214+
$this->testCase = $testCase;
215+
$this->context = $context;
216+
$this->message = $message;
217+
$this->assertions = $assertions;
218+
}
219+
220+
public function withPath($path)
221+
{
222+
$this->propertyPath = $path;
223+
224+
return $this;
225+
}
226+
227+
public function withParameter($key, $value)
228+
{
229+
$this->parameters[$key] = $value;
230+
231+
return $this;
232+
}
233+
234+
public function withParameters(array $parameters)
235+
{
236+
$this->parameters = $parameters;
237+
238+
return $this;
239+
}
240+
241+
public function withTranslationDomain($translationDomain)
242+
{
243+
$this->translationDomain = $translationDomain;
244+
245+
return $this;
246+
}
247+
248+
public function withInvalidValue($invalidValue)
173249
{
174-
$violations = $this->context->getViolations();
250+
$this->invalidValue = $invalidValue;
175251

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

180-
protected function assertViolations(array $expected)
255+
public function withPlural($number)
181256
{
182-
$violations = $this->context->getViolations();
257+
$this->plural = $number;
258+
259+
return $this;
260+
}
183261

184-
$this->assertCount(count($expected), $violations);
262+
public function withCode($code)
263+
{
264+
$this->code = $code;
185265

186-
$i = 0;
266+
return $this;
267+
}
268+
269+
public function andViolation($message)
270+
{
271+
$assertions = $this->assertions;
272+
$assertions[] = $this;
273+
274+
return new self($this->testCase, $this->context, $message, $assertions);
275+
}
276+
277+
public function assertNow()
278+
{
279+
$expected = array();
280+
foreach ($this->assertions as $assertion) {
281+
$expected[] = $assertion->getViolation();
282+
}
283+
$expected[] = $this->getViolation();
284+
285+
$violations = iterator_to_array($this->context->getViolations());
286+
287+
$this->testCase->assertCount(count($expected), $violations);
288+
289+
reset($violations);
187290

188291
foreach ($expected as $violation) {
189-
$this->assertEquals($violation, $violations[$i++]);
292+
$this->testCase->assertEquals($violation, current($violations));
293+
next($violations);
190294
}
191295
}
192296

193-
abstract protected function createValidator();
297+
private function getViolation()
298+
{
299+
return new ConstraintViolation(
300+
null,
301+
$this->message,
302+
$this->parameters,
303+
$this->context->getRoot(),
304+
$this->propertyPath,
305+
$this->invalidValue,
306+
$this->plural,
307+
$this->code
308+
);
309+
}
194310
}

‎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
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public function testInvalidNumbers($scheme, $number)
5757

5858
$this->validator->validate($number, $constraint);
5959

60-
$this->assertViolation('myMessage', array(
61-
'{{ value }}' => is_string($number) ? '"'.$number.'"' : $number,
62-
));
60+
$this->assertViolation('myMessage')
61+
->withParameter('{{ value }}', is_string($number) ? '"'.$number.'"' : $number)
62+
->assertNow();
6363
}
6464

6565
public function getValidNumbers()

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php
+24-18Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ public function testInvalidChoice()
138138

139139
$this->validator->validate('baz', $constraint);
140140

141-
$this->assertViolation('myMessage', array(
142-
'{{ value }}' => '"baz"',
143-
));
141+
$this->assertViolation('myMessage')
142+
->withParameter('{{ value }}', '"baz"')
143+
->assertNow();
144144
}
145145

146146
public function testInvalidChoiceMultiple()
@@ -153,9 +153,9 @@ public function testInvalidChoiceMultiple()
153153

154154
$this->validator->validate(array('foo', 'baz'), $constraint);
155155

156-
$this->assertViolation('myMessage', array(
157-
'{{ value }}' => '"baz"',
158-
));
156+
$this->assertViolation('myMessage')
157+
->withParameter('{{ value }}', '"baz"')
158+
->assertNow();
159159
}
160160

161161
public function testTooFewChoices()
@@ -173,9 +173,12 @@ public function testTooFewChoices()
173173

174174
$this->validator->validate($value, $constraint);
175175

176-
$this->assertViolation('myMessage', array(
177-
'{{ limit }}' => 2,
178-
), 'property.path', $value, 2);
176+
$this->assertViolation('myMessage')
177+
->withParameter('{{ limit }}', 2)
178+
->withPath('property.path')
179+
->withInvalidValue($value)
180+
->withPlural(2)
181+
->assertNow();
179182
}
180183

181184
public function testTooManyChoices()
@@ -193,9 +196,12 @@ public function testTooManyChoices()
193196

194197
$this->validator->validate($value, $constraint);
195198

196-
$this->assertViolation('myMessage', array(
197-
'{{ limit }}' => 2,
198-
), 'property.path', $value, 2);
199+
$this->assertViolation('myMessage')
200+
->withParameter('{{ limit }}', 2)
201+
->withPath('property.path')
202+
->withInvalidValue($value)
203+
->withPlural(2)
204+
->assertNow();
199205
}
200206

201207
public function testNonStrict()
@@ -233,9 +239,9 @@ public function testStrictDisallowsDifferentType()
233239

234240
$this->validator->validate('2', $constraint);
235241

236-
$this->assertViolation('myMessage', array(
237-
'{{ value }}' => '"2"',
238-
));
242+
$this->assertViolation('myMessage')
243+
->withParameter('{{ value }}', '"2"')
244+
->assertNow();
239245
}
240246

241247
public function testNonStrictWithMultipleChoices()
@@ -262,8 +268,8 @@ public function testStrictWithMultipleChoices()
262268

263269
$this->validator->validate(array(2, '3'), $constraint);
264270

265-
$this->assertViolation('myMessage', array(
266-
'{{ value }}' => '"3"',
267-
));
271+
$this->assertViolation('myMessage')
272+
->withParameter('{{ value }}', '"3"')
273+
->assertNow();
268274
}
269275
}

0 commit comments

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