Completed
Push — master ( 8c6023...dead72 )
by Andy
02:16
created

KernelEventListenerTest::getGetResponseEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
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
        $request = $this->getFooRequest(false);
26
        $event   = $this->getGetResponseEvent($request);
27
28
        $response = new Response();
29
        $event->setResponse($response);
30
31
        $listener = $this->getKernelEventListener($config);
32
        $listener->onKernelRequest($event);
33
34
        $this->assertNotSame($response, $event->getResponse());
35
        $this->assertTrue($event->getResponse() instanceof RedirectResponse);
36
    }
37
38
    /**
39
     * @dataProvider configProvider
40
     * @param array $config
41
     */
42 View Code Duplication
    public function testNoRedirectWhenUrlIsCanonical(array $config)
43
    {
44
        $request = $this->getFooRequest(true, false);
45
        $event   = $this->getGetResponseEvent($request);
46
47
        $response = new Response();
48
        $event->setResponse($response);
49
50
        $listener = $this->getKernelEventListener($config);
51
        $listener->onKernelRequest($event);
52
53
        $this->assertSame($response, $event->getResponse());
54
    }
55
56
    /**
57
     * @dataProvider configProvider
58
     * @param array $config
59
     */
60
    public function testKernelRequestListenerDoesNothingWithEmptyRoute(array $config)
61
    {
62
        $event = $this->getGetResponseEvent(new Request());
63
64
        $listener = $this->getKernelEventListener($config);
65
66
        $returnValue = $listener->onKernelRequest($event);
67
68
        $this->assertFalse($returnValue);
69
    }
70
71
    /**
72
     * @dataProvider configProvider
73
     * @param array $config
74
     */
75 View Code Duplication
    public function testTrailingSlashRedirect(array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $request = $this->getFooRequest();
78
        $event   = $this->getGetResponseForExceptionEvent($request);
79
80
        $listener = $this->getKernelEventListener($config);
81
82
        $listener->onKernelException($event);
83
84
        /** @var RedirectResponse $response */
85
        $response = $event->getResponse();
86
87
        $this->assertTrue($response instanceof RedirectResponse);
88
        $this->assertEquals('https://example.org/foo', $response->getTargetUrl());
89
    }
90
91
    /**
92
     * @dataProvider configProvider
93
     * @param array $config
94
     */
95
    public function testNoTrailingSlashRedirect(array $config)
96
    {
97
        $config['trailing_slash'] = true;
98
99
        $request = $this->getBazRequest(true, false);
100
101
        $event = $this->getGetResponseForExceptionEvent($request);
102
103
        $listener = $this->getKernelEventListener($config);
104
105
        $listener->onKernelException($event);
106
107
        /** @var RedirectResponse $response */
108
        $response = $event->getResponse();
109
110
        $this->assertTrue($response instanceof RedirectResponse);
111
        $this->assertEquals('https://example.org/baz/', $response->getTargetUrl());
112
    }
113
114
    /**
115
     * @dataProvider configProvider
116
     * @param array $config
117
     */
118 View Code Duplication
    public function testNonMatchingAlternativeRouteReturnsFalse(array $config)
119
    {
120
        $request = Request::create('https://example.org/bar/');
121
        $event   = $this->getGetResponseForExceptionEvent($request);
122
123
        $listener = $this->getKernelEventListener($config);
124
125
        $returnValue = $listener->onKernelException($event);
126
127
        $this->assertFalse($returnValue);
128
    }
129
130
    /**
131
     * @dataProvider configProvider
132
     * @param array $config
133
     */
134 View Code Duplication
    public function testKernelRequestListenerDoesNothingForNonExistentRoute(array $config)
135
    {
136
        $request = Request::create('https://example.org/bar');
137
        $event   = $this->getGetResponseForExceptionEvent($request);
138
139
        $listener = $this->getKernelEventListener($config);
140
141
        $returnValue = $listener->onKernelException($event);
142
143
        $this->assertFalse($returnValue);
144
    }
145
146
    /**
147
     * @dataProvider configProvider
148
     * @param array $config
149
     */
150 View Code Duplication
    public function testRouteWithUrlParametersAndTrailingSlashRedirectsToCorrectRoute(array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $request = Request::create('https://example.org/foo/bar/');
153
        $event   = $this->getGetResponseForExceptionEvent($request);
154
155
        $listener = $this->getKernelEventListener($config);
156
157
        $listener->onKernelException($event);
158
159
        /** @var RedirectResponse $response */
160
        $response = $event->getResponse();
161
162
        $this->assertInstanceOf(RedirectResponse::class, $response);
163
        $this->assertEquals('https://example.org/foo/bar', $response->getTargetUrl());
164
    }
165
166
    /**
167
     * @param Request $request
168
     * @return GetResponseForExceptionEvent
169
     */
170
    protected function getGetResponseForExceptionEvent(Request $request)
171
    {
172
        $event = new GetResponseForExceptionEvent(
173
            new TestHttpKernel(),
174
            $request,
175
            HttpKernelInterface::MASTER_REQUEST,
176
            new NotFoundHttpException('')
177
        );
178
179
        return $event;
180
    }
181
182
    /**
183
     * @param Request $request
184
     * @return GetResponseEvent
185
     */
186
    protected function getGetResponseEvent(Request $request)
187
    {
188
        $event = new GetResponseEvent(
189
            new TestHttpKernel(),
190
            $request,
191
            HttpKernelInterface::MASTER_REQUEST
192
        );
193
194
        return $event;
195
    }
196
197
    /**
198
     * @param array $config
199
     * @return KernelEventListener
200
     */
201
    protected function getKernelEventListener(array $config)
202
    {
203
        $router = $this->getRouter();
204
205
        $urlGenerator = new CanonicalUrlGenerator($router, $config);
206
        $listener     = new KernelEventListener($router, $urlGenerator, $config);
207
208
        return $listener;
209
    }
210
}
211