1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Beelab\SimplePageBundle\Tests\Validator\Constraints; |
4
|
|
|
|
5
|
|
|
use Beelab\SimplePageBundle\Validator\Constraints\NoExistingRoute; |
6
|
|
|
use Beelab\SimplePageBundle\Validator\Constraints\NoExistingRouteValidator; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\Routing\Route; |
9
|
|
|
use Symfony\Component\Routing\RouterInterface; |
10
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
11
|
|
|
|
12
|
|
|
final class NoExistingRouteValidatorTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
protected $router; |
15
|
|
|
protected $context; |
16
|
|
|
|
17
|
|
|
protected function setUp(): void |
18
|
|
|
{ |
19
|
|
|
$this->router = $this->createMock(RouterInterface::class); |
20
|
|
|
$this->context = $this->createMock(ExecutionContextInterface::class); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testInvalid(): void |
24
|
|
|
{ |
25
|
|
|
$validator = new NoExistingRouteValidator($this->router); |
|
|
|
|
26
|
|
|
$validator->initialize($this->context); |
|
|
|
|
27
|
|
|
$routes = ['foo_bar' => new Route('/foobar')]; |
28
|
|
|
$constraint = new NoExistingRoute(); |
29
|
|
|
|
30
|
|
|
$this->router->expects($this->once())->method('getRouteCollection')->willReturn($routes); |
31
|
|
|
$this->context->expects($this->atLeastOnce())->method('addViolation'); |
32
|
|
|
|
33
|
|
|
$validator->validate('foobar', $constraint); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testInvalidWithoutRoute(): void |
37
|
|
|
{ |
38
|
|
|
$validator = new NoExistingRouteValidator($this->router, false); |
|
|
|
|
39
|
|
|
$validator->initialize($this->context); |
|
|
|
|
40
|
|
|
$routes = ['bar_foo' => new Route('/barfoo')]; |
41
|
|
|
$constraint = new NoExistingRoute(); |
42
|
|
|
|
43
|
|
|
$this->router->expects($this->once())->method('getRouteCollection')->willReturn($routes); |
44
|
|
|
$this->context->expects($this->atLeastOnce())->method('addViolation'); |
45
|
|
|
|
46
|
|
|
$validator->validate('barfoo', $constraint); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testValid(): void |
50
|
|
|
{ |
51
|
|
|
$validator = new NoExistingRouteValidator($this->router, false); |
|
|
|
|
52
|
|
|
$validator->initialize($this->context); |
|
|
|
|
53
|
|
|
$routes = ['foo_baz' => new Route('/foobaz')]; |
54
|
|
|
$constraint = new NoExistingRoute(); |
55
|
|
|
|
56
|
|
|
$this->router->expects($this->once())->method('getRouteCollection')->willReturn($routes); |
57
|
|
|
$this->context->expects($this->never())->method('addViolation'); |
58
|
|
|
|
59
|
|
|
$validator->validate('bazbaz', $constraint); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: