Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aac1013

Browse filesBrowse files
committed
[Validator][Tests] Fix AssertingContextualValidator not throwing on remaining expectations
1 parent 94007a5 commit aac1013
Copy full SHA for aac1013

File tree

Expand file treeCollapse file tree

2 files changed

+87
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+87
-1
lines changed

‎src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,17 @@ class AssertingContextualValidator implements ContextualValidatorInterface
391391
private $validateCalls = -1;
392392
private $expectedValidate = [];
393393

394+
public function __destruct()
395+
{
396+
if ($this->expectedAtPath) {
397+
throw new ExpectationFailedException('Some expected validation calls for paths were not done.');
398+
}
399+
400+
if ($this->expectedValidate) {
401+
throw new ExpectationFailedException('Some expected validation calls for values were not done.');
402+
}
403+
}
404+
394405
public function atPath($path)
395406
{
396407
}
@@ -403,7 +414,10 @@ public function doAtPath($path)
403414
throw new ExpectationFailedException(sprintf('Validation for property path "%s" was not expected.', $path));
404415
}
405416

406-
Assert::assertSame($this->expectedAtPath[$this->atPathCalls], $path);
417+
$expectedPath = $this->expectedAtPath[$this->atPathCalls];
418+
unset($this->expectedAtPath[$this->atPathCalls]);
419+
420+
Assert::assertSame($expectedPath, $path);
407421

408422
return $this;
409423
}
@@ -417,6 +431,7 @@ public function doValidate($value, $constraints = null, $groups = null)
417431
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');
418432

419433
[$expectedValue, $expectedGroup, $expectedConstraints] = $this->expectedValidate[++$this->validateCalls];
434+
unset($this->expectedValidate[$this->validateCalls]);
420435

421436
Assert::assertSame($expectedValue, $value);
422437
$expectedConstraints($constraints);
+71Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Test;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Constraints\DateTime;
17+
use Symfony\Component\Validator\Constraints\NotNull;
18+
use Symfony\Component\Validator\ConstraintValidator;
19+
use Symfony\Component\Validator\ConstraintValidatorInterface;
20+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
21+
22+
class ConstraintValidatorTestCaseTest extends ConstraintValidatorTestCase
23+
{
24+
protected function createValidator(): ConstraintValidatorInterface
25+
{
26+
return new TestCustomValidator();
27+
}
28+
29+
public function testAssertingContextualValidatorRemainingExpectationsThrow()
30+
{
31+
$this->expectValidateValueAt(0, 'k1', 'ccc', [
32+
new NotNull(),
33+
]);
34+
$this->expectValidateValueAt(1, 'k2', 'ccc', [
35+
new DateTime(),
36+
]);
37+
38+
$this->validator->validate('ccc', $this->constraint);
39+
40+
$contextualValidator = $this->context->getValidator()->inContext($this->context);
41+
// Simulate __destruct to assert it throws
42+
try {
43+
$contextualValidator->__destruct();
44+
$this->fail();
45+
} catch (ExpectationFailedException $e) {
46+
}
47+
48+
// Actually fulfill expectations so real __destruct doesn't throw
49+
$contextualValidator
50+
->atPath('k2')
51+
->validate('ccc', [
52+
new DateTime(),
53+
]);
54+
}
55+
}
56+
57+
class TestCustomValidator extends ConstraintValidator
58+
{
59+
public function validate($value, Constraint $constraint)
60+
{
61+
$validator = $this->context
62+
->getValidator()
63+
->inContext($this->context);
64+
65+
$validator
66+
->atPath('k1')
67+
->validate($value, [
68+
new NotNull(),
69+
]);
70+
}
71+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.