1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\CanonicalUrlBundle\Tests\EventListener; |
4
|
|
|
|
5
|
|
|
use Palmtree\CanonicalUrlBundle\EventListener\ExceptionListener; |
6
|
|
|
use Palmtree\CanonicalUrlBundle\Tests\AbstractTest; |
7
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
11
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
12
|
|
|
use Symfony\Component\HttpKernel\Tests\TestHttpKernel; |
13
|
|
|
|
14
|
|
|
class ExceptionListenerTest extends AbstractTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @dataProvider configProvider |
18
|
|
|
* |
19
|
|
|
* @param array $config |
20
|
|
|
*/ |
21
|
|
|
public function testTrailingSlashRedirect(array $config) |
22
|
|
|
{ |
23
|
|
|
$request = $this->getFooRequest(); |
24
|
|
|
$event = $this->getGetResponseForExceptionEvent($request); |
25
|
|
|
|
26
|
|
|
$listener = $this->getListener($config); |
27
|
|
|
|
28
|
|
|
$listener->onKernelException($event); |
29
|
|
|
|
30
|
|
|
/** @var RedirectResponse $response */ |
31
|
|
|
$response = $event->getResponse(); |
32
|
|
|
|
33
|
|
|
$this->assertTrue($response instanceof RedirectResponse); |
34
|
|
|
$this->assertEquals('https://example.org/foo', $response->getTargetUrl()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @dataProvider configProvider |
39
|
|
|
* |
40
|
|
|
* @param array $config |
41
|
|
|
*/ |
42
|
|
|
public function testNoTrailingSlashRedirect(array $config) |
43
|
|
|
{ |
44
|
|
|
$config['trailing_slash'] = true; |
45
|
|
|
|
46
|
|
|
$request = $this->getBazRequest(true, false); |
47
|
|
|
|
48
|
|
|
$event = $this->getGetResponseForExceptionEvent($request); |
49
|
|
|
|
50
|
|
|
$listener = $this->getListener($config); |
51
|
|
|
|
52
|
|
|
$listener->onKernelException($event); |
53
|
|
|
|
54
|
|
|
/** @var RedirectResponse $response */ |
55
|
|
|
$response = $event->getResponse(); |
56
|
|
|
|
57
|
|
|
$this->assertTrue($response instanceof RedirectResponse); |
58
|
|
|
$this->assertEquals('https://example.org/baz/', $response->getTargetUrl()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @dataProvider configProvider |
63
|
|
|
* |
64
|
|
|
* @param array $config |
65
|
|
|
*/ |
66
|
|
|
public function testNonMatchingAlternativeRouteReturnsFalse(array $config) |
67
|
|
|
{ |
68
|
|
|
$request = Request::create('https://example.org/bar/'); |
69
|
|
|
$event = $this->getGetResponseForExceptionEvent($request); |
70
|
|
|
|
71
|
|
|
$listener = $this->getListener($config); |
72
|
|
|
|
73
|
|
|
$returnValue = $listener->onKernelException($event); |
74
|
|
|
|
75
|
|
|
$this->assertFalse($returnValue); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @dataProvider configProvider |
80
|
|
|
* |
81
|
|
|
* @param array $config |
82
|
|
|
*/ |
83
|
|
|
public function testKernelRequestListenerDoesNothingForNonExistentRoute(array $config) |
84
|
|
|
{ |
85
|
|
|
$request = Request::create('https://example.org/bar'); |
86
|
|
|
$event = $this->getGetResponseForExceptionEvent($request); |
87
|
|
|
|
88
|
|
|
$listener = $this->getListener($config); |
89
|
|
|
|
90
|
|
|
$returnValue = $listener->onKernelException($event); |
91
|
|
|
|
92
|
|
|
$this->assertFalse($returnValue); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @dataProvider configProvider |
97
|
|
|
* |
98
|
|
|
* @param array $config |
99
|
|
|
*/ |
100
|
|
|
public function testRouteWithUrlParametersAndTrailingSlashRedirectsToCorrectRoute(array $config) |
101
|
|
|
{ |
102
|
|
|
$request = Request::create('https://example.org/foo/bar/'); |
103
|
|
|
$event = $this->getGetResponseForExceptionEvent($request); |
104
|
|
|
|
105
|
|
|
$listener = $this->getListener($config); |
106
|
|
|
|
107
|
|
|
$listener->onKernelException($event); |
108
|
|
|
|
109
|
|
|
/** @var RedirectResponse $response */ |
110
|
|
|
$response = $event->getResponse(); |
111
|
|
|
|
112
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
113
|
|
|
$this->assertEquals('https://example.org/foo/bar', $response->getTargetUrl()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Request $request |
118
|
|
|
* |
119
|
|
|
* @return GetResponseForExceptionEvent |
120
|
|
|
*/ |
121
|
|
|
protected function getGetResponseForExceptionEvent(Request $request) |
122
|
|
|
{ |
123
|
|
|
$event = new GetResponseForExceptionEvent( |
124
|
|
|
new TestHttpKernel(), |
125
|
|
|
$request, |
126
|
|
|
HttpKernelInterface::MASTER_REQUEST, |
127
|
|
|
new NotFoundHttpException('') |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return $event; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array $config |
135
|
|
|
* |
136
|
|
|
* @return ExceptionListener |
137
|
|
|
*/ |
138
|
|
|
protected function getListener(array $config) |
139
|
|
|
{ |
140
|
|
|
$router = $this->getRouter(); |
141
|
|
|
$listener = new ExceptionListener($router, $config); |
142
|
|
|
|
143
|
|
|
return $listener; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|