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

Skip to content

Commit 1203dca

Browse files
committed
[Validator] Simplified testing of violations
1 parent d671406 commit 1203dca

36 files changed

+501
-329
lines changed

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

Lines 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+
->setPath('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+
->setPath('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+
->setPath('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+
->setPath('property.path.single')
335+
->setInvalidValue(1)
336+
->assertRaised();
325337
}
326338

327339
public function testAssociatedEntityWithNull()

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

Lines 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
}
227 10000 229

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

Lines 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

Lines changed: 155 additions & 6 deletions
10000
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,14 +168,32 @@ protected function assertNoViolation()
169168
$this->assertCount(0, $this->context->getViolations());
170169
}
171170

171+
/**
172+
* @param $message
173+
* @param array $parameters
174+
* @param string $propertyPath
175+
* @param string $invalidValue
176+
* @param null $plural
177+
* @param null $code
178+
*
179+
* @deprecated
180+
*/
172181
protected function assertViolation($message, array $parameters = array(), $propertyPath = 'property.path', $invalidValue = 'InvalidValue', $plural = null, $code = null)
173182
{
174-
$violations = $this->context->getViolations();
175-
176-
$this->assertCount(1, $violations);
177-
$this->assertEquals($this->createViolation($message, $parameters, $propertyPath, $invalidValue, $plural, $code), $violations[0]);
183+
$this->buildViolation($message)
184+
->setParameters($parameters)
185+
->setPath($propertyPath)
186+
->setInvalidValue($invalidValue)
187+
->setCode($code)
188+
->setPlural($plural)
189+
->assertRaised();
178190
}
179191

192+
/**
193+
* @param array $expected
194+
*
195+
* @deprecated
196+
*/
180197
protected function assertViolations(array $expected)
181198
{
182199
$violations = $this->context->getViolations();
@@ -190,5 +207,137 @@ protected function assertViolations(array $expected)
190207
}
191208
}
192209

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

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

Lines 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