Skip to content

[Validator] allow consumers to mock all methods #38131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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