Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitb409cb2

Browse files
committed
[Validator][Tests] Fix AssertingContextualValidator not throwing on remaining expectations
1 parent94007a5 commitb409cb2

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

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

Lines 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+
publicfunction__destruct()
395+
{
396+
if ($this->expectedAtPath) {
397+
thrownewExpectationFailedException('Some expected validation calls for paths were not done.');
398+
}
399+
400+
if ($this->expectedValidate) {
401+
thrownewExpectationFailedException('Some expected validation calls for values were not done.');
402+
}
403+
}
404+
394405
publicfunctionatPath($path)
395406
{
396407
}
@@ -403,7 +414,10 @@ public function doAtPath($path)
403414
thrownewExpectationFailedException(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);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
namespaceSymfony\Component\Validator\Tests\Test;
13+
14+
usePHPUnit\Framework\ExpectationFailedException;
15+
useSymfony\Component\Validator\Constraint;
16+
useSymfony\Component\Validator\Constraints\DateTime;
17+
useSymfony\Component\Validator\Constraints\NotNull;
18+
useSymfony\Component\Validator\ConstraintValidator;
19+
useSymfony\Component\Validator\ConstraintValidatorInterface;
20+
useSymfony\Component\Validator\Test\ConstraintValidatorTestCase;
21+
22+
class ConstraintValidatorTestCaseTestextends ConstraintValidatorTestCase
23+
{
24+
protectedfunctioncreateValidator():ConstraintValidatorInterface
25+
{
26+
returnnewTestCustomValidator();
27+
}
28+
29+
publicfunctiontestAssertingContextualValidatorRemainingExpectationsThrow()
30+
{
31+
$this->expectValidateValueAt(0,'k1','ccc', [
32+
newNotNull(),
33+
]);
34+
$this->expectValidateValueAt(1,'k2','ccc', [
35+
newDateTime(),
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+
newDateTime(),
53+
]);
54+
}
55+
}
56+
57+
class TestCustomValidatorextends ConstraintValidator
58+
{
59+
publicfunctionvalidate($value,Constraint$constraint)
60+
{
61+
$validator =$this->context
62+
->getValidator()
63+
->inContext($this->context);
64+
65+
$validator
66+
->atPath('k1')
67+
->validate($value, [
68+
newNotNull(),
69+
]);
70+
71+
}
72+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp