-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #42458 [Validator][Tests] Fix AssertingContextualValidator not th…
…rowing on remaining expectations (fancyweb) This PR was merged into the 4.4 branch. Discussion ---------- [Validator][Tests] Fix AssertingContextualValidator not throwing on remaining expectations | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - While working on something I noticed two missing things in the `AssertingContextualValidator`. 1. We don't check if there are remaining expected calls when the `AssertingContextualValidator` is destroyed. Therefore the tests using it always pass, whether the validator actually calls `validate` or not. (for example: comment the `->validate()` line in `AllValidator` and tests still pass) ~2. When the expected value / value is an object, we should use `assertEquals` because it cannot logically be the same instance.~ Ping `@xabbuh` Commits ------- aac1013 [Validator][Tests] Fix AssertingContextualValidator not throwing on remaining expectations
- Loading branch information
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Symfony/Component/Validator/Tests/Test/ConstraintValidatorTestCaseTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Test; | ||
|
||
use PHPUnit\Framework\ExpectationFailedException; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints\DateTime; | ||
use Symfony\Component\Validator\Constraints\NotNull; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\ConstraintValidatorInterface; | ||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | ||
|
||
class ConstraintValidatorTestCaseTest extends ConstraintValidatorTestCase | ||
{ | ||
protected function createValidator(): ConstraintValidatorInterface | ||
{ | ||
return new TestCustomValidator(); | ||
} | ||
|
||
public function testAssertingContextualValidatorRemainingExpectationsThrow() | ||
{ | ||
$this->expectValidateValueAt(0, 'k1', 'ccc', [ | ||
new NotNull(), | ||
]); | ||
$this->expectValidateValueAt(1, 'k2', 'ccc', [ | ||
new DateTime(), | ||
]); | ||
|
||
$this->validator->validate('ccc', $this->constraint); | ||
|
||
$contextualValidator = $this->context->getValidator()->inContext($this->context); | ||
// Simulate __destruct to assert it throws | ||
try { | ||
$contextualValidator->__destruct(); | ||
$this->fail(); | ||
} catch (ExpectationFailedException $e) { | ||
} | ||
|
||
// Actually fulfill expectations so real __destruct doesn't throw | ||
$contextualValidator | ||
->atPath('k2') | ||
->validate('ccc', [ | ||
new DateTime(), | ||
]); | ||
} | ||
} | ||
|
||
class TestCustomValidator extends ConstraintValidator | ||
{ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
$validator = $this->context | ||
->getValidator() | ||
->inContext($this->context); | ||
|
||
$validator | ||
->atPath('k1') | ||
->validate($value, [ | ||
new NotNull(), | ||
]); | ||
} | ||
} |