1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\CanonicalUrlBundle\Tests\EventListener; |
4
|
|
|
|
5
|
|
|
use Palmtree\CanonicalUrlBundle\EventListener\KernelEventListener; |
6
|
|
|
use Palmtree\CanonicalUrlBundle\Service\CanonicalUrlGenerator; |
7
|
|
|
use Palmtree\CanonicalUrlBundle\Tests\AbstractTest; |
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
12
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
13
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
14
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
15
|
|
|
use Symfony\Component\HttpKernel\Tests\TestHttpKernel; |
16
|
|
|
|
17
|
|
|
class KernelEventListenerTest extends AbstractTest |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @dataProvider configProvider |
21
|
|
|
* @param array $config |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function testCanonicalRedirect(array $config) |
24
|
|
|
{ |
25
|
|
|
$event = new GetResponseEvent( |
26
|
|
|
new TestHttpKernel(), |
27
|
|
|
$this->getFooRequest(false), |
28
|
|
|
HttpKernelInterface::MASTER_REQUEST |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$response = new Response(); |
32
|
|
|
$event->setResponse($response); |
33
|
|
|
|
34
|
|
|
$listener = $this->getKernelEventListener($config); |
35
|
|
|
$listener->onKernelRequest($event); |
36
|
|
|
|
37
|
|
|
$this->assertNotSame($response, $event->getResponse()); |
38
|
|
|
$this->assertTrue($event->getResponse() instanceof RedirectResponse); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @dataProvider configProvider |
43
|
|
|
* @param array $config |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function testNoRedirectWhenUrlIsCanonical(array $config) |
46
|
|
|
{ |
47
|
|
|
$event = new GetResponseEvent( |
48
|
|
|
new TestHttpKernel(), |
49
|
|
|
$this->getFooRequest(true, false), |
50
|
|
|
HttpKernelInterface::MASTER_REQUEST |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$response = new Response(); |
54
|
|
|
$event->setResponse($response); |
55
|
|
|
|
56
|
|
|
$listener = $this->getKernelEventListener($config); |
57
|
|
|
$listener->onKernelRequest($event); |
58
|
|
|
|
59
|
|
|
$this->assertSame($response, $event->getResponse()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @dataProvider configProvider |
64
|
|
|
* @param array $config |
65
|
|
|
*/ |
66
|
|
|
public function testTrailingSlashRedirect(array $config) |
67
|
|
|
{ |
68
|
|
|
$event = new GetResponseForExceptionEvent( |
69
|
|
|
new TestHttpKernel(), |
70
|
|
|
$this->getFooRequest(), |
71
|
|
|
HttpKernelInterface::MASTER_REQUEST, |
72
|
|
|
new NotFoundHttpException('') |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$listener = $this->getKernelEventListener($config); |
76
|
|
|
|
77
|
|
|
$listener->onKernelException($event); |
78
|
|
|
|
79
|
|
|
/** @var RedirectResponse $response */ |
80
|
|
|
$response = $event->getResponse(); |
81
|
|
|
|
82
|
|
|
$this->assertTrue($response instanceof RedirectResponse); |
83
|
|
|
$this->assertEquals('https://example.org/foo', $response->getTargetUrl()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @dataProvider configProvider |
88
|
|
|
* @param array $config |
89
|
|
|
*/ |
90
|
|
|
public function testNoTrailingSlashRedirect(array $config) |
91
|
|
|
{ |
92
|
|
|
$config['trailing_slash'] = true; |
93
|
|
|
|
94
|
|
|
$event = new GetResponseForExceptionEvent( |
95
|
|
|
new TestHttpKernel(), |
96
|
|
|
$this->getBazRequest(true, false), |
97
|
|
|
HttpKernelInterface::MASTER_REQUEST, |
98
|
|
|
new NotFoundHttpException('') |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$listener = $this->getKernelEventListener($config); |
102
|
|
|
|
103
|
|
|
$listener->onKernelException($event); |
104
|
|
|
|
105
|
|
|
/** @var RedirectResponse $response */ |
106
|
|
|
$response = $event->getResponse(); |
107
|
|
|
|
108
|
|
|
$this->assertTrue($response instanceof RedirectResponse); |
109
|
|
|
$this->assertEquals('https://example.org/baz/', $response->getTargetUrl()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @dataProvider configProvider |
114
|
|
|
* @param array $config |
115
|
|
|
*/ |
116
|
|
|
public function testKernelRequestListenerDoesNothingWithEmptyRoute(array $config) |
117
|
|
|
{ |
118
|
|
|
$event = new GetResponseEvent( |
119
|
|
|
new TestHttpKernel(), |
120
|
|
|
new Request(), |
121
|
|
|
HttpKernelInterface::MASTER_REQUEST |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$listener = $this->getKernelEventListener($config); |
125
|
|
|
|
126
|
|
|
$returnValue = $listener->onKernelRequest($event); |
127
|
|
|
|
128
|
|
|
$this->assertFalse($returnValue); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @dataProvider configProvider |
133
|
|
|
* @param array $config |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function testNonMatchingAlternativeRouteReturnsFalse(array $config) |
136
|
|
|
{ |
137
|
|
|
$request = Request::create('https://example.org/bar/'); |
138
|
|
|
|
139
|
|
|
$event = new GetResponseForExceptionEvent( |
140
|
|
|
new TestHttpKernel(), |
141
|
|
|
$request, |
142
|
|
|
HttpKernelInterface::MASTER_REQUEST, |
143
|
|
|
new NotFoundHttpException('') |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$listener = $this->getKernelEventListener($config); |
147
|
|
|
|
148
|
|
|
$returnValue = $listener->onKernelException($event); |
149
|
|
|
|
150
|
|
|
$this->assertFalse($returnValue); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @dataProvider configProvider |
155
|
|
|
* @param array $config |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function testKernelRequestListenerDoesNothingForNonExistentRoute(array $config) |
158
|
|
|
{ |
159
|
|
|
$request = Request::create('https://example.org/bar'); |
160
|
|
|
|
161
|
|
|
$event = new GetResponseForExceptionEvent( |
162
|
|
|
new TestHttpKernel(), |
163
|
|
|
$request, |
164
|
|
|
HttpKernelInterface::MASTER_REQUEST, |
165
|
|
|
new NotFoundHttpException('') |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$listener = $this->getKernelEventListener($config); |
169
|
|
|
|
170
|
|
|
$returnValue = $listener->onKernelException($event); |
171
|
|
|
|
172
|
|
|
$this->assertFalse($returnValue); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @dataProvider configProvider |
177
|
|
|
* @param array $config |
178
|
|
|
*/ |
179
|
|
|
public function testRouteWithUrlParametersAndTrailingSlashRedirectsToCorrectRoute(array $config) |
180
|
|
|
{ |
181
|
|
|
$request = Request::create('https://example.org/foo/bar/'); |
182
|
|
|
$listener = $this->getKernelEventListener($config); |
183
|
|
|
|
184
|
|
|
$event = new GetResponseForExceptionEvent( |
185
|
|
|
new TestHttpKernel(), |
186
|
|
|
$request, |
187
|
|
|
HttpKernelInterface::MASTER_REQUEST, |
188
|
|
|
new NotFoundHttpException('') |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$listener->onKernelException($event); |
192
|
|
|
|
193
|
|
|
/** @var RedirectResponse $response */ |
194
|
|
|
$response = $event->getResponse(); |
195
|
|
|
|
196
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
197
|
|
|
$this->assertEquals('https://example.org/foo/bar', $response->getTargetUrl()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param array $config |
202
|
|
|
* @return KernelEventListener |
203
|
|
|
*/ |
204
|
|
|
protected function getKernelEventListener(array $config) |
205
|
|
|
{ |
206
|
|
|
$router = $this->getRouter(); |
207
|
|
|
|
208
|
|
|
$urlGenerator = new CanonicalUrlGenerator($router, $config); |
209
|
|
|
$listener = new KernelEventListener($router, $urlGenerator, $config); |
210
|
|
|
|
211
|
|
|
return $listener; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|