-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Simplified testing of violations #12015
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
7264f61
to
4d59ccf
Compare
use Symfony\Component\Validator\Constraints\Collection; | ||
use Symfony\Component\Validator\Constraints\NotNull; | ||
use Symfony\Component\Validator\Constraints\Range; | ||
use Symfony\Component\Validator\Constraints\Choice; |
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 avoid moving use statements around. It does not change anything and make merging harder.
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.
Ah, this happened automatically. Thanks.
IMO, instead of building a class with an |
3b9356b
to
53b5033
Compare
@stof However, let's give better names to |
53b5033
to
9bf2317
Compare
@@ -71,8 +71,7 @@ protected function createRepositoryMock() | |||
{ | |||
$repository = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectRepository') | |||
->setMethods(array('findByCustom', 'find', 'findAll', 'findOneBy', 'findBy', 'getClassName')) | |||
->getMock() | |||
; | |||
->getMock(); |
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.
Sorry about that, but that's a wrong change coming from fabbot/PHP-CS-Fixer. That should be reverted.
What about: $this->withViolation('invalid_message_key')
->assertParameter('{{ value }}', 'foo')
->assertParameter('{{ foo }}', 'bar')
->assertInvalidValue('foo')
->assertCode(Form::ERR_INVALID)
->done(); |
1203dca
to
d90ef9c
Compare
d90ef9c
to
8e5537b
Compare
I changed it to $this->buildViolation('invalid_message_key')
->setParameter('{{ value }}', 'foo')
->setParameter('{{ foo }}', 'bar')
->setInvalidValue('foo')
->setCode(Form::ERR_INVALID)
->assertRaised(); This way the API is equivalent to the violation builder API. Also, "raise" is the proper verb to go with violations. |
The fabbot issues are due to a bug in fabbot. Unless Travis fails, this is good to merge from my side. ping @symfony/deciders |
👍 |
1 similar comment
👍 |
Thank you @webmozart. |
This PR was merged into the 2.3 branch. Discussion ---------- [Validator] Simplified testing of violations | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - I simplified the assertion of violations in preparation of a replacement PR for #7276. Commits ------- 8e5537b [Validator] Simplified testing of violations
…multiple error causes (webmozart) This PR was merged into the 2.6-dev branch. Discussion ---------- [Validator] Added error codes to all constraints with multiple error causes | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7276 | License | MIT | Doc PR | TODO This PR depends on #12015 and #12016 being merged first. However, a few changes in 52cb7df first must be backported to #12016. This PR introduces error codes for all constraints with multiple error paths. This lets you determine what exactly the reason was that a constraint failed: ```php $validator = Validation::createValidator(); $violations = $validator->validate('0-4X19-92619812', new Isbn()); foreach ($violations as $violation) { var_dump($violation->getCode()); // => int(3) var_dump(Isbn::getErrorName($violation->getCode())); // => string(24) "ERROR_INVALID_CHARACTERS" var_dump($violation->getConstraint()->getErrorName($violation->getCode())); // => string(24) "ERROR_INVALID_CHARACTERS" } ``` The `getErrorName()` method is especially helpful for REST APIs, where you can return both an error code and a description of that error now. **Todos** - [x] Backport a few structural changes to #12016 - [x] Update constraints outside of the Validator component - [x] Rebase on master (after merging #12015 and #12016) Commits ------- 3b50bf2 [Validator] Added error codes to all constraints with multiple error causes
I simplified the assertion of violations in preparation of a replacement PR for #7276.