10000 Merge branch '5.1' · jeremyFreeAgent/symfony@c23166c · GitHub
[go: up one dir, main page]

Skip to content

Commit c23166c

Browse files
committed
Merge branch '5.1'
* 5.1: [Validator] fix tests
2 parents c8566e0 + 56bf1bc commit c23166c

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\Validator\Constraints\Valid;
2424
use Symfony\Component\Validator\ConstraintValidatorInterface;
2525
use Symfony\Component\Validator\ConstraintViolation;
26+
use Symfony\Component\Validator\ConstraintViolationInterface;
2627
use Symfony\Component\Validator\Context\ExecutionContext;
2728
use Symfony\Component\Validator\Context\ExecutionContextInterface;
2829
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -115,7 +116,7 @@ protected function createContext()
115116
$validator->expects($this->any())
116117
->method('inContext')
117118
->with($context)
118-
->willReturn($this->getMockBuilder(AssertingContextualValidator::class)->setMethods(null)->getMock());
119+
->willReturn($this->getMockBuilder(AssertingContextualValidator::class)->setConstructorArgs([$context])->setMethods(null)->getMock());
119120

120121
return $context;
121122
}
@@ -185,10 +186,25 @@ protected function expectValidateAt($i, $propertyPath, $value, $group)
185186
protected function expectValidateValue(int $i, $value, array $constraints = [], $group = null)
186187
{
187188
$contextualValidator = $this->context->getValidator()->inContext($this->context);
188-
$contextualValidator->expects($this->at($i))
189-
->method('validate')
190-
->with($value, $constraints, $group)
191-
->willReturn($contextualValidator);
189+
$contextualValidator->expectValidation($i, '', $value, $group, function ($passedConstraints) use ($constraints) {
190+
if (\is_array($constraints) && !\is_array($passedConstraints)) {
191+
$passedConstraints = [$passedConstraints];
192+
}
193+
194+
Assert::assertEquals($constraints, $passedConstraints);
195+
});
196+
}
197+
198+
protected function expectFailingValueValidation(int $i, $value, array $constraints, $group, ConstraintViolationInterface $violation)
199+
{
200+
$contextualValidator = $this->context->getValidator()->inContext($this->context);
201+
$contextualValidator->expectValidation($i, '', $value, $group, function ($passedConstraints) use ($constraints) {
202+
if (\is_array($constraints) && !\is_array($passedConstraints)) {
203+
$passedConstraints = [$passedConstraints];
204+
}
205+
206+
Assert::assertEquals($constraints, $passedConstraints);
207+
}, $violation);
192208
}
193209

194210
protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = null)
@@ -371,12 +387,18 @@ private function getViolation(): ConstraintViolation
371387

372388
class AssertingContextualValidator implements ContextualValidatorInterface
373389
{
390+
private $context;
374391
private $expectNoValidate = false;
375392
private $atPathCalls = -1;
376393
private $expectedAtPath = [];
377394
private $validateCalls = -1;
378395
private $expectedValidate = [];
379396

397+
public function __construct(ExecutionContextInterface $context)
398+
{
399+
$this->context = $context;
400+
}
401+
380402
public function atPath($path)
381403
{
382404
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');
@@ -394,12 +416,20 @@ public function validate($value, $constraints = null, $groups = null)
394416
{
395417
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');
396418

397-
list($expectedValue, $expectedGroup, $expectedConstraints) = $this->expectedValidate[++$this->validateCalls];
419+
if (!isset($this->expectedValidate[++$this->validateCalls])) {
420+
return $this;
421+
}
422+
423+
list($expectedValue, $expectedGroup, $expectedConstraints, $violation) = $this->expectedValidate[$this->validateCalls];
398424

399425
Assert::assertSame($expectedValue, $value);
400426
$expectedConstraints($constraints);
401427
Assert::assertSame($expectedGroup, $groups);
402428

429+
if (null !== $violation) {
430+
$this->context->addViolation($violation->getMessage(), $violation->getParameters());
431+
}
432+
403433
return $this;
404434
}
405435

@@ -415,16 +445,17 @@ public function validatePropertyValue($objectOrClass, $propertyName, $value, $gr
415445

416446
public function getViolations()
417447
{
448+
return $this->context->getViolations();
418449
}
419450

420451
public function expectNoValidate()
421452
{
422453
$this->expectNoValidate = true;
423454
}
424455

425-
public function expectValidation($call, $propertyPath, $value, $group, $constraints)
456+
public function expectValidation($call, $propertyPath, $value, $group, $constraints, ConstraintViolationInterface $violation = null)
426457
{
427458
$this->expectedAtPath[$call] = $propertyPath;
428-
$this->expectedValidate[$call] = [$value, $group, $constraints];
459+
$this->expectedValidate[$call] = [$value, $group, $constraints, $violation];
429460
}
430461
}

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,8 @@ public function testWalkThroughConstraints()
3636

3737
$value = 6;
3838

39-
$contextualValidator = $this->context->getValidator()->inContext($this->context);
40-
$contextualValidator->expects($this->any())->method('getViolations')->willReturn($this->context->getViolations());
41-
$contextualValidator->expects($this->exactly(2))
42-
->method('validate')
43-
->withConsecutive(
44-
[$value, $constraints[0]],
45-
[$value, $constraints[1]]
46-
)
47-
->willReturn($contextualValidator);
39+
$this->expectValidateValue(0, $value, [$constraints[0]]);
40+
$this->expectValidateValue(1, $value, [$constraints[1]]);
4841

4942
$this->validator->validate($value, new Sequentially($constraints));
5043

@@ -61,24 +54,8 @@ public function testStopsAtFirstConstraintWithViolations()
6154

6255
$value = 'Foo';
6356

64-
$contextualValidator = $this->context->getValidator()->inContext($this->context);
65-
$contextualValidator->expects($this->any())->method('getViolations')->willReturn($this->context->getViolations());
66-
$contextualValidator->expects($this->exactly(2))
67-
->method('validate')
68-
->withConsecutive(
69-
[$value, $constraints[0]],
70-
[$value, $constraints[1]]
71-
)
72-
->will($this->onConsecutiveCalls(
73-
// Noop, just return the validator:
74-
$this->returnValue($contextualValidator),
75-
// Add violation on second call:
76-
$this->returnCallback(function () use ($contextualValidator) {
77-
$this->context->getViolations()->add($violation = new ConstraintViolation('regex error', null, [], null, '', null, null, 'regex'));
78-
79-
return $contextualValidator;
80-
}
81-
)));
57+
$this->expectValidateValue(0, $value, [$constraints[0]]);
58+
$this->expectFailingValueValidation(1, $value, [$constraints[1]], null, new ConstraintViolation('regex error', null, [], null, '', null, null, 'regex'));
8259

8360
$this->validator->validate($value, new Sequentially($constraints));
8461

0 commit comments

Comments
 (0)
0