1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* |
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\Tests\Twig; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
15
|
|
|
use Yarhon\RouteGuardBundle\Twig\RoutingRuntime; |
16
|
|
|
use Yarhon\RouteGuardBundle\Routing\AuthorizedUrlGeneratorInterface; |
17
|
|
|
use Yarhon\RouteGuardBundle\Exception\InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class RoutingRuntimeTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
private $generator; |
25
|
|
|
|
26
|
|
|
private $runtime; |
27
|
|
|
|
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->generator = $this->createMock(AuthorizedUrlGeneratorInterface::class); |
31
|
|
|
|
32
|
|
|
$this->runtime = new RoutingRuntime($this->generator); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @dataProvider urlDataProvider |
37
|
|
|
*/ |
38
|
|
|
public function testUrl($arguments, $expectedReferenceType) |
39
|
|
|
{ |
40
|
|
|
$expectedArguments = array_slice($arguments, 0, 3); |
41
|
|
|
$expectedArguments += [1 => [], 2 => 'GET', 3 => $expectedReferenceType]; |
42
|
|
|
|
43
|
|
|
$this->generator->expects($this->once()) |
44
|
|
|
->method('generate') |
45
|
|
|
->with(...$expectedArguments) |
46
|
|
|
->willReturn('/url1'); |
47
|
|
|
|
48
|
|
|
$this->assertEquals('/url1', $this->runtime->url(...$arguments)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function urlDataProvider() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
[ |
55
|
|
|
['route1', ['page' => 1], 'POST'], |
56
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL, |
57
|
|
|
], |
58
|
|
|
|
59
|
|
|
[ |
60
|
|
|
['route1', ['page' => 1], 'POST', true], |
61
|
|
|
UrlGeneratorInterface::NETWORK_PATH, |
62
|
|
|
], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @dataProvider pathDataProvider |
68
|
|
|
*/ |
69
|
|
|
public function testPath($arguments, $expectedReferenceType) |
70
|
|
|
{ |
71
|
|
|
$expectedArguments = array_slice($arguments, 0, 3); |
72
|
|
|
$expectedArguments += [1 => [], 2 => 'GET', 3 => $expectedReferenceType]; |
73
|
|
|
|
74
|
|
|
$this->generator->expects($this->once()) |
75
|
|
|
->method('generate') |
76
|
|
|
->with(...$expectedArguments) |
77
|
|
|
->willReturn('/url1'); |
78
|
|
|
|
79
|
|
|
$this->assertEquals('/url1', $this->runtime->path(...$arguments)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function pathDataProvider() |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
[ |
86
|
|
|
['route1', ['page' => 1], 'POST'], |
87
|
|
|
UrlGeneratorInterface::ABSOLUTE_PATH, |
88
|
|
|
], |
89
|
|
|
|
90
|
|
|
[ |
91
|
|
|
['route1', ['page' => 1], 'POST', true], |
92
|
|
|
UrlGeneratorInterface::RELATIVE_PATH, |
93
|
|
|
], |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @dataProvider routeDataProvider |
99
|
|
|
*/ |
100
|
|
|
public function testRoute($arguments, $expectedReferenceType) |
101
|
|
|
{ |
102
|
|
|
$expectedArguments = array_slice($arguments, 0, 3); |
103
|
|
|
$expectedArguments += [1 => [], 2 => 'GET', 3 => $expectedReferenceType]; |
104
|
|
|
|
105
|
|
|
$this->generator->expects($this->once()) |
106
|
|
|
->method('generate') |
107
|
|
|
->with(...$expectedArguments) |
108
|
|
|
->willReturn('/url1'); |
109
|
|
|
|
110
|
|
|
$this->assertEquals('/url1', $this->runtime->route(...$arguments)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function routeDataProvider() |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
|
|
[ |
117
|
|
|
['route1', ['page' => 1], 'POST'], |
118
|
|
|
UrlGeneratorInterface::ABSOLUTE_PATH, |
119
|
|
|
], |
120
|
|
|
|
121
|
|
|
[ |
122
|
|
|
['route1', ['page' => 1], 'POST', ['path']], |
123
|
|
|
UrlGeneratorInterface::ABSOLUTE_PATH, |
124
|
|
|
], |
125
|
|
|
|
126
|
|
|
[ |
127
|
|
|
['route1', ['page' => 1], 'POST', ['path', false]], |
128
|
|
|
UrlGeneratorInterface::ABSOLUTE_PATH, |
129
|
|
|
], |
130
|
|
|
|
131
|
|
|
[ |
132
|
|
|
['route1', ['page' => 1], 'POST', ['path', true]], |
133
|
|
|
UrlGeneratorInterface::RELATIVE_PATH, |
134
|
|
|
], |
135
|
|
|
|
136
|
|
|
[ |
137
|
|
|
['route1', ['page' => 1], 'POST', ['url']], |
138
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL, |
139
|
|
|
], |
140
|
|
|
|
141
|
|
|
[ |
142
|
|
|
['route1', ['page' => 1], 'POST', ['url', false]], |
143
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL, |
144
|
|
|
], |
145
|
|
|
|
146
|
|
|
[ |
147
|
|
|
['route1', ['page' => 1], 'POST', ['url', true]], |
148
|
|
|
UrlGeneratorInterface::NETWORK_PATH, |
149
|
|
|
], |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testRouteException() |
154
|
|
|
{ |
155
|
|
|
$this->expectException(InvalidArgumentException::class); |
156
|
|
|
$this->expectExceptionMessage('Invalid reference type: "qwerty"'); |
157
|
|
|
|
158
|
|
|
$this->runtime->route('route1', ['page' => 1], 'POST', ['qwerty']); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|