Skip to content

Commit a8da8b4

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: add validator translation 99 for Italian language stop using the deprecated at() PHPUnit matcher Fix typehint phpdoc
2 parents a3018d9 + 9c9568a commit a8da8b4

File tree

2 files changed

+82
-22
lines changed

2 files changed

+82
-22
lines changed

Resources/translations/validators.it.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@
382382
<source>Each element of this collection should satisfy its own set of constraints.</source>
383383
<target>Ciascun elemento di questa collezione dovrebbe soddisfare il suo insieme di vincoli.</target>
384384
</trans-unit>
385+
<trans-unit id="99">
386+
<source>This value is not a valid International Securities Identification Number (ISIN).</source>
387+
<target>Questo valore non è un codice identificativo internazionale di valori mobiliari (ISIN) valido.</target>
388+
</trans-unit>
385389
</body>
386390
</file>
387391
</xliff>

Test/ConstraintValidatorTestCase.php

Lines changed: 78 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@
1212
namespace Symfony\Component\Validator\Test;
1313

1414
use PHPUnit\Framework\Assert;
15+
use PHPUnit\Framework\Constraint\IsIdentical;
16+
use PHPUnit\Framework\Constraint\IsInstanceOf;
17+
use PHPUnit\Framework\Constraint\IsNull;
18+
use PHPUnit\Framework\Constraint\LogicalOr;
19+
use PHPUnit\Framework\ExpectationFailedException;
1520
use PHPUnit\Framework\TestCase;
1621
use Symfony\Component\Validator\Constraint;
1722
use Symfony\Component\Validator\Constraints\NotNull;
23+
use Symfony\Component\Validator\Constraints\Valid;
1824
use Symfony\Component\Validator\ConstraintValidatorInterface;
1925
use Symfony\Component\Validator\ConstraintViolation;
2026
use Symfony\Component\Validator\Context\ExecutionContext;
2127
use Symfony\Component\Validator\Context\ExecutionContextInterface;
2228
use Symfony\Component\Validator\Mapping\ClassMetadata;
2329
use Symfony\Component\Validator\Mapping\PropertyMetadata;
30+
use Symfony\Component\Validator\Validator\ContextualValidatorInterface;
2431
use Symfony\Contracts\Translation\TranslatorInterface;
2532

2633
/**
@@ -99,7 +106,6 @@ protected function createContext()
99106
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
100107
$translator->expects($this->any())->method('trans')->willReturnArgument(0);
101108
$validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock();
102-
$contextualValidator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ContextualValidatorInterface')->getMock();
103109

104110
$context = new ExecutionContext($validator, $this->root, $translator);
105111
$context->setGroup($this->group);
@@ -109,7 +115,7 @@ protected function createContext()
109115
$validator->expects($this->any())
110116
->method('inContext')
111117
->with($context)
112-
->willReturn($contextualValidator);
118+
->willReturn(new AssertingContextualValidator());
113119

114120
return $context;
115121
}
@@ -162,23 +168,18 @@ protected function setPropertyPath($propertyPath)
162168
protected function expectNoValidate()
163169
{
164170
$validator = $this->context->getValidator()->inContext($this->context);
165-
$validator->expects($this->never())
166-
->method('atPath');
167-
$validator->expects($this->never())
168-
->method('validate');
171+
$validator->expectNoValidate();
169172
}
170173

171174
protected function expectValidateAt($i, $propertyPath, $value, $group)
172175
{
173176
$validator = $this->context->getValidator()->inContext($this->context);
174-
$validator->expects($this->at(2 * $i))
175-
->method('atPath')
176-
->with($propertyPath)
177-
->willReturn($validator);
178-
$validator->expects($this->at(2 * $i + 1))
179-
->method('validate')
180-
->with($value, $this->logicalOr(null, [], $this->isInstanceOf('\Symfony\Component\Validator\Constraints\Valid')), $group)
181-
->willReturn($validator);
177+
$validator->expectValidation($i, $propertyPath, $value, $group, function ($passedConstraints) {
178+
$expectedConstraints = new LogicalOr();
179+
$expectedConstraints->setConstraints([new IsNull(), new IsIdentical([]), new IsInstanceOf(Valid::class)]);
180+
181+
Assert::assertThat($passedConstraints, $expectedConstraints);
182+
});
182183
}
183184

184185
protected function expectValidateValue(int $i, $value, array $constraints = [], $group = null)
@@ -193,14 +194,9 @@ protected function expectValidateValue(int $i, $value, array $constraints = [],
193194
protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = null)
194195
{
195196
$contextualValidator = $this->context->getValidator()->inContext($this->context);
196-
$contextualValidator->expects($this->at(2 * $i))
197-
->method('atPath')
198-
->with($propertyPath)
199-
->willReturn($contextualValidator);
200-
$contextualValidator->expects($this->at(2 * $i + 1))
201-
->method('validate')
202-
->with($value, $constraints, $group)
203-
->willReturn($contextualValidator);
197+
$contextualValidator->expectValidation($i, $propertyPath, $value, $group, function ($passedConstraints) use ($constraints) {
198+
Assert::assertEquals($constraints, $passedConstraints);
199+
});
204200
}
205201

206202
protected function expectViolationsAt($i, $value, Constraint $constraint)
@@ -372,3 +368,63 @@ private function getViolation(): ConstraintViolation
372368
);
373369
}
374370
}
371+
372+
class AssertingContextualValidator implements ContextualValidatorInterface
373+
{
374+
private $expectNoValidate = false;
375+
private $atPathCalls = -1;
376+
private $expectedAtPath = [];
377+
private $validateCalls = -1;
378+
private $expectedValidate = [];
379+
380+
public function atPath($path)
381+
{
382+
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');
383+
384+
if (!isset($this->expectedAtPath[++$this->atPathCalls])) {
385+
throw new ExpectationFailedException(sprintf('Validation for property path "%s" was not expected.', $path));
386+
}
387+
388+
Assert::assertSame($this->expectedAtPath[$this->atPathCalls], $path);
389+
390+
return $this;
391+
}
392+
393+
public function validate($value, $constraints = null, $groups = null)
394+
{
395+
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');
396+
397+
list($expectedValue, $expectedGroup, $expectedConstraints) = $this->expectedValidate[++$this->validateCalls];
398+
399+
Assert::assertSame($expectedValue, $value);
400+
$expectedConstraints($constraints);
401+
Assert::assertSame($expectedGroup, $groups);
402+
403+
return $this;
404+
}
405+
406+
public function validateProperty($object, $propertyName, $groups = null)
407+
{
408+
return $this;
409+
}
410+
411+
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
412+
{
413+
return $this;
414+
}
415+
416+
public function getViolations()
417+
{
418+
}
419+
420+
public function expectNoValidate()
421+
{
422+
$this->expectNoValidate = true;
423+
}
424+
425+
public function expectValidation($call, $propertyPath, $value, $group, $constraints)
426+
{
427+
$this->expectedAtPath[$call] = $propertyPath;
428+
$this->expectedValidate[$call] = [$value, $group, $constraints];
429+
}
430+
}

0 commit comments

Comments
 (0)