testKernelRequestListenerDoesNothingWithEmptyRoute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Palmtree\CanonicalUrlBundle\Tests\EventListener;
4
5
use Palmtree\CanonicalUrlBundle\EventListener\RequestListener;
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\HttpKernelInterface;
13
use Symfony\Component\HttpKernel\Tests\TestHttpKernel;
14
15
class RequestListenerTest extends AbstractTest
16
{
17
    /**
18
     * @dataProvider configProvider
19
     *
20
     * @param array $config
21
     */
22
    public function testCanonicalRedirect(array $config)
23
    {
24
        $request = $this->getFooRequest(false);
25
        $event   = $this->getGetResponseEvent($request);
26
27
        $response = new Response();
28
        $event->setResponse($response);
29
30
        $listener = $this->getListener($config);
31
        $listener->onKernelRequest($event);
32
33
        $this->assertNotSame($response, $event->getResponse());
34
        $this->assertInstanceOf(RedirectResponse::class, $event->getResponse());
35
    }
36
37
    /**
38
     * @dataProvider configProvider
39
     *
40
     * @param array $config
41
     */
42
    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->getListener($config);
51
        $listener->onKernelRequest($event);
52
53
        $this->assertSame($response, $event->getResponse());
54
    }
55
56
    /**
57
     * @dataProvider configProvider
58
     *
59
     * @param array $config
60
     */
61
    public function testKernelRequestListenerDoesNothingWithEmptyRoute(array $config)
62
    {
63
        $event = $this->getGetResponseEvent(new Request());
64
65
        $listener = $this->getListener($config);
66
67
        $returnValue = $listener->onKernelRequest($event);
68
69
        $this->assertFalse($returnValue);
70
    }
71
72
    /**
73
     * @param Request $request
74
     *
75
     * @return GetResponseEvent
76
     */
77
    protected function getGetResponseEvent(Request $request)
78
    {
79
        $event = new GetResponseEvent(
80
            new TestHttpKernel(),
81
            $request,
82
            HttpKernelInterface::MASTER_REQUEST
83
        );
84
85
        return $event;
86
    }
87
88
    /**
89
     * @param array $config
90
     *
91
     * @return RequestListener
92
     */
93
    protected function getListener(array $config)
94
    {
95
        $router = $this->getRouter();
96
97
        $urlGenerator = new CanonicalUrlGenerator($router, $config);
98
        $listener     = new RequestListener($urlGenerator, $config);
99
100
        return $listener;
101
    }
102
}
103