Skip to content

[Validator] Unit Tests #13898

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

Closed
wants to merge 6 commits into from
Closed
Changes from 3 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
70 changes: 70 additions & 0 deletions validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,73 @@ not to the property:
$metadata->addConstraint(new ProtocolClass());
}
}

How to Unit Test your Validator
-------------------------------

To create a unit test for you custom validator, you can use ``ConstraintValidatorTestCase`` class. All you need is to extend
from it and implement the ``createValidator()`` method. This method must return an instance of your custom constraint validator class::

protected function createValidator()
{
return new ContainsAlphanumericValidator();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using the ProtocolClassValidator here that we implemented above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I see, the ProtocolClassValidator is more like 'sketchy' snippet, no real logic in there. Ergo I am not sure that we can produce some significant unit test example for it. Maybe we should improve the ProtocolClassValidator snippet? @xabbuh

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work for me too. As long as we use a consistent example throughout this document the experience when reading the article will be better IMO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@henry2778 it would be very great if you could use (and enhance) the ProtocolClassValidator to stay consistent across the document. Thanks

}

After that you can add any test cases you need to cover the validation logic::

use AppBundle\Validator\Constraints\ContainsAlphanumeric;
use AppBundle\Validator\Constraints\ContainsAlphanumericValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

class ContainsAlphanumericValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
{
return new ContainsAlphanumericValidator();
}

/**
* @dataProvider getValidStrings
*/
public function testValidStrings($string)
{
$this->validator->validate($string, new ContainsAlphanumeric());

$this->assertNoViolation();
}

public function getValidStrings()
{
return [
['Fabien'],
['SymfonyIsGreat'],
['HelloWorld123'],
];
}

/**
* @dataProvider getInvalidStrings
*/
public function testInvalidStrings($string)
{
$constraint = new ContainsAlphanumeric([
'message' => 'myMessage',
]);

$this->validator->validate($string, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ string }}', $string)
->assertRaised();
}

public function getInvalidStrings()
{
return [
['example_'],
['@$^&'],
['hello-world'],
['<body>'],
];
}
}