Passed
Push — master ( fff9a6...600230 )
by Andy
02:12
created

testNoTrailingSlashRedirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 12
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)
1 ignored issue
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...
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)
1 ignored issue
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...
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)
1 ignored issue
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...
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)
1 ignored issue
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...
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
     * @param array $config
177
     * @return KernelEventListener
178
     */
179
    protected function getKernelEventListener(array $config)
180
    {
181
        $router = $this->getRouter();
182
183
        $urlGenerator = new CanonicalUrlGenerator($router, $config);
184
        $listener     = new KernelEventListener($router, $urlGenerator, $config);
185
186
        return $listener;
187
    }
188
}
189