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

ExceptionListenerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 41.6 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 52
loc 125
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testTrailingSlashRedirect() 15 15 1
A testNoTrailingSlashRedirect() 0 18 1
A testNonMatchingAlternativeRouteReturnsFalse() 11 11 1
A testKernelRequestListenerDoesNothingForNonExistentRoute() 11 11 1
A testRouteWithUrlParametersAndTrailingSlashRedirectsToCorrectRoute() 15 15 1
A getGetResponseForExceptionEvent() 0 11 1
A getListener() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * @param array $config
19
     */
20 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...
21
    {
22
        $request = $this->getFooRequest();
23
        $event   = $this->getGetResponseForExceptionEvent($request);
24
25
        $listener = $this->getListener($config);
26
27
        $listener->onKernelException($event);
28
29
        /** @var RedirectResponse $response */
30
        $response = $event->getResponse();
31
32
        $this->assertTrue($response instanceof RedirectResponse);
33
        $this->assertEquals('https://example.org/foo', $response->getTargetUrl());
34
    }
35
36
    /**
37
     * @dataProvider configProvider
38
     * @param array $config
39
     */
40
    public function testNoTrailingSlashRedirect(array $config)
41
    {
42
        $config['trailing_slash'] = true;
43
44
        $request = $this->getBazRequest(true, false);
45
46
        $event = $this->getGetResponseForExceptionEvent($request);
47
48
        $listener = $this->getListener($config);
49
50
        $listener->onKernelException($event);
51
52
        /** @var RedirectResponse $response */
53
        $response = $event->getResponse();
54
55
        $this->assertTrue($response instanceof RedirectResponse);
56
        $this->assertEquals('https://example.org/baz/', $response->getTargetUrl());
57
    }
58
59
    /**
60
     * @dataProvider configProvider
61
     * @param array $config
62
     */
63 View Code Duplication
    public function testNonMatchingAlternativeRouteReturnsFalse(array $config)
64
    {
65
        $request = Request::create('https://example.org/bar/');
66
        $event   = $this->getGetResponseForExceptionEvent($request);
67
68
        $listener = $this->getListener($config);
69
70
        $returnValue = $listener->onKernelException($event);
71
72
        $this->assertFalse($returnValue);
73
    }
74
75
    /**
76
     * @dataProvider configProvider
77
     * @param array $config
78
     */
79 View Code Duplication
    public function testKernelRequestListenerDoesNothingForNonExistentRoute(array $config)
80
    {
81
        $request = Request::create('https://example.org/bar');
82
        $event   = $this->getGetResponseForExceptionEvent($request);
83
84
        $listener = $this->getListener($config);
85
86
        $returnValue = $listener->onKernelException($event);
87
88
        $this->assertFalse($returnValue);
89
    }
90
91
    /**
92
     * @dataProvider configProvider
93
     * @param array $config
94
     */
95 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...
96
    {
97
        $request = Request::create('https://example.org/foo/bar/');
98
        $event   = $this->getGetResponseForExceptionEvent($request);
99
100
        $listener = $this->getListener($config);
101
102
        $listener->onKernelException($event);
103
104
        /** @var RedirectResponse $response */
105
        $response = $event->getResponse();
106
107
        $this->assertInstanceOf(RedirectResponse::class, $response);
108
        $this->assertEquals('https://example.org/foo/bar', $response->getTargetUrl());
109
    }
110
111
    /**
112
     * @param Request $request
113
     * @return GetResponseForExceptionEvent
114
     */
115
    protected function getGetResponseForExceptionEvent(Request $request)
116
    {
117
        $event = new GetResponseForExceptionEvent(
118
            new TestHttpKernel(),
119
            $request,
120
            HttpKernelInterface::MASTER_REQUEST,
121
            new NotFoundHttpException('')
122
        );
123
124
        return $event;
125
    }
126
127
    /**
128
     * @param array $config
129
     * @return ExceptionListener
130
     */
131
    protected function getListener(array $config)
132
    {
133
        $router   = $this->getRouter();
134
        $listener = new ExceptionListener($router, $config);
135
136
        return $listener;
137
    }
138
}
139