Skip to content
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] Constraint validators now use the 2.5 API #11485

Merged
merged 5 commits into from
Aug 4, 2014

Conversation

webmozart
Copy link
Contributor

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

See the comments in #11049 for the origin of this PR.

Currently, the 2.5 API needs to use LegacyExecutionContextFactory because the constraint validators rely on methods from the old ExecutionContext class (like validate(), validateValue()). Consequently it is impossible to switch to the pure 2.5 API and check whether all calls to deprecated methods were removed from application code. This is fixed now.

This PR also introduces a complete test suite to test each constraint validator against all three APIs: 2.4, 2.5-BC and 2.5. Currently, some tests are not executed yet when running the complete test suite is run. I expect this to be fixed soon (ticket: sebastianbergmann/phpunit#529, pr: sebastianbergmann/phpunit#1327).

@@ -41,11 +41,10 @@ public function validate($value, Constraint $constraint)

$context = $this->context;
$group = $context->getGroup();
$validator = $context->getValidator()->inContext($context);
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this is correct at the moment. But it looks kinda strange from an API point of view.
Would it not make more sense if $context->getValidator() automatically returns a ContextualValidatorInterface?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because you don't always want to validate in the same context. For example, imagine validating some constraints and, depending on the result, adding one single combined violation. Then you need to validate in a different context.

This was in fact one of the motivations for the API change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #9888 for example

@webmozart
Copy link
Contributor Author

Ping @fabpot, @stof, @Tobion: A review would be great :)

@fabpot
Copy link
Member

fabpot commented Aug 4, 2014

👍

@Tobion
Copy link
Contributor

Tobion commented Aug 4, 2014

I think the duplicated validation code for some new and legacy validators is not good. The validation code is the critical part. So it might make more sense to put that in an internal abstract class and then just change the implementation of adding a violation in the concrete class, i.e. addViolationAt vs. buildViolation.
But apart from that the PR looks good.

@webmozart
Copy link
Contributor Author

@Tobion I was thinking about that, but then again I think this over-complicates the code (lots of new abstraction and classes). I think that the code - although duplicated - will be easy to maintain for two reasons:

  • the tests are not duplicated, so whenever we change/add a test, the tests fail for both implementations;
  • the validators are very stable and rarely change.

For these reasons I would prefer to keep the duplicated code until we really need to combine it into a more complex abstraction.

@Tobion
Copy link
Contributor

Tobion commented Aug 4, 2014

OK 👍

@webmozart webmozart merged commit 295e5bb into symfony:2.5 Aug 4, 2014
webmozart added a commit that referenced this pull request Aug 4, 2014
…ebmozart)

This PR was squashed before being merged into the 2.5 branch (closes #11485).

Discussion
----------

[Validator] Constraint validators now use the 2.5 API

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

See the comments in #11049 for the origin of this PR.

Currently, the 2.5 API needs to use `LegacyExecutionContextFactory` because the constraint validators rely on methods from the old `ExecutionContext` class (like `validate()`, `validateValue()`). Consequently it is impossible to switch to the pure 2.5 API and check whether all calls to deprecated methods were removed from application code. This is fixed now.

This PR also introduces a complete test suite to test each constraint validator against all three APIs: 2.4, 2.5-BC and 2.5. Currently, some tests are not executed yet when running the complete test suite is run. I expect this to be fixed soon (ticket: sebastianbergmann/phpunit#529, pr: sebastianbergmann/phpunit#1327).

Commits
-------

295e5bb [Validator] Fixed failing tests
3bd6d80 [Validator] CS fixes
870a41a [FrameworkBundle] Made ConstraintValidatorFactory aware of the legacy validators
7504448 [Validator] Added extensive test coverage for the constraint validators for the different APIs
8e461af [Validator] Constraint validators now use the 2.5 API. For incompatible validators, legacy validators were created
@webmozart webmozart deleted the fix-validator-legacy-dependency branch August 4, 2014 14:42
@jakzal
Copy link
Contributor

jakzal commented Aug 4, 2014

@webmozart tests are terribly broken:

PHP Fatal error:  Call to undefined method Symfony\Component\Validator\Context\ExecutionContext::expects() in /home/travis/build/symfony/symfony/src/Symfony/Component/Validator/Tests/Constraints/ExpressionValidatorTest.php on line 161

jakzal added a commit to jakzal/symfony that referenced this pull request Aug 4, 2014
Both tests were introduced in symfony#11498, but symfony#11485 changed the approach to testing the validator and did not include these tests.
@jakzal jakzal mentioned this pull request Aug 4, 2014
@jakzal
Copy link
Contributor

jakzal commented Aug 4, 2014

see #11562

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.

4 participants