Skip to content

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

Closed
wants to merge 1 commit into from
Closed

Conversation

mcsky
Copy link

@mcsky mcsky commented Nov 23, 2019

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

@mcsky mcsky force-pushed the unit-tests-validator branch from cb2f596 to 929d7a2 Compare November 23, 2019 14:24
@mcsky mcsky changed the title Add documentation: unit tests of custom validator Unit tests of custom validator Nov 23, 2019
@mcsky
Copy link
Author

mcsky commented Dec 13, 2019

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>`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Instead, :ref:`unit test your custom constraints directly<testing-data-providers>`.
Instead, :ref:`unit test your custom constraints directly <testing-data-providers>`.

Copy link
Member

@xabbuh xabbuh left a 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.
Copy link
Member

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.
Copy link
Member

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.

@mcsky
Copy link
Author

mcsky commented Mar 25, 2020

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 LessThan? And I remove the constraint and validator that I previously wrote.

@xabbuh
Copy link
Member

xabbuh commented Mar 27, 2020

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);
Copy link

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?

@Nek-
Copy link
Contributor

Nek- commented Jun 10, 2020

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);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

tearDown is missing btw.

@wouterj
Copy link
Member

wouterj commented Oct 20, 2020

Hi @mcsky! Thanks for creating this PR. It has already helped people while not being merged :)

Unfortunately, there is not much activity in this PR. As there is another PR about this topic in #13898, I'm going to close this one. Feel free to leave comments & a review on that PR.

@wouterj wouterj closed this Oct 20, 2020
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.

7 participants