[Validator] fix tests

This commit is contained in:
Christian Flothmann 2020-08-14 12:50:32 +02:00
parent b109a29658
commit 162771f7e3
2 changed files with 43 additions and 35 deletions

View File

@ -23,6 +23,7 @@ use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Context\ExecutionContext;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
@ -115,7 +116,7 @@ abstract class ConstraintValidatorTestCase extends TestCase
$validator->expects($this->any())
->method('inContext')
->with($context)
->willReturn($this->getMockBuilder(AssertingContextualValidator::class)->setMethods(null)->getMock());
->willReturn($this->getMockBuilder(AssertingContextualValidator::class)->setConstructorArgs([$context])->setMethods(null)->getMock());
return $context;
}
@ -185,10 +186,25 @@ abstract class ConstraintValidatorTestCase extends TestCase
protected function expectValidateValue(int $i, $value, array $constraints = [], $group = null)
{
$contextualValidator = $this->context->getValidator()->inContext($this->context);
$contextualValidator->expects($this->at($i))
->method('validate')
->with($value, $constraints, $group)
->willReturn($contextualValidator);
$contextualValidator->expectValidation($i, '', $value, $group, function ($passedConstraints) use ($constraints) {
if (\is_array($constraints) && !\is_array($passedConstraints)) {
$passedConstraints = [$passedConstraints];
}
Assert::assertEquals($constraints, $passedConstraints);
});
}
protected function expectFailingValueValidation(int $i, $value, array $constraints, $group, ConstraintViolationInterface $violation)
{
$contextualValidator = $this->context->getValidator()->inContext($this->context);
$contextualValidator->expectValidation($i, '', $value, $group, function ($passedConstraints) use ($constraints) {
if (\is_array($constraints) && !\is_array($passedConstraints)) {
$passedConstraints = [$passedConstraints];
}
Assert::assertEquals($constraints, $passedConstraints);
}, $violation);
}
protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = null)
@ -371,12 +387,18 @@ class ConstraintViolationAssertion
class AssertingContextualValidator implements ContextualValidatorInterface
{
private $context;
private $expectNoValidate = false;
private $atPathCalls = -1;
private $expectedAtPath = [];
private $validateCalls = -1;
private $expectedValidate = [];
public function __construct(ExecutionContextInterface $context)
{
$this->context = $context;
}
public function atPath($path)
{
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');
@ -394,12 +416,20 @@ class AssertingContextualValidator implements ContextualValidatorInterface
{
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');
list($expectedValue, $expectedGroup, $expectedConstraints) = $this->expectedValidate[++$this->validateCalls];
if (!isset($this->expectedValidate[++$this->validateCalls])) {
return $this;
}
list($expectedValue, $expectedGroup, $expectedConstraints, $violation) = $this->expectedValidate[$this->validateCalls];
Assert::assertSame($expectedValue, $value);
$expectedConstraints($constraints);
Assert::assertSame($expectedGroup, $groups);
if (null !== $violation) {
$this->context->addViolation($violation->getMessage(), $violation->getParameters());
}
return $this;
}
@ -415,6 +445,7 @@ class AssertingContextualValidator implements ContextualValidatorInterface
public function getViolations()
{
return $this->context->getViolations();
}
public function expectNoValidate()
@ -422,9 +453,9 @@ class AssertingContextualValidator implements ContextualValidatorInterface
$this->expectNoValidate = true;
}
public function expectValidation($call, $propertyPath, $value, $group, $constraints)
public function expectValidation($call, $propertyPath, $value, $group, $constraints, ConstraintViolationInterface $violation = null)
{
$this->expectedAtPath[$call] = $propertyPath;
$this->expectedValidate[$call] = [$value, $group, $constraints];
$this->expectedValidate[$call] = [$value, $group, $constraints, $violation];
}
}

View File

@ -36,15 +36,8 @@ class SequentiallyValidatorTest extends ConstraintValidatorTestCase
$value = 6;
$contextualValidator = $this->context->getValidator()->inContext($this->context);
$contextualValidator->expects($this->any())->method('getViolations')->willReturn($this->context->getViolations());
$contextualValidator->expects($this->exactly(2))
->method('validate')
->withConsecutive(
[$value, $constraints[0]],
[$value, $constraints[1]]
)
->willReturn($contextualValidator);
$this->expectValidateValue(0, $value, [$constraints[0]]);
$this->expectValidateValue(1, $value, [$constraints[1]]);
$this->validator->validate($value, new Sequentially($constraints));
@ -61,24 +54,8 @@ class SequentiallyValidatorTest extends ConstraintValidatorTestCase
$value = 'Foo';
$contextualValidator = $this->context->getValidator()->inContext($this->context);
$contextualValidator->expects($this->any())->method('getViolations')->willReturn($this->context->getViolations());
$contextualValidator->expects($this->exactly(2))
->method('validate')
->withConsecutive(
[$value, $constraints[0]],
[$value, $constraints[1]]
)
->will($this->onConsecutiveCalls(
// Noop, just return the validator:
$this->returnValue($contextualValidator),
// Add violation on second call:
$this->returnCallback(function () use ($contextualValidator) {
$this->context->getViolations()->add($violation = new ConstraintViolation('regex error', null, [], null, '', null, null, 'regex'));
return $contextualValidator;
}
)));
$this->expectValidateValue(0, $value, [$constraints[0]]);
$this->expectFailingValueValidation(1, $value, [$constraints[1]], null, new ConstraintViolation('regex error', null, [], null, '', null, null, 'regex'));
$this->validator->validate($value, new Sequentially($constraints));