Skip to content

Commit 9c9568a

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

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
/**
@@ -101,7 +108,6 @@ protected function createContext()
101108
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
102109
$translator->expects($this->any())->method('trans')->willReturnArgument(0);
103110
$validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock();
104-
$contextualValidator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ContextualValidatorInterface')->getMock();
105111

106112
$context = new ExecutionContext($validator, $this->root, $translator);
107113
$context->setGroup($this->group);
@@ -111,7 +117,7 @@ protected function createContext()
111117
$validator->expects($this->any())
112118
->method('inContext')
113119
->with($context)
114-
->willReturn($contextualValidator);
120+
->willReturn(new AssertingContextualValidator());
115121

116122
return $context;
117123
}
@@ -164,36 +170,26 @@ protected function setPropertyPath($propertyPath)
164170
protected function expectNoValidate()
165171
{
166172
$validator = $this->context->getValidator()->inContext($this->context);
167-
$validator->expects($this->never())
168-
->method('atPath');
169-
$validator->expects($this->never())
170-
->method('validate');
173+
$validator->expectNoValidate();
171174
}
172175

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

186187
protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = null)
187188
{
188189
$contextualValidator = $this->context->getValidator()->inContext($this->context);
189-
$contextualValidator->expects($this->at(2 * $i))
190-
->method('atPath')
191-
->with($propertyPath)
192-
->willReturn($contextualValidator);
193-
$contextualValidator->expects($this->at(2 * $i + 1))
194-
->method('validate')
195-
->with($value, $constraints, $group)
196-
->willReturn($contextualValidator);
190+
$contextualValidator->expectValidation($i, $propertyPath, $value, $group, function ($passedConstraints) use ($constraints) {
191+
Assert::assertEquals($constraints, $passedConstraints);
192+
});
197193
}
198194

199195
protected function assertNoViolation()
@@ -346,3 +342,63 @@ private function getViolation(): ConstraintViolation
346342
);
347343
}
348344
}
345+
346+
class AssertingContextualValidator implements ContextualValidatorInterface
347+
{
348+
private $expectNoValidate = false;
349+
private $atPathCalls = -1;
350+
private $expectedAtPath = [];
351+
private $validateCalls = -1;
352+
private $expectedValidate = [];
353+
354+
public function atPath($path)
355+
{
356+
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');
357+
358+
if (!isset($this->expectedAtPath[++$this->atPathCalls])) {
359+
throw new ExpectationFailedException(sprintf('Validation for property path "%s" was not expected.', $path));
360+
}
361+
362+
Assert::assertSame($this->expectedAtPath[$this->atPathCalls], $path);
363+
364+
return $this;
365+
}
366+
367+
public function validate($value, $constraints = null, $groups = null)
368+
{
369+
Assert::assertFalse($this->expectNoValidate, 'No validation calls have been expected.');
370+
371+
list($expectedValue, $expectedGroup, $expectedConstraints) = $this->expectedValidate[++$this->validateCalls];
372+
373+
Assert::assertSame($expectedValue, $value);
374+
$expectedConstraints($constraints);
375+
Assert::assertSame($expectedGroup, $groups);
376+
377+
return $this;
378+
}
379+
380+
public function validateProperty($object, $propertyName, $groups = null)
381+
{
382+
return $this;
383+
}
384+
385+
public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
386+
{
387+
return $this;
388+
}
389+
390+
public function getViolations()
391+
{
392+
}
393+
394+
public function expectNoValidate()
395+
{
396+
$this->expectNoValidate = true;
397+
}
398+
399+
public function expectValidation($call, $propertyPath, $value, $group, $constraints)
400+
{
401+
$this->expectedAtPath[$call] = $propertyPath;
402+
$this->expectedValidate[$call] = [$value, $group, $constraints];
403+
}
404+
}

0 commit comments

Comments
 (0)