10000 [Validator] Simplified testing of violations · symfony/symfony@9bf2317 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bf2317

Browse files
committed
[Validator] Simplified testing of violations
1 parent d0537e0 commit 9bf2317

36 files changed

+478
-355
lines changed

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,47 +71,39 @@ protected function createRepositoryMock()
7171
{
7272
$repository = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectRepository')
7373
->setMethods(array('findByCustom', 'find', 'findAll', 'findOneBy', 'findBy', 'getClassName'))
74-
->getMock()
75-
;
74+
->getMock();
7675

7776
return $repository;
7877
}
7978

8079
protected function createEntityManagerMock($repositoryMock)
8180
{
8281
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
83-
->getMock()
84-
;
82+
->getMock();
8583
$em->expects($this->any())
8684
->method('getRepository')
87-
->will($this->returnValue($repositoryMock))
88-
;
85+
->will($this->returnValue($repositoryMock));
8986

9087
$classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
9188
$classMetadata
9289
->expects($this->any())
9390
->method('hasField')
94-
->will($this->returnValue(true))
95-
;
91+
->will($this->returnValue(true));
9692
$reflParser = $this->getMockBuilder('Doctrine\Common\Reflection\StaticReflectionParser')
9793
->disableOriginalConstructor()
98-
->getMock()
99-
;
94+
->getMock();
10095
$refl = $this->getMockBuilder('Doctrine\Common\Reflection\StaticReflectionProperty')
10196
->setConstructorArgs(array($reflParser, 'property-name'))
10297
->setMethods(array('getValue'))
103-
->getMock()
104-
;
98+
->getMock();
10599
$refl
106100
->expects($this->any())
107101
->method('getValue')
108-
->will($this->returnValue(true))
109-
;
102+
->will($this->returnValue(true));
110103
$classMetadata->reflFields = array('name' => $refl);
111104
$em->expects($this->any())
112105
->method('getClassMetadata')
113-
->will($this->returnValue($classMetadata))
114-
;
106+
->will($this->returnValue($classMetadata));
115107

116108
return $em;
117109
}
@@ -159,7 +151,10 @@ public function testValidateUniqueness()
159151

160152
$this->validator->validate($entity2, $constraint);
161153

162-
$this->assertViolation('myMessage', array(), 'property.path.name', 'Foo');
154+
$this->assertViolation('myMessage')
155+
->withPath('property.path.name')
156+
->withInvalidValue('Foo')
157+
->assertNow();
163158
}
164159

165160
public function testValidateCustomErrorPath()
@@ -179,7 +174,10 @@ public function testValidateCustomErrorPath()
179174

180175
$this->validator->validate($entity2, $constraint);
181176

182-
$this->assertViolation('myMessage', array(), 'property.path.bar', 'Foo');
177+
$this->assertViolation('myMessage')
178+
->withPath('property.path.bar')
179+
->withInvalidValue('Foo')
180+
->assertNow();
183181
}
184182

185183
public function testValidateUniquenessWithNull()
@@ -227,7 +225,10 @@ public function testValidateUniquenessWithIgnoreNull()
227225

228226
$this->validator->validate($entity2, $constraint);
229227

230-
$this->assertViolation('myMessage', array(), 'property.path.name', 'Foo');
228+
$this->assertViolation('myMessage')
229+
->withPath('property.path.name')
230+
->withInvalidValue('Foo')
231+
->assertNow();
231232
}
232233

233234
public function testValidateUniquenessUsingCustomRepositoryMethod()
@@ -242,8 +243,7 @@ public function testValidateUniquenessUsingCustomRepositoryMethod()
242243
$repository = $this->createRepositoryMock();
243244
$repository->expects($this->once())
244245
->method('findByCustom')
245-
->will($this->returnValue(array()))
246-
;
246+
->will($this->returnValue(array()));
247247
$this->em = $this->createEntityManagerMock($repository);
248248
$this->registry = $this->createRegistryMock($this->em);
249249
$this->validator = $this->createValidator();
@@ -279,8 +279,7 @@ public function testValidateUniquenessWithUnrewoundArray()
279279

280280
return $returnValue;
281281
})
282-
)
283-
;
282+
);
284283
$this->em = $this->createEntityManagerMock($repository);
285284
$this->registry = $this->createRegistryMock($this->em);
286285
$this->validator = $this->createValidator();
@@ -321,7 +320,10 @@ public function testAssociatedEntity()
321320

322321
$this->validator->validate($associated2, $constraint);
323322

324-
$this->assertViolation('myMessage', array(), 'property.path.single', 1);
323+
$this->assertViolation('myMessage')
324+
->withPath('property.path.single')
325+
->withInvalidValue(1)
326+
->assertNow();
325327
}
326328

327329
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->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

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

Lines 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

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->assertViolation('myMessage')
50+
->withParameter('{{ value }}', $valueAsString)
51+
->assertNow();
5352
}
5453

5554
public function getInvalidValues()

0 commit comments

Comments
 (0)
0