-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Unit tests of custom validator #12676
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
Conversation
1576365
to
cb2f596
Compare
cb2f596
to
929d7a2
Compare
Any news on this PR? :) |
@@ -97,7 +97,7 @@ method is only set to ``false`` if a data transformer throws an exception:: | |||
|
|||
Don't test the validation: it is applied by a listener that is not | |||
active in the test case and it relies on validation configuration. | |||
Instead, unit test your custom constraints directly. | |||
Instead, :ref:`unit test your custom constraints directly<testing-data-providers>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, :ref:`unit test your custom constraints directly<testing-data-providers>`. | |
Instead, :ref:`unit test your custom constraints directly <testing-data-providers>`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for you effort @mcsky. I think we should keep this article as short and as to the point as possible. To achieve that I suggest that we take into account that we already have an article explaining how to write your own validator (see https://symfony.com/doc/current/validation/custom_constraint.html). Here we can then focus on the testing part and refer to the other article if the reader is interested in the validator code. So this chapter would mainly focus on showing how to use the ConstraintValidatorTestCase
class.
What do you think?
|
||
Depending on the way you installed your Symfony or Symfony Validator component | ||
the tests may not be downloaded. Use the ``--prefer-source`` option with | ||
Composer if this is the case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this note. You won't need the Symfony tests to write tests for your own constraints.
This article is intended for developers who create | ||
:doc:`custom constraint </validation/custom_constraint>`. If you are using | ||
the :doc:`built-in Symfony constraints </validation>` or the constraints | ||
provided by third-party bundles, you don't need to unit test them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this note. In other places where we talk about tests we also do not explain that you do not need to write tests for Symfony core functions.
Let's be honest, I totally forgot this PR! Sorry. I agree with you, the documentation should mainly speak about unit testing of the constraint, not the validator. But I think the user should be able to compare the constraint with his validator, to really understand how the values are passed to the validator, and what he needs to test. What do you think if I wrote a really simple (obvious) constraint, like |
I still think that inventing just another constraint is not really worth it. Talking about tests totally makes sense to me, but we could reuse the custom constraint example from https://symfony.com/doc/current/validation/custom_constraint.html for that. |
protected function setUp(): void | ||
{ | ||
parent::setUp(); // This is important | ||
$this->order = $this->prophesize(Order::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need another one dependency?
I just went straight to this PR to test my validator... I assume I'm not the only one that needs it but not many people are aware it's here. @mcsky what's the state of this? ^^ |
{ | ||
parent::setUp(); // This is important | ||
$this->order = $this->prophesize(Order::class); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tearDown is missing btw.
Currently, there isn't documentation to unit test custom validator.
The goal of this PR is to add one 👍 :)
As an example, I added a simple model
Order -> (got) Products
.Then there is a constraint with his test. The goal here is to show a way to test options and their types.
To finish there is the validator, also with his test. The validator's test uses the
ConstraintValidatorTestCase
to improves DX writing the tests.All feedbacks are very welcome. Especially about link to the newly created documentation page
#SymfonyHackday is amazing