Skip to content

Commit

Permalink
allow consumers to mock all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Sep 9, 2020
1 parent ff7f030 commit 51f0033
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion Test/ConstraintValidatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,44 @@ protected function createContext()
$context->setNode($this->value, $this->object, $this->metadata, $this->propertyPath);
$context->setConstraint($this->constraint);

$contextualValidator = $this->getMockBuilder(AssertingContextualValidator::class)
->setMethods([
'atPath',
'validate',
'validateProperty',
'validatePropertyValue',
'getViolations',
])
->getMock();
$contextualValidator->expects($this->any())
->method('atPath')
->willReturnCallback(function ($path) use ($contextualValidator) {
return $contextualValidator->doAtPath($path);
});
$contextualValidator->expects($this->any())
->method('validate')
->willReturnCallback(function ($value, $constraints = null, $groups = null) use ($contextualValidator) {
return $contextualValidator->doValidate($value, $constraints, $groups);
});
$contextualValidator->expects($this->any())
->method('validateProperty')
->willReturnCallback(function ($object, $propertyName, $groups = null) use ($contextualValidator) {
return $contextualValidator->validateProperty($object, $propertyName, $groups);
});
$contextualValidator->expects($this->any())
->method('validatePropertyValue')
->willReturnCallback(function ($objectOrClass, $propertyName, $value, $groups = null) use ($contextualValidator) {
return $contextualValidator->doValidatePropertyValue($objectOrClass, $propertyName, $value, $groups);
});
$contextualValidator->expects($this->any())
->method('getViolations')
->willReturnCallback(function () use ($contextualValidator) {
return $contextualValidator->doGetViolations();
});
$validator->expects($this->any())
->method('inContext')
->with($context)
->willReturn($this->getMockBuilder(AssertingContextualValidator::class)->setMethods(null)->getMock());
->willReturn($contextualValidator);

return $context;
}
Expand Down Expand Up @@ -353,6 +387,10 @@ class AssertingContextualValidator implements ContextualValidatorInterface
private $expectedValidate = [];

public function atPath($path)
{
}

public function doAtPath($path)
{
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');

Expand All @@ -366,6 +404,10 @@ public function atPath($path)
}

public function validate($value, $constraints = null, $groups = null)
{
}

public function doValidate($value, $constraints = null, $groups = null)
{
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');

Expand All @@ -379,11 +421,19 @@ public function validate($value, $constraints = null, $groups = null)
}

public function validateProperty($object, $propertyName, $groups = null)
{
}

public function doValidateProperty($object, $propertyName, $groups = null)
{
return $this;
}

public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
{
}

public function doValidatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
{
return $this;
}
Expand All @@ -392,6 +442,10 @@ public function getViolations()
{
}

public function doGetViolations()
{
}

public function expectNoValidate()
{
$this->expectNoValidate = true;
Expand Down

0 comments on commit 51f0033

Please sign in to comment.