-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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
[Validator] Unit Tests #13898
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I see, the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
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>'], | ||
]; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.