Test Failed
Pull Request — master (#42)
by Matthieu
05:34
created

testLicenseIsNotActiveOrDevelopment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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

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