Test Setup Failed
Pull Request — master (#41)
by Matthieu
04:47
created

TestUser::getUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
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;
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
use Symfony\Component\Security\Core\User\UserInterface;
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
    protected function setUp(): void
46
    {
47
        $this->router = $this->createMock(RouterInterface::class);
48
        $this->kernel = $this->createMock(KernelInterface::class);
49
        $this->tokenStorage = $this->createMock(TokenStorageInterface::class);
50
51
        $this->listener = new LicenseListener(
52
            $this->router,
53
            $this->tokenStorage
54
        );
55
    }
56
57
    public function testItSkipsOnASubRequest(): void
58
    {
59
        $attributeParameterBag = $this->createMock(ParameterBagInterface::class);
60
        $attributeParameterBag
61
            ->expects($this->never())
62
            ->method('get');
63
64
        $request = new Request();
65
        $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...
66
67
        $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

67
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
68
            $this->kernel,
69
            $request,
70
            KernelInterface::SUB_REQUEST
71
        );
72
73
        $this->listener->onKernelRequest($event);
74
    }
75
76
    public function testItSkipsWhenTheRouteIsNullAndRouteRequiresNoLicense()
77
    {
78
        $request = new Request(
79
            ['lic' => 'test'],
80
            [],
81
            [
82
                '_route' => 'route',
83
                'requires_license' => false,
84
            ]
85
        );
86
87
        $this->kernel
88
            ->expects($this->never())
89
            ->method('getEnvironment');
90
91
        $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

91
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
92
            $this->kernel,
93
            $request,
94
            KernelInterface::MASTER_REQUEST
95
        );
96
97
        $this->listener->onKernelRequest($event);
98
    }
99
100
    public function testLicenseIsNotActiveOrDevelopment(): void
101
    {
102
        $request1 = new Request(
103
            ['lic' => 'active'],
104
            [],
105
            [
106
                '_route' => 'route',
107
                'requires_license' => true,
108
            ]
109
        );
110
111
        $request2 = new Request(
112
            ['lic' => 'notactive'],
113
            [],
114
            [
115
                '_route' => 'route',
116
                'requires_license' => true,
117
            ]
118
        );
119
120
        $this->kernel
121
            ->expects($this->once())
122
            ->method('getEnvironment')
123
            ->willReturn('dev');
124
125
        $this->tokenStorage
126
            ->expects($this->never())
127
            ->method('getToken');
128
129
        $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

129
        $event1 = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
130
            $this->kernel,
131
            $request1,
132
            KernelInterface::MASTER_REQUEST
133
        );
134
135
        $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

135
        $event2 = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
136
            $this->kernel,
137
            $request2,
138
            KernelInterface::MASTER_REQUEST
139
        );
140
141
        $this->listener->onKernelRequest($event1);
142
        $this->listener->onKernelRequest($event2);
143
    }
144
145
    public function testUserIsNoTenant(): void
146
    {
147
        $request = new Request(
148
            ['lic' => 'not_active'],
149
            [],
150
            [
151
                '_route' => 'route',
152
                'requires_license' => true,
153
            ]
154
        );
155
156
        $this->kernel
157
            ->expects($this->once())
158
            ->method('getEnvironment')
159
            ->willReturn('prod');
160
161
        $this->kernel
162
            ->expects($this->never())
163
            ->method('getContainer');
164
165
        $token = $this->createMock(TokenInterface::class);
166
        $token
167
            ->expects($this->once())
168
            ->method('getUser')
169
            ->willReturn(new TestUser());
170
171
        $this->tokenStorage
172
            ->expects($this->once())
173
            ->method('getToken')
174
            ->willReturn($token);
175
176
        $this->router
177
            ->expects($this->once())
178
            ->method('generate')
179
            ->with('atlassian_connect_unlicensed')
180
            ->willReturn('http://website.com');
181
182
        $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

182
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
183
            $this->kernel,
184
            $request,
185
            KernelInterface::MASTER_REQUEST
186
        );
187
188
        $this->listener->onKernelRequest($event);
189
190
        $response = $event->getResponse();
191
        $this->assertInstanceOf(RedirectResponse::class, $response);
192
        $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

192
        $this->assertEquals('http://website.com', $response->/** @scrutinizer ignore-call */ getTargetUrl());
Loading history...
193
    }
194
195
    public function testTenantIsWhiteListed(): void
196
    {
197
        $request = new Request(
198
            ['lic' => 'not_active'],
199
            [],
200
            [
201
                '_route' => 'route',
202
                'requires_license' => true,
203
            ]
204
        );
205
206
        $this->kernel
207
            ->expects($this->once())
208
            ->method('getEnvironment')
209
            ->willReturn('prod');
210
211
        $user = new Tenant();
212
        $user->setIsWhiteListed(true);
213
214
        $token = $this->createMock(TokenInterface::class);
215
        $token
216
            ->expects($this->once())
217
            ->method('getUser')
218
            ->willReturn($user);
219
220
        $this->tokenStorage
221
            ->expects($this->once())
222
            ->method('getToken')
223
            ->willReturn($token);
224
225
        $this->kernel
226
            ->expects($this->never())
227
            ->method('getContainer');
228
229
        $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

229
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
230
            $this->kernel,
231
            $request,
232
            KernelInterface::MASTER_REQUEST
233
        );
