Completed
Pull Request — master (#2748)
by Jeroen
14:58
created

HostOverrideListenerTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
     * Sets up the fixture, for example, opens a network connection.
24
     * This method is called before a test is executed.
25
     */
26
    protected function setUp(): void
27
    {
28
    }
29
30
    /**
31
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
32
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
33
     */
34 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...
35
    {
36
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
37
        $flashBag
38
            ->expects($this->once())
39
            ->method('add')
40
            ->with('warning', 'multi_domain.host_override_active');
41
42
        $object = $this->getHostOverrideListener($flashBag);
43
44
        $event = $this->getFilterResponseEvent($this->getAdminRequest(), $this->getResponse());
45
        $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...
46
    }
47
48
    /**
49
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
50
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
51
     */
52 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...
53
    {
54
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
55
        $flashBag
56
            ->expects($this->never())
57
            ->method('add');
58
59
        $object = $this->getHostOverrideListener($flashBag);
60
61
        $event = $this->getFilterResponseEvent($this->getAdminRequest(), $this->getRedirectResponse());
62
        $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...
63
    }
64
65
    /**
66
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
67
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
68
     */
69 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...
70
    {
71
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
72
        $flashBag
73
            ->expects($this->never())
74
            ->method('add');
75
76
        $object = $this->getHostOverrideListener($flashBag);
77
78
        $event = $this->getFilterResponseEvent($this->getAdminRequest(), $this->getResponse(), HttpKernelInterface::SUB_REQUEST);
79
        $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...
80
    }
81
82
    /**
83
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
84
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
85
     */
86 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...
87
    {
88
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
89
        $flashBag
90
            ->expects($this->never())
91
            ->method('add');
92
93
        $object = $this->getHostOverrideListener($flashBag);
94
95
        $event = $this->getFilterResponseEvent($this->getXmlHttpRequest(), $this->getResponse());
96
        $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...
97
    }
98
99
    /**
100
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
101
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
102
     */
103 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...
104
    {
105
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
106
        $flashBag
107
            ->expects($this->never())
108
            ->method('add');
109
110
        $object = $this->getHostOverrideListener($flashBag);
111
112
        $event = $this->getFilterResponseEvent($this->getAdminPreviewRequest(), $this->getResponse());
113
        $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...
114
    }
115
116
    /**
117
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::__construct
118
     * @covers \Kunstmaan\MultiDomainBundle\EventListener\HostOverrideListener::onKernelResponse
119
     */
120 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...
121
    {
122
        $flashBag = $this->createMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface');
123
        $flashBag
124
            ->expects($this->never())
125
            ->method('add');
126
127
        $object = $this->getHostOverrideListener($flashBag);
128
129
        $event = $this->getFilterResponseEvent($this->getFrontendRequest(), $this->getResponse());
130
        $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...
131
    }
132
133
    private function getHostOverrideListener($flashBag)
134
    {
135
        $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
136
            ->disableOriginalConstructor()
137
            ->getMock();
138
        $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...
139
            ->willReturn($flashBag);
140
141
        $domainConfiguration = $this->createMock('Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface');
142
        $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...
143
            ->willReturn('override-domain.tld');
144
        $translator = $this->createMock('Symfony\Component\Translation\TranslatorInterface');
145
        $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...
146
            ->willReturnArgument(0);
147
148
        $adminRouteReturnValueMap = array(
149
            array('/nl/admin/preview/some-uri', false),
150
            array('/nl/some-uri', false),
151
            array('/nl/admin/some-admin-uri', true),
152
        );
153
154
        $adminRouteHelper = $this->getMockBuilder('Kunstmaan\AdminBundle\Helper\AdminRouteHelper')
155
            ->disableOriginalConstructor()
156
            ->getMock();
157
        $adminRouteHelper
158
            ->expects($this->any())
159
            ->method('isAdminRoute')
160
            ->willReturnMap($adminRouteReturnValueMap);
161
162
        $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...
163
164
        return $listener;
165
    }
166
167
    private function getFilterResponseEvent($request, $response, $requestType = HttpKernelInterface::MASTER_REQUEST)
168
    {
169
        $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')
170
            ->disableOriginalConstructor()
171
            ->getMock();
172
        $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...
173
            ->willReturn($requestType);
174
175
        $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...
176
            ->willReturn($response);
177
178
        $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...
179
            ->willReturn($request);
180
181
        return $event;
182
    }
183
184
    private function getResponse()
185
    {
186
        return $this->createMock('Symfony\Component\HttpFoundation\Response');
187
    }
188
189
    private function getRedirectResponse()
190
    {
191
        $response = $this->getMockBuilder('Symfony\Component\HttpFoundation\RedirectResponse')
192
            ->disableOriginalConstructor()
193
            ->getMock();
194
195
        return $response;
196
    }
197
198
    private function getXmlHttpRequest()
199
    {
200
        $request = Request::create('http://domain.tld/nl/admin/some-admin-uri');
201
        $request->headers->set('X-Requested-With', 'XMLHttpRequest');
202
203
        return $request;
204
    }
205
206
    private function getAdminRequest()
207
    {
208
        return Request::create('http://domain.tld/nl/admin/some-admin-uri');
209
    }
210
211
    private function getAdminPreviewRequest()
212
    {
213
        return Request::create('http://domain.tld/nl/admin/preview/some-uri');
214
    }
215
216
    private function getFrontendRequest()
217
    {
218
        return Request::create('http://domain.tld/nl/some-uri');
219
    }
220
}
221