Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

unit/EventListener/HostOverrideListenerTest.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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