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

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Mar 13, 2019

No description provided.

@xabbuh xabbuh added this to the 3.4 milestone Mar 13, 2019
@xabbuh xabbuh requested a review from HeahDude March 13, 2019 16:18
@xabbuh
Copy link
Member Author

xabbuh commented Mar 13, 2019

Thanks to @clavat who made me aware of this overly complicated section that was not so helpful IMO.

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

return [
new ValidatorExtension($this->validator),
Copy link
Contributor

@kunicmarko20 kunicmarko20 Mar 13, 2019

Choose a reason for hiding this comment

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

Suggested change
new ValidatorExtension($this->validator),
new ValidatorExtension($validator),

@xabbuh xabbuh force-pushed the form-test-validator-extension branch from 575f8fb to 272e479 Compare March 13, 2019 17:54
Copy link
Contributor

@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

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

I left a comment otherwise this change looks good to me.

$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.

Copy link
Contributor

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

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

Nice simplification

@javiereguiluz
Copy link
Member

A lovely improvement! Thanks Christian and @clavat

@javiereguiluz javiereguiluz merged commit 272e479 into symfony:3.4 Mar 14, 2019
javiereguiluz added a commit that referenced this pull request Mar 14, 2019
This PR was merged into the 3.4 branch.

Discussion
----------

do not mock the validator in form type tests

Commits
-------

272e479 do not mock the validator in form type tests
@xabbuh xabbuh deleted the form-test-validator-extension branch March 14, 2019 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants