Test Setup Failed
Push — master ( c6cae9...d17922 )
by Bukashk0zzz
46s queued 13s
created

LicenseListenerTest::testThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 41
rs 9.472
1
<?php declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Tests\Listener;
4
5
use AtlassianConnectBundle\Entity\Tenant;
6
use AtlassianConnectBundle\Listener\LicenseListener;
7
use PHPUnit\Framework\MockObject\MockObject;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
14
use Symfony\Component\HttpKernel\KernelInterface;
15
use Symfony\Component\Routing\RouterInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18
19
/**
20
 * Class LicenseListenerTest
21
 */
22
final class LicenseListenerTest extends TestCase
23
{
24
    /**
25
     * @var MockObject|KernelInterface
26
     */
27
    private $kernel;
28
29
    /**
30
     * @var MockObject|RouterInterface
31
     */
32
    private $router;
33
34
    /**
35
     * @var MockObject|TokenStorageInterface
36
     */
37
    private $tokenStorage;
38
39
    /**
40
     * @var LicenseListener
41
     */
42
    private $listener;
43
44
    /**
45
     * setUp
46
     */
47
    protected function setUp(): void
48
    {
49
        $this->router = $this->createMock(RouterInterface::class);
50
        $this->kernel = $this->createMock(KernelInterface::class);
51
        $this->tokenStorage = $this->createMock(TokenStorageInterface::class);
52
53
        $this->listener = new LicenseListener(
54
            $this->router,
55
            $this->tokenStorage
56
        );
57
    }
58
59
    /**
60
     * Test
61
     */
62
    public function testItSkipsOnASubRequest(): void
63
    {
64
        $attributeParameterBag = $this->createMock(ParameterBagInterface::class);
65
        $attributeParameterBag
66
            ->expects($this->never())
67
            ->method('get');
68
69
        $request = new Request();
70
        $request->attributes = $attributeParameterBag;
0 ignored issues
show
Documentation Bug introduced by
It seems like $attributeParameterBag of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\HttpFoundation\ParameterBag of property $attributes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
72
        $event = new GetResponseEvent(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated: since Symfony 4.3, use RequestEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

72
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
73
            $this->kernel,
74
            $request,
75
            KernelInterface::SUB_REQUEST
76
        );
77
78
        $this->listener->onKernelRequest($event);
79
    }
80
81
    /**
82
     * Test
83
     */
84
    public function testItSkipsWhenTheRouteIsNullAndRouteRequiresNoLicense(): void
85
    {
86
        $request = new Request(
87
            ['lic' => 'test'],
88
            [],
89
            [
90
                '_route' => 'route',
91
                'requires_license' => false,
92
            ]
93
        );
94
95
        $this->kernel
96
            ->expects($this->never())
97
            ->method('getEnvironment');
98
99
        $event = new GetResponseEvent(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated: since Symfony 4.3, use RequestEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

99
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
100
            $this->kernel,
101
            $request,
102
            KernelInterface::MASTER_REQUEST
103
        );
104
105
        $this->listener->onKernelRequest($event);
106
    }
107
108
    /**
109
     * Test
110
     */
111
    public function testLicenseIsNotActiveOrDevelopment(): void
112
    {
113
        $request1 = new Request(
114
            ['lic' => 'active'],
115
            [],
116
            [
117
                '_route' => 'route',
118
                'requires_license' => true,
119
            ]
120
        );
121
122
        $request2 = new Request(
123
            ['lic' => 'notactive'],
124
            [],
125
            [
126
                '_route' => 'route',
127
                'requires_license' => true,
128
            ]
129
        );
130
131
        $this->kernel
132
            ->expects($this->once())
133
            ->method('getEnvironment')
134
            ->willReturn('dev');
135
136
        $this->tokenStorage
137
            ->expects($this->never())
138
            ->method('getToken');
139
140
        $event1 = new GetResponseEvent(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated: since Symfony 4.3, use RequestEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

140
        $event1 = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
141
            $this->kernel,
142
            $request1,
143
            KernelInterface::MASTER_REQUEST
144
        );
145
146
        $event2 = new GetResponseEvent(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated: since Symfony 4.3, use RequestEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

146
        $event2 = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
147
            $this->kernel,
148
            $request2,
149
            KernelInterface::MASTER_REQUEST
150
        );
151
152
        $this->listener->onKernelRequest($event1);
153
        $this->listener->onKernelRequest($event2);
154
    }
155
156
    /**
157
     * Test
158
     */
159
    public function testUserIsNoTenant(): void
160
    {
161
        $request = new Request(
162
            ['lic' => 'not_active'],
163
            [],
164
            [
165
                '_route' => 'route',
166
                'requires_license' => true,
167
            ]
168
        );
169
170
        $this->kernel
171
            ->expects($this->once())
172
            ->method('getEnvironment')
173
            ->willReturn('prod');
174
175
        $this->kernel
176
            ->expects($this->never())
177
            ->method('getContainer');
178
179
        $token = $this->createMock(TokenInterface::class);
180
        $token
181
            ->expects($this->once())
182
            ->method('getUser')
183
            ->willReturn(new TestUser());
184
185
        $this->tokenStorage
186
            ->expects($this->once())
187
            ->method('getToken')
188
            ->willReturn($token);
189
190
        $this->router
191
            ->expects($this->once())
192
            ->method('generate')
193
            ->with('atlassian_connect_unlicensed')
194
            ->willReturn('http://website.com');
195
196
        $event = new GetResponseEvent(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated: since Symfony 4.3, use RequestEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

196
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
197
            $this->kernel,
198
            $request,
199
            KernelInterface::MASTER_REQUEST
200
        );
201
202
        $this->listener->onKernelRequest($event);
203
204
        $response = $event->getResponse();
205
        $this->assertInstanceOf(RedirectResponse::class, $response);
206
        $this->assertEquals('http://website.com', $response->getTargetUrl());
0 ignored issues
show
Bug introduced by
The method getTargetUrl() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Symfony\Component\HttpFoundation\RedirectResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
        $this->assertEquals('http://website.com', $response->/** @scrutinizer ignore-call */ getTargetUrl());
Loading history...
207
    }
208
209
    /**
210
     * Test
211
     */
212
    public function testTenantIsWhiteListed(): void
213
    {
214
        $request = new Request(
215
            ['lic' => 'not_active'],
216
            [],
217
            [
218
                '_route' => 'route',
219
                'requires_license' => true,
220
            ]
221
        );
222
223
        $this->kernel
224
            ->expects($this->once())
225
            ->method('getEnvironment')
226
            ->willReturn('prod');
227
228
        $user = new Tenant();
229
        $user->setIsWhiteListed(true);
230
231
        $token = $this->createMock(TokenInterface::class);
232
        $token
233
            ->expects($this->once())
234
            ->method('getUser')
235
            ->willReturn($user);
236
237
        $this->tokenStorage
238
            ->expects($this->once())
239
            ->method('getToken')
240
            ->willReturn($token);
241
242
        $this->kernel
243
            ->expects($this->never())
244
            ->method('getContainer');
245
246
        $event = new GetResponseEvent(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated: since Symfony 4.3, use RequestEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

246
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
247
            $this->kernel,
248
            $request,
249
            KernelInterface::MASTER_REQUEST
250
        );
251
252
        $this->listener->onKernelRequest($event);
253
    }
254
255
    /**
256
     * Test
257
     */
258
    public function testIsValidByWhiteList(): void
259
    {
260
        $request = new Request(
261
            ['lic' => 'not_active'],
262
            [],
263
            [
264
                '_route' => 'route',
265
                'requires_license' => true,
266
            ]
267
        );
268
269
        $this->kernel
270
            ->expects($this->once())
271
            ->method('getEnvironment')
272
            ->willReturn('prod');
273
274
        $user = new Tenant();
275
        $user->setClientKey('key');
276
277
        $token = $this->createMock(TokenInterface::class);
278
        $token
279
            ->expects($this->once())
280
            ->method('getUser')
281
            ->willReturn($user);
282
283
        $this->tokenStorage
284
            ->expects($this->once())
285
            ->method('getToken')
286
            ->willReturn($token);
287
288
        $date = new \DateTime();
289
        $date->modify('+1 day');
290
291
        $container = $this->createMock(ContainerInterface::class);
292
        $container
293
            ->expects($this->once())
294
            ->method('getParameter')
295
            ->willReturn([
296
                ['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key'],
297
            ]);
298
299
        $this->kernel
300
            ->expects($this->once())
301
            ->method('getContainer')
302
            ->willReturn($container);
303
304
        $event = new GetResponseEvent(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated: since Symfony 4.3, use RequestEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

304
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
305
            $this->kernel,
306
            $request,
307
            KernelInterface::MASTER_REQUEST
308
        );
309
310
        $this->listener->onKernelRequest($event);
311
        $this->assertNull($event->getResponse());
312
    }
313
314
    /**
315
     * Test
316
     */
317
    public function testWhiteListIsExpired(): void
318
    {
319
        $request = new Request(
320
            ['lic' => 'not_active'],
321
            [],
322
            [
323
                '_route' => 'route',
324
                'requires_license' => true,
325
            ]
326
        );
327
328
        $this->kernel
329
            ->expects($this->once())
330
            ->method('getEnvironment')
331
            ->willReturn('prod');
332
333
        $user = new Tenant();
334
        $user->setClientKey('key');
335
336
        $token = $this->createMock(TokenInterface::class);
337
        $token
338
            ->expects($this->once())
339
            ->method('getUser')
340
            ->willReturn($user);
341
342
        $this->tokenStorage
343
            ->expects($this->once())
344
            ->method('getToken')
345
            ->willReturn($token);
346
347
        $date = new \DateTime();
348
        $date->modify('-1 day');
349
350
        $container = $this->createMock(ContainerInterface::class);
351
        $container
352
            ->expects($this->once())
353
            ->method('getParameter')
354
            ->willReturn([
355
                ['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key'],
356
            ]);
357
358
        $this->kernel
359
            ->expects($this->once())
360
            ->method('getContainer')
361
            ->willReturn($container);
362
363
        $this->router
364
            ->expects($this->once())
365
            ->method('generate')
366
            ->with('atlassian_connect_unlicensed')
367
            ->willReturn('http://website.com');
368
369
        $event = new GetResponseEvent(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated: since Symfony 4.3, use RequestEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

369
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
370
            $this->kernel,
371
            $request,
372
            KernelInterface::MASTER_REQUEST
373
        );
374
375
        $this->listener->onKernelRequest($event);
376
        $this->assertNotNull($event->getResponse());
377
        $response = $event->getResponse();
378
        $this->assertInstanceOf(RedirectResponse::class, $response);
379
        $this->assertEquals('http://website.com', $response->getTargetUrl());
380
    }
381
382
    /**
383
     * Test
384
     */
385
    public function testThrowsException(): void
386
    {
387
        $request = new Request(
388
            ['lic' => 'not_active'],
389
            [],
390
            [
391
                '_route' => 'route',
392
                'requires_license' => true,
393
            ]
394
        );
395
396
        $this->kernel
397
            ->expects($this->once())
398
            ->method('getEnvironment')
399
            ->willReturn('prod');
400
401
        $user = new Tenant();
402
        $user->setClientKey('key');
403
404
        $this->tokenStorage
405
            ->expects($this->once())
406
            ->method('getToken')
407
            ->willThrowException(new \Exception());
408
409
        $this->router
410
            ->expects($this->once())
411
            ->method('generate')
412
            ->with('atlassian_connect_unlicensed')
413
            ->willReturn('http://website.com');
414
415
        $event = new GetResponseEvent(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated: since Symfony 4.3, use RequestEvent instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

415
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
416
            $this->kernel,
417
            $request,
418
            KernelInterface::MASTER_REQUEST
419
        );
420
421
        $this->listener->onKernelRequest($event);
422
        $this->assertNotNull($event->getResponse());
423
        $response = $event->getResponse();
424
        $this->assertInstanceOf(RedirectResponse::class, $response);
425
        $this->assertEquals('http://website.com', $response->getTargetUrl());
426
    }
427
}
428