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\Cache\DataCollector; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Symfony\Component\Routing\RouteCollection; |
16
|
|
|
use Symfony\Component\Routing\Route; |
17
|
|
|
use Yarhon\RouteGuardBundle\Controller\ControllerMetadata; |
18
|
|
|
use Yarhon\RouteGuardBundle\Controller\ControllerNameResolverInterface; |
19
|
|
|
use Yarhon\RouteGuardBundle\Routing\RouteMetadata; |
20
|
|
|
use Yarhon\RouteGuardBundle\Security\Test\AbstractTestBagInterface; |
21
|
|
|
use Yarhon\RouteGuardBundle\Exception\InvalidArgumentException; |
22
|
|
|
use Yarhon\RouteGuardBundle\Cache\DataCollector\RouteDataCollector; |
23
|
|
|
use Yarhon\RouteGuardBundle\Cache\DataCollector\RouteCollectionDataCollector; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class RouteCollectionDataCollectorTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
private $routeDataCollector; |
31
|
|
|
|
32
|
|
|
private $controllerNameResolver; |
33
|
|
|
|
34
|
|
|
private $logger; |
35
|
|
|
|
36
|
|
|
public function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->routeDataCollector = $this->createMock(RouteDataCollector::class); |
39
|
|
|
$this->controllerNameResolver = $this->createMock(ControllerNameResolverInterface::class); |
40
|
|
|
$this->logger = $this->createMock(LoggerInterface::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testCollect() |
44
|
|
|
{ |
45
|
|
|
$collector = $this->createCollector(); |
46
|
|
|
|
47
|
|
|
$this->controllerNameResolver->method('resolve') |
48
|
|
|
->willReturnArgument(0); |
49
|
|
|
|
50
|
|
|
$routeData = $this->createRouteData(); |
51
|
|
|
|
52
|
|
|
$this->routeDataCollector->method('collect') |
53
|
|
|
->willReturn($routeData); |
54
|
|
|
|
55
|
|
|
$routeCollection = $this->createRouteCollection([ |
56
|
|
|
'/path1' => 'class::method', |
57
|
|
|
'/path2' => 'class::method2', |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$data = $collector->collect($routeCollection); |
61
|
|
|
|
62
|
|
|
$expected = [ |
63
|
|
|
'/path1' => $routeData, |
64
|
|
|
'/path2' => $routeData, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->assertSame($expected, $data); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testCollectWithControllerNameResolverException() |
71
|
|
|
{ |
72
|
|
|
$collector = $this->createCollector(); |
73
|
|
|
|
74
|
|
|
$this->controllerNameResolver->method('resolve') |
75
|
|
|
->willThrowException(new InvalidArgumentException('Inner exception.')); |
76
|
|
|
|
77
|
|
|
$routeCollection = $this->createRouteCollection([ |
78
|
|
|
'/path1' => 'class::method', |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$this->expectException(InvalidArgumentException::class); |
82
|
|
|
$this->expectExceptionMessage('Route "/path1": Inner exception.'); |
83
|
|
|
|
84
|
|
|
$collector->collect($routeCollection); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testCollectWithRouteDataCollectorException() |
88
|
|
|
{ |
89
|
|
|
$collector = $this->createCollector(); |
90
|
|
|
|
91
|
|
|
$this->controllerNameResolver->method('resolve') |
92
|
|
|
->willReturnArgument(0); |
93
|
|
|
|
94
|
|
|
$this->routeDataCollector->method('collect') |
95
|
|
|
->willThrowException(new InvalidArgumentException('Inner exception.')); |
96
|
|
|
|
97
|
|
|
$routeCollection = $this->createRouteCollection([ |
98
|
|
|
'/path1' => 'class::method', |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$this->expectException(InvalidArgumentException::class); |
102
|
|
|
$this->expectExceptionMessage('Route "/path1": Inner exception.'); |
103
|
|
|
|
104
|
|
|
$collector->collect($routeCollection); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testCollectWithControllerNameResolverExceptionCaught() |
108
|
|
|
{ |
109
|
|
|
$collector = $this->createCollector(['ignore_exceptions' => true]); |
110
|
|
|
$collector->setLogger($this->logger); |
111
|
|
|
|
112
|
|
|
$this->controllerNameResolver->method('resolve') |
113
|
|
|
->will($this->onConsecutiveCalls( |
114
|
|
|
$this->throwException(new InvalidArgumentException('Inner exception.')), |
115
|
|
|
$this->returnArgument(0) |
116
|
|
|
)); |
117
|
|
|
|
118
|
|
|
$this->logger->expects($this->once()) |
119
|
|
|
->method('error') |
120
|
|
|
->with('Route "/path1" would be ignored because of exception caught: Inner exception.'); |
121
|
|
|
|
122
|
|
|
$routeData = $this->createRouteData(); |
123
|
|
|
|
124
|
|
|
$this->routeDataCollector->method('collect') |
125
|
|
|
->with('/path2') |
126
|
|
|
->willReturn($routeData); |
127
|
|
|
|
128
|
|
|
$routeCollection = $this->createRouteCollection([ |
129
|
|
|
'/path1' => 'class::method', |
130
|
|
|
'/path2' => 'class::method2', |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
$data = $collector->collect($routeCollection); |
134
|
|
|
|
135
|
|
|
$expected = [ |
136
|
|
|
'/path2' => $routeData, |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$this->assertSame($expected, $data); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testCollectWithRouteDataCollectorExceptionCaught() |
143
|
|
|
{ |
144
|
|
|
$collector = $this->createCollector(['ignore_exceptions' => true]); |
145
|
|
|
$collector->setLogger($this->logger); |
146
|
|
|
|
147
|
|
|
$this->controllerNameResolver->method('resolve') |
148
|
|
|
->willReturnArgument(0); |
149
|
|
|
|
150
|
|
|
$routeData = $this->createRouteData(); |
151
|
|
|
|
152
|
|
|
$this->routeDataCollector->method('collect') |
153
|
|
|
->will($this->onConsecutiveCalls( |
154
|
|
|
$this->throwException(new InvalidArgumentException('Inner exception.')), |
155
|
|
|
$this->returnValue($routeData) |
156
|
|
|
)); |
157
|
|
|
|
158
|
|
|
$this->logger->expects($this->once()) |
159
|
|
|
->method('error') |
160
|
|
|
->with('Route "/path1" would be ignored because of exception caught: Inner exception.'); |
161
|
|
|
|
162
|
|
|
$routeCollection = $this->createRouteCollection([ |
163
|
|
|
'/path1' => 'class::method', |
164
|
|
|
'/path2' => 'class::method2', |
165
|
|
|
]); |
166
|
|
|
|
167
|
|
|
$data = $collector->collect($routeCollection); |
168
|
|
|
|
169
|
|
|
$expected = [ |
170
|
|
|
'/path2' => $routeData, |
171
|
|
|
]; |
172
|
|
|
|
173
|
|
|
$this->assertSame($expected, $data); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testIgnoredControllers() |
177
|
|
|
{ |
178
|
|
|
$ignoredControllers = [ |
179
|
|
|
'class1', |
180
|
|
|
'class2::method2', |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
$collector = $this->createCollector(['ignore_controllers' => $ignoredControllers]); |
184
|
|
|
|
185
|
|
|
$this->controllerNameResolver->method('resolve') |
186
|
|
|
->willReturnArgument(0); |
187
|
|
|
|
188
|
|
|
$routeData = $this->createRouteData(); |
189
|
|
|
|
190
|
|
|
$this->routeDataCollector->method('collect') |
191
|
|
|
->willReturn($routeData); |
192
|
|
|
|
193
|
|
|
$routeCollection = $this->createRouteCollection([ |
194
|
|
|
'/path1' => 'class1::method', |
195
|
|
|
'/path2' => 'class2::method1', |
196
|
|
|
'/path3' => 'class2::method2', |
197
|
|
|
]); |
198
|
|
|
|
199
|
|
|
$data = $collector->collect($routeCollection); |
200
|
|
|
|
201
|
|
|
$expected = [ |
202
|
|
|
'/path2' => $routeData, |
203
|
|
|
]; |
204
|
|
|
|
205
|
|
|
$this->assertSame($expected, $data); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
private function createRouteCollection($routes = []) |
209
|
|
|
{ |
210
|
|
|
$routeCollection = new RouteCollection(); |
211
|
|
|
|
212
|
|
|
foreach ($routes as $path => $controller) { |
213
|
|
|
$route = new Route($path, ['_controller' => $controller]); |
214
|
|
|
$routeCollection->add($path, $route); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $routeCollection; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
private function createCollector(array $options = []) |
221
|
|
|
{ |
222
|
|
|
return new RouteCollectionDataCollector($this->routeDataCollector, $this->controllerNameResolver, $options); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
private function createRouteData() |
226
|
|
|
{ |
227
|
|
|
$testBags = [$this->createMock(AbstractTestBagInterface::class)]; |
228
|
|
|
|
229
|
|
|
return [$testBags, $this->createMock(ControllerMetadata::class), $this->createMock(RouteMetadata::class)]; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|