Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit8e5537b

Browse files
committed
[Validator] Simplified testing of violations
1 parentd671406 commit8e5537b

File tree

36 files changed

+516
-329
lines changed

36 files changed

+516
-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+
->atPath('property.path.name')
164+
->setInvalidValue('Foo')
165+
->assertRaised();
163166
}
164167

165168
publicfunctiontestValidateCustomErrorPath()
@@ -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
publicfunctiontestValidateUniquenessWithNull()
@@ -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
publicfunctiontestValidateUniquenessUsingCustomRepositoryMethod()
@@ -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
publicfunctiontestAssociatedEntityWithNull()

‎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,newForm());
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
publicfunctiontestAddInvalidErrorEvenIfNoValidationGroups()
@@ -251,10 +253,12 @@ function () { throw new TransformationFailedException(); }
251253

252254
$this->validator->validate($form,newForm());
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
publicfunctiontestDontValidateConstraintsIfNotSynchronized()
@@ -283,9 +287,11 @@ function () { throw new TransformationFailedException(); }
283287

284288
$this->validator->validate($form,newForm());
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,newForm());
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: 170 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
useSymfony\Component\Validator\ConstraintValidatorInterface;
1515
useSymfony\Component\Validator\ConstraintViolation;
16-
useSymfony\Component\Validator\Context\ExecutionContext;
17-
useSymfony\Component\Validator\Context\ExecutionContextInterface;
16+
useSymfony\Component\Validator\ExecutionContextInterface;
1817
useSymfony\Component\Validator\Mapping\ClassMetadata;
1918
useSymfony\Component\Validator\Mapping\PropertyMetadata;
2019
useSymfony\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
protectedfunctioncreateViolation($message,array$parameters =array(),$propertyPath ='property.path',$invalidValue ='InvalidValue',$plural =null,$code =null)
8496
{
8597
returnnewConstraintViolation(
@@ -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
protectedfunctionassertViolation($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
protectedfunctionassertViolations(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+
protectedfunctionbuildViolation($message)
231+
{
232+
returnnewConstraintViolationAssertion($this->context,$message);
233+
}
234+
193235
abstractprotectedfunctioncreateValidator();
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+
publicfunction__construct(ExecutionContextInterface$context,$message,array$assertions =array())
262+
{
263+
$this->context =$context;
264+
$this->message =$message;
265+
$this->assertions =$assertions;
266+
}
267+
268+
publicfunctionatPath($path)
269+
{
270+
$this->propertyPath =$path;
271+
272+
return$this;
273+
}
274+
275+
publicfunctionsetParameter($key,$value)
276+
{
277+
$this->parameters[$key] =$value;
278+
279+
return$this;
280+
}
281+
282+
publicfunctionsetParameters(array$parameters)
283+
{
284+
$this->parameters =$parameters;
285+
286+
return$this;
287+
}
288+
289+
publicfunctionsetTranslationDomain($translationDomain)
290+
{
291+
$this->translationDomain =$translationDomain;
292+
293+
return$this;
294+
}
295+
296+
publicfunctionsetInvalidValue($invalidValue)
297+
{
298+
$this->invalidValue =$invalidValue;
299+
300+
return$this;
301+
}
302+
303+
publicfunctionsetPlural($number)
304+
{
305+
$this->plural =$number;
306+
307+
return$this;
308+
}
309+
310+
publicfunctionsetCode($code)
311+
{
312+
$this->code =$code;
313+
314+
return$this;
315+
}
316+
317+
publicfunctionbuildNextViolation($message)
318+
{
319+
$assertions =$this->assertions;
320+
$assertions[] =$this;
321+
322+
returnnewself($this->context,$message,$assertions);
323+
}
324+
325+
publicfunctionassertRaised()
326+
{
327+
$expected =array();
328+
foreach ($this->assertionsas$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 ($expectedas$violation) {
340+
\PHPUnit_Framework_Assert::assertEquals($violation,current($violations));
341+
next($violations);
342+
}
343+
}
344+
345+
privatefunctiongetViolation()
346+
{
347+
returnnewConstraintViolation(
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‎

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
publicfunctiongetInvalidValues()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp