Skip to content

Commit 973dd67

Browse files
committed
stop using the deprecated at() PHPUnit matcher
1 parent 77a38e0 commit 973dd67

File tree

1 file changed

+78
-22
lines changed

1 file changed

+78
-22
lines changed

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

2532
/**
2633
* A test case to ease testing Constraint Validators.
@@ -99,7 +106,6 @@ protected function createContext()
99106
{
100107
$translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock();
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,36 +168,26 @@ 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 expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = null)
185186
{
186187
$contextualValidator = $this->context->getValidator()->inContext($this->context);
187-
$contextualValidator->expects($this->at(2 * $i))
188-
->method('atPath')
189-
->with($propertyPath)
190-
->willReturn($contextualValidator);
191-
$contextualValidator->expects($this->at(2 * $i + 1))
192-
->method('validate')
193-
->with($value, $constraints, $group)
194-
->willReturn($contextualValidator);
188+
$contextualValidator->expectValidation($i, $propertyPath, $value, $group, function ($passedConstraints) use ($constraints) {
189+
Assert::assertEquals($constraints, $passedConstraints);
190+
});
195191
}
196192

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

0 commit comments

Comments
 (0)