Skip to content

do not mock the validator in form type tests #11149

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
Mar 14, 2019
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
25 changes: 8 additions & 17 deletions form/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,31 +182,22 @@ allows you to return a list of extensions to register::
namespace Tests\AppBundle\Form\Type;

// ...
use AppBundle\Form\Type\TestedType;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Form;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\Validation;

class TestedTypeTest extends TypeTestCase
{
private $validator;

protected function getExtensions()
{
$this->validator = $this->createMock(ValidatorInterface::class);
// use getMock() on PHPUnit 5.3 or below
// $this->validator = $this->getMock(ValidatorInterface::class);
$this->validator
->method('validate')
->will($this->returnValue(new ConstraintViolationList()));
$this->validator
->method('getMetadataFor')
->will($this->returnValue(new ClassMetadata(Form::class)));
$validator = Validation::createValidator();
Copy link
Contributor

Choose a reason for hiding this comment

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

I would keep the current private property and do:

if (null === $this->validator) {
    $this->validator = Validation::createValidator();
    // ...
}

Otherwise the setUp will be heavy , so also maybe a comment or an implementation of tearDownAfterClass.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't really see for what you would need to access the validator. Previously this was just the case because you had to define in each tests individually which constraint violations should be raised.

Copy link
Contributor

Choose a reason for hiding this comment

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

The property would not be meant to access it anymore but to prevent creating the validator for running each test in a class, performances wise.
We just need to create it once now that we don't need to control the mock anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense but is it really worth it? Wouldn't it just make the code harder to understand compared to the provided benefit?

Copy link
Contributor

Choose a reason for hiding this comment

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

What about a PR to see the diff? You could advise to have that a logic in a trait. We could even provide such trait in core.


// or if you also need to read constraints from annotations
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();

return [
new ValidatorExtension($this->validator),
new ValidatorExtension($validator),
];
}

Expand Down