Completed
Push — master ( f33806...7a283c )
by Andy
02:14
created

testKernelRequestListenerDoesNothingWithEmptyRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

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 5
nc 1
nop 1
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
     * @param array $config
20
     */
21 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...
22
    {
23
        $request = $this->getFooRequest(false);
24
        $event   = $this->getGetResponseEvent($request);
25
26
        $response = new Response();
27
        $event->setResponse($response);
28
29
        $listener = $this->getListener($config);
30
        $listener->onKernelRequest($event);
31
32
        $this->assertNotSame($response, $event->getResponse());
33
        $this->assertInstanceOf(RedirectResponse::class, $event->getResponse());
34
    }
35
36
    /**
37
     * @dataProvider configProvider
38
     * @param array $config
39
     */
40 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...
41
    {
42
        $request = $this->getFooRequest(true, false);
43
        $event   = $this->getGetResponseEvent($request);
44
45
        $response = new Response();
46
        $event->setResponse($response);
47
48
        $listener = $this->getListener($config);
49
        $listener->onKernelRequest($event);
50
51
        $this->assertSame($response, $event->getResponse());
52
    }
53
54
    /**
55
     * @dataProvider configProvider
56
     * @param array $config
57
     */
58
    public function testKernelRequestListenerDoesNothingWithEmptyRoute(array $config)
59
    {
60
        $event = $this->getGetResponseEvent(new Request());
61
62
        $listener = $this->getListener($config);
63
64
        $returnValue = $listener->onKernelRequest($event);
65
66
        $this->assertFalse($returnValue);
67
    }
68
69
    /**
70
     * @param Request $request
71
     * @return GetResponseEvent
72
     */
73
    protected function getGetResponseEvent(Request $request)
74
    {
75
        $event = new GetResponseEvent(
76
            new TestHttpKernel(),
77
            $request,
78
            HttpKernelInterface::MASTER_REQUEST
79
        );
80
81
        return $event;
82
    }
83
84
    /**
85
     * @param array $config
86
     * @return RequestListener
87
     */
88
    protected function getListener(array $config)
89
    {
90
        $router = $this->getRouter();
91
92
        $urlGenerator = new CanonicalUrlGenerator($router, $config);
93
        $listener     = new RequestListener($urlGenerator, $config);
94
95
        return $listener;
96
    }
97
}
98