Passed
Push — master ( d17922...d3d540 )
by Bukashk0zzz
02:09 queued 14s
created

LicenseListenerTest::getEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKe...\Event\GetResponseEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Symfony\Component\HttpKernel\Event\RequestEvent;
15
use Symfony\Component\HttpKernel\KernelInterface;
16
use Symfony\Component\Routing\RouterInterface;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19
use Symfony\Component\Security\Core\User\UserInterface;
20
21
/**
22
 * Class LicenseListenerTest
23
 */
24
final class LicenseListenerTest extends TestCase
25
{
26
    /**
27
     * @var MockObject|KernelInterface
28
     */
29
    private $kernel;
30
31
    /**
32
     * @var MockObject|RouterInterface
33
     */
34
    private $router;
35
36
    /**
37
     * @var MockObject|TokenStorageInterface
38
     */
39
    private $tokenStorage;
40
41
    /**
42
     * @var LicenseListener
43
     */
44
    private $listener;
45
46
    /**
47
     * setUp
48
     */
49
    protected function setUp(): void
50
    {
51
        $this->router = $this->createMock(RouterInterface::class);
52
        $this->kernel = $this->createMock(KernelInterface::class);
53
        $this->tokenStorage = $this->createMock(TokenStorageInterface::class);
54
55
        $this->listener = new LicenseListener(
56
            $this->router,
57
            $this->tokenStorage
58
        );
59
    }
60
61
    /**
62
     * Test
63
     */
64
    public function testItSkipsOnASubRequest(): void
65
    {
66
        $attributeParameterBag = $this->createMock(ParameterBagInterface::class);
67
        $attributeParameterBag
68
            ->expects($this->never())
69
            ->method('get');
70
71
        $request = new Request();
72
        $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...
73
74
        $event = $this->getEvent(
75
            $this->kernel,
76
            $request,
77
            KernelInterface::SUB_REQUEST
78
        );
79
80
        $this->listener->onKernelRequest($event);
81
    }
82
83
    /**
84
     * Test
85
     */
86
    public function testItSkipsWhenTheRouteIsNullAndRouteRequiresNoLicense(): void
87
    {
88
        $request = new Request(
89
            ['lic' => 'test'],
90
            [],
91
            [
92
                '_route' => 'route',
93
                'requires_license' => false,
94
            ]
95
        );
96
97
        $this->kernel
98
            ->expects($this->never())
99
            ->method('getEnvironment');
100
101
        $event = $this->getEvent(
102
            $this->kernel,
103
            $request,
104
            KernelInterface::MASTER_REQUEST
105
        );
106
107
        $this->listener->onKernelRequest($event);
108
    }
109
110
    /**
111
     * Test
112
     */
113
    public function testLicenseIsNotActiveOrDevelopment(): void
114
    {
115
        $request1 = new Request(
116
            ['lic' => 'active'],
117
            [],
118
            [
119
                '_route' => 'route',
120
                'requires_license' => true,
121
            ]
122
        );
123
124
        $request2 = new Request(
125
            ['lic' => 'notactive'],
126
            [],
127
            [
128
                '_route' => 'route',
129
                'requires_license' => true,
130
            ]
131
        );
132
133
        $this->kernel
134
            ->expects($this->once())
135
            ->method('getEnvironment')
136
            ->willReturn('dev');
137
138
        $this->tokenStorage
139
            ->expects($this->never())
140
            ->method('getToken');
141
142
        $event1 = $this->getEvent(
143
            $this->kernel,
144
            $request1,
145
            KernelInterface::MASTER_REQUEST
146
        );
147
148
        $event2 = $this->getEvent(
149
            $this->kernel,
150
            $request2,
151
            KernelInterface::MASTER_REQUEST
152
        );
153
154
        $this->listener->onKernelRequest($event1);
155
        $this->listener->onKernelRequest($event2);
156
    }
157
158
    /**
159
     * Test
160
     */
161
    public function testUserIsNoTenant(): void
162
    {
163
        $request = new Request(
164
            ['lic' => 'not_active'],
165
            [],
166
            [
167
                '_route' => 'route',
168
                'requires_license' => true,
169
            ]
170
        );
171
172
        $this->kernel
173
            ->expects($this->once())
174
            ->method('getEnvironment')
175
            ->willReturn('prod');
176
177
        $this->kernel
178
            ->expects($this->never())
179
            ->method('getContainer');
180
181
        $token = $this->createMock(TokenInterface::class);
182
        $token
183
            ->expects($this->once())
184
            ->method('getUser')
185
            ->willReturn($this->createMock(UserInterface::class));
186
187
        $this->tokenStorage
188
            ->expects($this->once())
189
            ->method('getToken')
190
            ->willReturn($token);
191
192
        $this->router
193
            ->expects($this->once())
194
            ->method('generate')
195
            ->with('atlassian_connect_unlicensed')
196
            ->willReturn('http://website.com');
197
198
        $event = $this->getEvent(
199
            $this->kernel,
200
            $request,
201
            KernelInterface::MASTER_REQUEST
202
        );
203
204
        $this->listener->onKernelRequest($event);
205
206
        $response = $event->getResponse();
207
        $this->assertInstanceOf(RedirectResponse::class, $response);
208
        $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

208
        $this->assertEquals('http://website.com', $response->/** @scrutinizer ignore-call */ getTargetUrl());
Loading history...
209
    }
210
211
    /**
212
     * Test
213
     */
214
    public function testTenantIsWhiteListed(): void
215
    {
216
        $request = new Request(
217
            ['lic' => 'not_active'],
218
            [],
219
            [
220
                '_route' => 'route',
221
                'requires_license' => true,
222
            ]
223
        );
224
225
        $this->kernel
226
            ->expects($this->once())
227
            ->method('getEnvironment')
228
            ->willReturn('prod');
229
230
        $user = new Tenant();
231
        $user->setIsWhiteListed(true);
232
233
        $token = $this->createMock(TokenInterface::class);
234
        $token
235
            ->expects($this->once())
236
            ->method('getUser')
237
            ->willReturn($user);
238
239
        $this->tokenStorage
240
            ->expects($this->once())
241
            ->method('getToken')
242
            ->willReturn($token);
243
244
        $this->kernel
245
            ->expects($this->never())
246
            ->method('getContainer');
247
248
        $event = $this->getEvent(
249
            $this->kernel,
250
            $request,
251
            KernelInterface::MASTER_REQUEST
252
        );
253
254
        $this->listener->onKernelRequest($event);
255
    }
256
257
    /**
258
     * Test
259
     */
260
    public function testIsValidByWhiteList(): void
261
    {
262
        $request = new Request(
263
            ['lic' => 'not_active'],
264
            [],
265
            [
266
                '_route' => 'route',
267
                'requires_license' => true,
268
            ]
269
        );
270
271
        $this->kernel
272
            ->expects($this->once())
273
            ->method('getEnvironment')
274
            ->willReturn('prod');
275
276
        $user = new Tenant();
277
        $user->setClientKey('key');
278
279
        $token = $this->createMock(TokenInterface::class);
280
        $token
281
            ->expects($this->once())
282
            ->method('getUser')
283
            ->willReturn($user);
284
285
        $this->tokenStorage
286
            ->expects($this->once())
287
            ->method('getToken')
288
            ->willReturn($token);
289
290
        $date = new \DateTime();
291
        $date->modify('+1 day');
292
293
        $container = $this->createMock(ContainerInterface::class);
294
        $container
295
            ->expects($this->once())
296
            ->method('getParameter')
297
            ->willReturn([
298
                ['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key'],
299
            ]);
300
301
        $this->kernel
302
            ->expects($this->once())
303
            ->method('getContainer')
304
            ->willReturn($container);
305
306
        $event = $this->getEvent(
307
            $this->kernel,
308
            $request,
309
            KernelInterface::MASTER_REQUEST
310
        );
311
312
        $this->listener->onKernelRequest($event);
313
        $this->assertNull($event->getResponse());
314
    }
315
316
    /**
317
     * Test
318
     */
319
    public function testWhiteListIsExpired(): void
320
    {
321
        $request = new Request(
322
            ['lic' => 'not_active'],
323
            [],
324
            [
325
                '_route' => 'route',
326
                'requires_license' => true,
327
            ]
328
        );
329
330
        $this->kernel
331
            ->expects($this->once())
332
            ->method('getEnvironment')
333
            ->willReturn('prod');
334
335
        $user = new Tenant();
336
        $user->setClientKey('key');
337
338
        $token = $this->createMock(TokenInterface::class);
339
        $token
340
            ->expects($this->once())
341
            ->method('getUser')
342
            ->willReturn($user);
343
344
        $this->tokenStorage
345
            ->expects($this->once())
346
            ->method('getToken')
347
            ->willReturn($token);
348
349
        $date = new \DateTime();
350
        $date->modify('-1 day');
351
352
        $container = $this->createMock(ContainerInterface::class);
353
        $container
354
            ->expects($this->once())
355
            ->method('getParameter')
356
            ->willReturn([
357
                ['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key'],
358
            ]);
359
360
        $this->kernel
361
            ->expects($this->once())
362
            ->method('getContainer')
363
            ->willReturn($container);
364
365
        $this->router
366
            ->expects($this->once())
367
            ->method('generate')
368
            ->with('atlassian_connect_unlicensed')
369
            ->willReturn('http://website.com');
370
371
        $event = $this->getEvent(
372
            $this->kernel,
373
            $request,
374
            KernelInterface::MASTER_REQUEST
375
        );
376
377
        $this->listener->onKernelRequest($event);
378
        $this->assertNotNull($event->getResponse());
379
        $response = $event->getResponse();
380
        $this->assertInstanceOf(RedirectResponse::class, $response);
381
        $this->assertEquals('http://website.com', $response->getTargetUrl());
382
    }
383
384
    /**
385
     * Test
386
     */
387
    public function testThrowsException(): void
388
    {
389
        $request = new Request(
390
            ['lic' => 'not_active'],
391
            [],
392
            [
393
                '_route' => 'route',
394
                'requires_license' => true,
395
            ]
396
        );
397
398
        $this->kernel
399
            ->expects($this->once())
400
            ->method('getEnvironment')
401
            ->willReturn('prod');
402
403
        $user = new Tenant();
404
        $user->setClientKey('key');
405
406
        $this->tokenStorage
407
            ->expects($this->once())
408
            ->method('getToken')
409
            ->willThrowException(new \Exception());
410
411
        $this->router
412
            ->expects($this->once())
413
            ->method('generate')
414
            ->with('atlassian_connect_unlicensed')
415
            ->willReturn('http://website.com');
416
417
        $event = $this->getEvent(
418
            $this->kernel,
419
            $request,
420
            KernelInterface::MASTER_REQUEST
421
        );
422
423
        $this->listener->onKernelRequest($event);
424
        $this->assertNotNull($event->getResponse());
425
        $response = $event->getResponse();
426
        $this->assertInstanceOf(RedirectResponse::class, $response);
427
        $this->assertEquals('http://website.com', $response->getTargetUrl());
428
    }
429
430
    /**
431
     * Gets an event according to the symfony version
432
     *
433
     * @param KernelInterface $kernel
434
     * @param Request         $request
435
     * @param int             $type
436
     *
437
     * @return GetResponseEvent|RequestEvent
438
     */
439
    private function getEvent(KernelInterface $kernel, Request $request, int $type)
440
    {
441
        if (\class_exists(RequestEvent::class)) {
442
            return new RequestEvent($kernel, $request, $type);
443
        }
444
445
        return new GetResponseEvent($kernel, $request, $type);
446
    }
447
}
448