HostOverrideListenerTest   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 203
Duplicated Lines 35.96 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 73
loc 203
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testHostOverrideMessageIsSetForAdmin() 13 13 1
A testHostOverrideMessageIsNotSetForAdminRedirectResponse() 12 12 1
A testHostOverrideMessageIsNotSetForSubRequest() 12 12 1
A testHostOverrideMessageIsNotSetForXmlRequest() 12 12 1
A testHostOverrideMessageIsNotSetForPreview() 12 12 1
A testHostOverrideMessageIsNotSetForFrontend() 12 12 1
A getHostOverrideListener() 0 33 1
A getFilterResponseEvent() 0 16 1
A getResponse() 0 4 1
A getRedirectResponse() 0 8 1
A getXmlHttpRequest() 0 7 1
A getAdminRequest() 0 4 1
A getAdminPreviewRequest() 0 4 1
A getFrontendRequest() 0 4 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 Kunstmaan\MultiDomainBundle\Tests\EventListener;
4
5
use Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener;
6
use PHPUnit\Framework\TestCase;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpKernel\HttpKernelInterface;
9
10
class HostOverrideListenerTest extends TestCase
11
{
12
    /**
13
     * @var HostOverrideListener
14
     */
15
    protected $object;
16
17
    /**
18
     * @var
19
     */
20
    protected $session;
21
22
    /**
23
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
24
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
25
     */
26 View Code Duplication
    public function testHostOverrideMessageIsSetForAdmin()
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...
27
    {
28
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
29
        $flashBag
30
            ->expects($this->once())
31
            ->method('add')
32
            ->with('warning', 'multi_domain.host_override_active');
33
34
        $object = $this->getHostOverrideListener($flashBag);
35
36
        $event = $this->getFilterResponseEvent($this->getAdminRequest(), $this->getResponse());
37
        $object->onKernelResponse($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...nt\FilterResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
    }
39
40
    /**
41
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
42
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
43
     */
44 View Code Duplication
    public function testHostOverrideMessageIsNotSetForAdminRedirectResponse()
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...
45
    {
46
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
47
        $flashBag
48
            ->expects($this->never())
49
            ->method('add');
50
51
        $object = $this->getHostOverrideListener($flashBag);
52
53
        $event = $this->getFilterResponseEvent($this->getAdminRequest(), $this->getRedirectResponse());
54
        $object->onKernelResponse($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...nt\FilterResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
    }
56
57
    /**
58
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
59
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
60
     */
61 View Code Duplication
    public function testHostOverrideMessageIsNotSetForSubRequest()
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...
62
    {
63
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
64
        $flashBag
65
            ->expects($this->never())
66
            ->method('add');
67
68
        $object = $this->getHostOverrideListener($flashBag);
69
70
        $event = $this->getFilterResponseEvent($this->getAdminRequest(), $this->getResponse(), HttpKernelInterface::SUB_REQUEST);
71
        $object->onKernelResponse($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...nt\FilterResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
    }
73
74
    /**
75
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
76
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
77
     */
78 View Code Duplication
    public function testHostOverrideMessageIsNotSetForXmlRequest()
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...
79
    {
80
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
81
        $flashBag
82
            ->expects($this->never())
83
            ->method('add');
84
85
        $object = $this->getHostOverrideListener($flashBag);
86
87
        $event = $this->getFilterResponseEvent($this->getXmlHttpRequest(), $this->getResponse());
88
        $object->onKernelResponse($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...nt\FilterResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
    }
90
91
    /**
92
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
93
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
94
     */
95 View Code Duplication
    public function testHostOverrideMessageIsNotSetForPreview()
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
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
98
        $flashBag
99
            ->expects($this->never())
100
            ->method('add');
101
102
        $object = $this->getHostOverrideListener($flashBag);
103
104
        $event = $this->getFilterResponseEvent($this->getAdminPreviewRequest(), $this->getResponse());
105
        $object->onKernelResponse($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...nt\FilterResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
    }
107
108
    /**
109
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
110
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
111
     */
112 View Code Duplication
    public function testHostOverrideMessageIsNotSetForFrontend()
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...
113
    {
114
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
115
        $flashBag
116
            ->expects($this->never())
117
            ->method('add');
118
119
        $object = $this->getHostOverrideListener($flashBag);
120
121
        $event = $this->getFilterResponseEvent($this->getFrontendRequest(), $this->getResponse());
122
        $object->onKernelResponse($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...nt\FilterResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
    }
124
125
    private function getHostOverrideListener($flashBag)
126
    {
127
        $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
128
            ->disableOriginalConstructor()
129
            ->getMock();
130
        $session->method('getFlashBag')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
            ->willReturn($flashBag);
132
133
        $domainConfiguration = $this->createMock('Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface');
134
        $domainConfiguration->method('getHost')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
            ->willReturn('override-domain.tld');
136
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
137
        $translator->method('trans')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
            ->willReturnArgument(0);
139
140
        $adminRouteReturnValueMap = array(
141
            array('/nl/admin/preview/some-uri', false),
142
            array('/nl/some-uri', false),
143
            array('/nl/admin/some-admin-uri', true),
144
        );
145
146
        $adminRouteHelper = $this->getMockBuilder('Kunstmaan\AdminBundle\Helper\AdminRouteHelper')
147
            ->disableOriginalConstructor()
148
            ->getMock();
149
        $adminRouteHelper
150
            ->expects($this->any())
151
            ->method('isAdminRoute')
152
            ->willReturnMap($adminRouteReturnValueMap);
153
154
        $listener = new HostOverrideListener($session, $translator, $domainConfiguration, $adminRouteHelper);
0 ignored issues
show
Documentation introduced by
$session is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...dation\Session\Session>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$translator is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...on\TranslatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$domainConfiguration is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminBu...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$adminRouteHelper is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\AdminBu...elper\AdminRouteHelper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
156
        return $listener;
157
    }
158
159
    private function getFilterResponseEvent($request, $response, $requestType = HttpKernelInterface::MASTER_REQUEST)
160
    {
161
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')
162
            ->disableOriginalConstructor()
163
            ->getMock();
164
        $event->method('getRequestType')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
            ->willReturn($requestType);
166
167
        $event->method('getResponse')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
168
            ->willReturn($response);
169
170
        $event->method('getRequest')
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
171
            ->willReturn($request);
172
173
        return $event;
174
    }
175
176
    private function getResponse()
177
    {
178
        return $this->createMock('Symfony\Component\HttpFoundation\Response');
179
    }
180
181
    private function getRedirectResponse()
182
    {
183
        $response = $this->getMockBuilder('Symfony\Component\HttpFoundation\RedirectResponse')
184
            ->disableOriginalConstructor()
185
            ->getMock();
186
187
        return $response;
188
    }
189
190
    private function getXmlHttpRequest()
191
    {
192
        $request = Request::create('http://domain.tld/nl/admin/some-admin-uri');
193
        $request->headers->set('X-Requested-With', 'XMLHttpRequest');
194
195
        return $request;
196
    }
197
198
    private function getAdminRequest()
199
    {
200
        return Request::create('http://domain.tld/nl/admin/some-admin-uri');
201
    }
202
203
    private function getAdminPreviewRequest()
204
    {
205
        return Request::create('http://domain.tld/nl/admin/preview/some-uri');
206
    }
207
208
    private function getFrontendRequest()
209
    {
210
        return Request::create('http://domain.tld/nl/some-uri');
211
    }
212
}
213