234
235
        $this->listener->onKernelRequest($event);
236
    }
237
238
    public function testIsValidByWhiteList(): void
239
    {
240
        $request = new Request(
241
            ['lic' => 'not_active'],
242
            [],
243
            [
244
                '_route' => 'route',
245
                'requires_license' => true,
246
            ]
247
        );
248
249
        $this->kernel
250
            ->expects($this->once())
251
            ->method('getEnvironment')
252
            ->willReturn('prod');
253
254
        $user = new Tenant();
255
        $user->setClientKey('key');
256
257
        $token = $this->createMock(TokenInterface::class);
258
        $token
259
            ->expects($this->once())
260
            ->method('getUser')
261
            ->willReturn($user);
262
263
        $this->tokenStorage
264
            ->expects($this->once())
265
            ->method('getToken')
266
            ->willReturn($token);
267
268
        $date = new \DateTime();
269
        $date->modify('+1 day');
270
271
        $container = $this->createMock(ContainerInterface::class);
272
        $container
273
            ->expects($this->once())
274
            ->method('getParameter')
275
            ->willReturn([
276
                ['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key'],
277
            ]);
278
279
        $this->kernel
280
            ->expects($this->once())
281
            ->method('getContainer')
282
            ->willReturn($container);
283
284
        $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

284
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
285
            $this->kernel,
286
            $request,
287
            KernelInterface::MASTER_REQUEST
288
        );
289
290
        $this->listener->onKernelRequest($event);
291
        $this->assertNull($event->getResponse());
292
    }
293
294
    public function testWhiteListIsExpired(): void
295
    {
296
        $request = new Request(
297
            ['lic' => 'not_active'],
298
            [],
299
            [
300
                '_route' => 'route',
301
                'requires_license' => true,
302
            ]
303
        );
304
305
        $this->kernel
306
            ->expects($this->once())
307
            ->method('getEnvironment')
308
            ->willReturn('prod');
309
310
        $user = new Tenant();
311
        $user->setClientKey('key');
312
313
        $token = $this->createMock(TokenInterface::class);
314
        $token
315
            ->expects($this->once())
316
            ->method('getUser')
317
            ->willReturn($user);
318
319
        $this->tokenStorage
320
            ->expects($this->once())
321
            ->method('getToken')
322
            ->willReturn($token);
323
324
        $date = new \DateTime();
325
        $date->modify('-1 day');
326
327
        $container = $this->createMock(ContainerInterface::class);
328
        $container
329
            ->expects($this->once())
330
            ->method('getParameter')
331
            ->willReturn([
332
                ['valid_till' => $date->format('Y-m-d'), 'client_key' => 'key'],
333
            ]);
334
335
        $this->kernel
336
            ->expects($this->once())
337
            ->method('getContainer')
338
            ->willReturn($container);
339
340
        $this->router
341
            ->expects($this->once())
342
            ->method('generate')
343
            ->with('atlassian_connect_unlicensed')
344
            ->willReturn('http://website.com');
345
346
        $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

346
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
347
            $this->kernel,
348
            $request,
349
            KernelInterface::MASTER_REQUEST
350
        );
351
352
        $this->listener->onKernelRequest($event);
353
        $this->assertNotNull($event->getResponse());
354
        $response = $event->getResponse();
355
        $this->assertInstanceOf(RedirectResponse::class, $response);
356
        $this->assertEquals('http://website.com', $response->getTargetUrl());
357
    }
358
359
    public function testThrowsException(): void
360
    {
361
        $request = new Request(
362
            ['lic' => 'not_active'],
363
            [],
364
            [
365
                '_route' => 'route',
366
                'requires_license' => true,
367
            ]
368
        );
369
370
        $this->kernel
371
            ->expects($this->once())
372
            ->method('getEnvironment')
373
            ->willReturn('prod');
374
375
        $user = new Tenant();
376
        $user->setClientKey('key');
377
378
        $this->tokenStorage
379
            ->expects($this->once())
380
            ->method('getToken')
381
            ->willThrowException(new \Exception());
382
383
        $this->router
384
            ->expects($this->once())
385
            ->method('generate')
386
            ->with('atlassian_connect_unlicensed')
387
            ->willReturn('http://website.com');
388
389
        $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

389
        $event = /** @scrutinizer ignore-deprecated */ new GetResponseEvent(
Loading history...
390
            $this->kernel,
391
            $request,
392
            KernelInterface::MASTER_REQUEST
393
        );
394
395
        $this->listener->onKernelRequest($event);
396
        $this->assertNotNull($event->getResponse());
397
        $response = $event->getResponse();
398
        $this->assertInstanceOf(RedirectResponse::class, $response);
399
        $this->assertEquals('http://website.com', $response->getTargetUrl());
400
    }
401
}
402
403
class TestUser implements UserInterface
404
{
405
    public function getRoles()
406
    {
407
    }
408
409
    public function getPassword()
410
    {
411
    }
412
413
    public function getSalt()
414
    {
415
    }
416
417
    public function getUsername()
418
    {
419
    }
420
421
    public function eraseCredentials()
422
    {
423
    }
424
}
425