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 8e5537b

Browse filesBrowse files
committed
[Validator] Simplified testing of violations
1 parent d671406 commit 8e5537b
Copy full SHA for 8e5537b
Expand file treeCollapse file tree

36 files changed

+516
-329
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->buildViolation('myMessage')
163+
->atPath('property.path.name')
164+
->setInvalidValue('Foo')
165+
->assertRaised();
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->buildViolation('myMessage')
186+
->atPath('property.path.bar')
187+
->setInvalidValue('Foo')
188+
->assertRaised();
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->buildViolation('myMessage')
237+
->atPath('property.path.name')
238+
->setInvalidValue('Foo')
239+
->assertRaised();
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->buildViolation('myMessage')
334+
->atPath('property.path.single')
335+
->setInvalidValue(1)
336+
->assertRaised();
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->buildViolation('invalid_message_key')
223+
->setParameter('{{ value }}', 'foo')
224+
->setParameter('{{ foo }}', 'bar')
225+
->setInvalidValue('foo')
226+
->setCode(Form::ERR_INVALID)
227+
->assertRaised();
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->buildViolation('invalid_message_key')
257+
->setParameter('{{ value }}', 'foo')
258+
->setParameter('{{ foo }}', 'bar')
259+
->setInvalidValue('foo')
260+
->setCode(Form::ERR_INVALID)
261+
->assertRaised();
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->buildViolation('invalid_message_key')
291+
->setParameter('{{ value }}', 'foo')
292+
->setInvalidValue('foo')
293+
->setCode(Form::ERR_INVALID)
294+
->assertRaised();
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->buildViolation('Extra!')
547+
->setParameter('{{ extra_fields }}', 'foo')
548+
->setInvalidValue(array('foo' => 'bar'))
549+
->assertRaised();
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->buildViolation('Constraint Message')
88+
->setParameter('{{ value }}', $dirtyValueAsString)
89+
->setParameter('{{ compared_value }}', $comparedValueString)
90+
->setParameter('{{ compared_value_type }}', $comparedValueType)
91+
->assertRaised();
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
+170-6Lines changed: 170 additions & 6 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;
@@ -80,6 +79,19 @@ protected function createContext()
8079
->getMock();
8180
}
8281

82+
/**
83+
* @param $message
84+
* @param array $parameters
85+
* @param string $propertyPath
86+
* @param string $invalidValue
87+
* @param null $plural
88+
* @param null $code
89+
*
90+
* @return ConstraintViolation
91+
*
92+
* @deprecated To be removed in Symfony 3.0. Use
93+
* {@link buildViolation()} instead.
94+
*/
8395
protected function createViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null)
8496
{
8597
return new ConstraintViolation(
@@ -169,14 +181,34 @@ protected function assertNoViolation()
169181
$this->assertCount(0, $this->context->getViolations());
170182
}
171183

184+
/**
185+
* @param $message
186+
* @param array $parameters
187+
* @param string $propertyPath
188+
* @param string $invalidValue
189+
* @param null $plural
190+
* @param null $code
191+
*
192+
* @deprecated To be removed in Symfony 3.0. Use
193+
* {@link buildViolation()} instead.
194+
*/
172195
protected function assertViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null)
173196
{
174-
$violations = $this->context->getViolations();
175-
176-
$this->assertCount(1, $violations);
177-
$this->assertEquals($this->createViolation($message, $parameters, $propertyPath, $invalidValue, $plural, $code), $violations[0]);
197+
$this->buildViolation($message)
198+
->setParameters($parameters)
199+
->atPath($propertyPath)
200+
->setInvalidValue($invalidValue)
201+
->setCode($code)
202+
->setPlural($plural)
203+
->assertRaised();
178204
}
179205

206+
/**
207+
* @param array $expected
208+
*
209+
* @deprecated To be removed in Symfony 3.0. Use
210+
* {@link buildViolation()} instead.
211+
*/
180212
protected function assertViolations(array $expected)
181213
{
182214
$violations = $this->context->getViolations();
@@ -190,5 +222,137 @@ protected function assertViolations(array $expected)
190222
}
191223
}
192224

225+
/**
226+
* @param $message
227+
*
228+
* @return ConstraintViolationAssertion
229+
*/
230+
protected function buildViolation($message)
231+
{
232+
return new ConstraintViolationAssertion($this->context, $message);
233+
}
234+
193235
abstract protected function createValidator();
194236
}
237+
238+
/**
239+
* @internal
240+
*/
241+
class ConstraintViolationAssertion
242+
{
243+
/**
244+
* @var ExecutionContextInterface
245+
*/
246+
private $context;
247+
248+
/**
249+
* @var ConstraintViolationAssertion[]
250+
*/
251+
private $assertions;
252+
253+
private $message;
254+
private $parameters = array();
255+
private $invalidValue = 'InvalidValue';
256+
private $propertyPath = 'property.path';
257+
private $translationDomain;
258+
private $plural;
259+
private $code;
260+
261+
public function __construct(ExecutionContextInterface $context, $message, array $assertions = array())
262+
{
263+
$this->context = $context;
264+
$this->message = $message;
265+
$this->assertions = $assertions;
266+
}
267+
268+
public function atPath($path)
269+
{
270+
$this->propertyPath = $path;
271+
272+
return $this;
273+
}
274+
275+
public function setParameter($key, $value)
276+
{
277+
$this->parameters[$key] = $value;
278+
279+
return $this;
280+
}
281+
282+
public function setParameters(array $parameters)
283+
{
284+
$this->parameters = $parameters;
285+
286+
return $this;
287+
}
288+
289+
public function setTranslationDomain($translationDomain)
290+
{
291+
$this->translationDomain = $translationDomain;
292+
293+
return $this;
294+
}
295+
296+
public function setInvalidValue($invalidValue)
297+
{
298+
$this->invalidValue = $invalidValue;
299+
300+
return $this;
301+
}
302+
303+
public function setPlural($number)
304+
{
305+
$this->plural = $number;
306+
307+
return $this;
308+
}
309+
310+
public function setCode($code)
311+
{
312+
$this->code = $code;
313+
314+
return $this;
315+
}
316+
317+
public function buildNextViolation($message)
318+
{
319+
$assertions = $this->assertions;
320+
$assertions[] = $this;
321+
322+
return new self($this->context, $message, $assertions);
323+
}
324+
325+
public function assertRaised()
326+
{
327+
$expected = array();
328+
foreach ($this->assertions as $assertion) {
329+
$expected[] = $assertion->getViolation();
330+
}
331+
$expected[] = $this->getViolation();
332+
333+
$violations = iterator_to_array($this->context->getViolations());
334+
335+
\PHPUnit_Framework_Assert::assertCount(count($expected), $violations);
336+
337+
reset($violations);
338+
339+
foreach ($expected as $violation) {
340+
\PHPUnit_Framework_Assert::assertEquals($violation, current($violations));
341+
next($violations);
342+
}
343+
}
344+
345+
private function getViolation()
346+
{
347+
return new ConstraintViolation(
348+
null,
349+
$this->message,
350+
$this->parameters,
351+
$this->context->getRoot(),
352+
$this->propertyPath,
353+
$this->invalidValue,
354+
$this->plural,
355+
$this->code
356+
);
357+
}
358+
}

‎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->buildViolation('myMessage')
50+
->setParameter('{{ value }}', $valueAsString)
51+
->assertRaised();
5352
}
5453

5554
public function getInvalidValues()

0 commit comments

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