Completed
Push — master ( c8ea17...488f5d )
by Guillaume
02:51 queued 01:29
created

JwtManagerTest::testGetTokenExpiredKeyException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 9.0036
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Eljam\GuzzleJwt\Tests\Manager;
4
5
use Eljam\GuzzleJwt\JwtToken;
6
use Eljam\GuzzleJwt\Manager\JwtManager;
7
use Eljam\GuzzleJwt\Strategy\Auth\QueryAuthStrategy;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\HandlerStack;
10
use GuzzleHttp\Handler\MockHandler;
11
use GuzzleHttp\Psr7\Response;
12
use Psr\Http\Message\RequestInterface;
13
14
/**
15
 * @author Guillaume Cavavana <[email protected]>
16
 */
17
class JwtManagerTest extends \PHPUnit_Framework_TestCase
18
{
19
20
    /**
21
     * testGetTokenExpiredKeyException
22
     */
23
    public function testGetTokenExpiredKeyException()
24
    {
25
        $mockHandler = new MockHandler([
26
            function (RequestInterface $request) {
27
28
                $this->assertTrue($request->hasHeader('timeout'));
29
                $this->assertEquals(
30
                    3,
31
                    $request->getHeaderLine('timeout')
32
                );
33
34
        $jsonPayload = <<<EOF
35
            {
36
                "status": "success",
37
                "message": "Login successful",
38
                "payload": {
39
                    "token": "1453720507"
40
                },
41
                "expires_in": 3600
42
            }
43
EOF;
44
45
                return new Response(
46
                    200,
47
                    ['Content-Type' => 'application/json'],
48
                    $jsonPayload
49
                );
50
            },
51
        ]);
52
53
        $handler = HandlerStack::create($mockHandler);
54
55
        $authClient = new Client([
56
            'handler' => $handler,
57
        ]);
58
59
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
60
61
        $jwtManager = new JwtManager(
62
            $authClient,
63
            $authStrategy,
64
            null,
65
            [
66
                'token_url' => '/api/token',
67
                'timeout' => 3,
68
                'token_key' => 'token',
69
                'expire_key' => 'expires_in'
70
            ]
71
        );
72
73
        $this->setExpectedException('Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException');
74
75
        $token = $jwtManager->getJwtToken();
0 ignored issues
show
Unused Code introduced by
$token is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
    }
77
78
    /**
79
     * testGetTokenWithSublevelResponse
80
     */
81 View Code Duplication
    public function testGetTokenWithSublevelResponse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $mockHandler = new MockHandler([
84
            function (RequestInterface $request) {
85
86
                $this->assertTrue($request->hasHeader('timeout'));
87
                $this->assertEquals(
88
                    3,
89
                    $request->getHeaderLine('timeout')
90
                );
91
92
        $jsonPayload = <<<EOF
93
            {
94
                "status": "success",
95
                "message": "Login successful",
96
                "payload": {
97
                    "token": "1453720507"
98
                },
99
                "expires_in": 3600
100
            }
101
EOF;
102
103
                return new Response(
104
                    200,
105
                    ['Content-Type' => 'application/json'],
106
                    $jsonPayload
107
                );
108
            },
109
        ]);
110
111
        $handler = HandlerStack::create($mockHandler);
112
113
        $authClient = new Client([
114
            'handler' => $handler,
115
        ]);
116
117
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
118
119
        $jwtManager = new JwtManager(
120
            $authClient,
121
            $authStrategy,
122
            null,
123
            [
124
                'token_url' => '/api/token',
125
                'timeout' => 3,
126
                'token_key' => 'payload.token',
127
                'expire_key' => 'expires_in'
128
            ]
129
        );
130
        $token = $jwtManager->getJwtToken();
131
132
        $this->assertInstanceOf(JwtToken::class, $token);
133
        $this->assertEquals('1453720507', $token->getToken());
134
    }
135
136
    /**
137
     * testGetToken.
138
     */
139 View Code Duplication
    public function testGetToken()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $mockHandler = new MockHandler([
142
            function (RequestInterface $request) {
143
144
                $this->assertTrue($request->hasHeader('timeout'));
145
                $this->assertEquals(
146
                    3,
147
                    $request->getHeaderLine('timeout')
148
                );
149
150
                return new Response(
151
                    200,
152
                    ['Content-Type' => 'application/json'],
153
                    json_encode(['token' => '1453720507', 'expires_in' => 3600])
154
                );
155
            },
156
        ]);
157
158
        $handler = HandlerStack::create($mockHandler);
159
160
        $authClient = new Client([
161
            'handler' => $handler,
162
        ]);
163
164
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
165
166
        $jwtManager = new JwtManager(
167
            $authClient,
168
            $authStrategy,
169
            null,
170
            ['token_url' => '/api/token', 'timeout' => 3]
171
        );
172
        $token = $jwtManager->getJwtToken();
173
174
        $this->assertInstanceOf(JwtToken::class, $token);
175
        $this->assertEquals('1453720507', $token->getToken());
176
    }
177
178 View Code Duplication
    public function testGetTokenWithTokenKeyOption()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    {
180
        $mockHandler = new MockHandler([
181
            function (RequestInterface $request) {
182
183
                $this->assertTrue($request->hasHeader('timeout'));
184
                $this->assertEquals(
185
                    3,
186
                    $request->getHeaderLine('timeout')
187
                );
188
189
                return new Response(
190
                    200,
191
                    ['Content-Type' => 'application/json'],
192
                    json_encode(['tokenkey' => '1453720507'])
193
                );
194
            },
195
        ]);
196
197
        $handler = HandlerStack::create($mockHandler);
198
199
        $authClient = new Client([
200
            'handler' => $handler,
201
        ]);
202
203
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
204
205
        $jwtManager = new JwtManager(
206
            $authClient,
207
            $authStrategy,
208
            null,
209
            ['token_url' => '/api/token', 'timeout' => 3, 'token_key' => 'tokenkey']
210
        );
211
        $token = $jwtManager->getJwtToken();
212
213
        $this->assertInstanceOf(JwtToken::class, $token);
214
        $this->assertEquals('1453720507', $token->getToken());
215
    }
216
217 View Code Duplication
    public function testGetTokenShouldGetNewTokenIfCachedTokenIsNotValid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
    {
219
        $mockHandler = new MockHandler(
220
            [
221
                function (RequestInterface $request) {
222
223
                    $this->assertTrue($request->hasHeader('timeout'));
224
                    $this->assertEquals(
225
                        3,
226
                        $request->getHeaderLine('timeout')
227
                    );
228
229
                    return new Response(
230
                        200,
231
                        ['Content-Type' => 'application/json'],
232
                        json_encode(['token' => '1453720507'])
233
                    );
234
                },
235
                function (RequestInterface $request) {
236
237
                    $this->assertTrue($request->hasHeader('timeout'));
238
                    $this->assertEquals(
239
                        3,
240
                        $request->getHeaderLine('timeout')
241
                    );
242
243
                    return new Response(
244
                        200,
245
                        ['Content-Type' => 'application/json'],
246
                        json_encode(['token' => 'foo123'])
247
                    );
248
                },
249
            ]
250
        );
251
252
        $handler = HandlerStack::create($mockHandler);
253
254
        $authClient = new Client([
255
            'handler' => $handler,
256
        ]);
257
258
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
259
260
        $jwtManager = new JwtManager(
261
            $authClient,
262
            $authStrategy,
263
            null,
264
            ['token_url' => '/api/token', 'timeout' => 3]
265
        );
266
        $token = $jwtManager->getJwtToken();
267
268
        $this->assertInstanceOf(JwtToken::class, $token);
269
        $this->assertEquals('1453720507', $token->getToken());
270
271
        $token = $jwtManager->getJwtToken();
272
273
        $this->assertInstanceOf(JwtToken::class, $token);
274
        $this->assertEquals('foo123', $token->getToken());
275
    }
276
277 View Code Duplication
    public function testGetTokenShouldUseTheCachedTokenIfItIsValid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
278
    {
279
        $mockHandler = new MockHandler(
280
            [
281
                function (RequestInterface $request) {
282
283
                    $this->assertTrue($request->hasHeader('timeout'));
284
                    $this->assertEquals(
285
                        3,
286
                        $request->getHeaderLine('timeout')
287
                    );
288
289
                    return new Response(
290
                        200,
291
                        ['Content-Type' => 'application/json'],
292
                        json_encode(['token' => '1453720507', 'expires_in' => 3600])
293
                    );
294
                },
295
                function (RequestInterface $request) {
296
297
                    $this->assertTrue($request->hasHeader('timeout'));
298
                    $this->assertEquals(
299
                        3,
300
                        $request->getHeaderLine('timeout')
301
                    );
302
303
                    return new Response(
304
                        200,
305
                        ['Content-Type' => 'application/json'],
306
                        json_encode(['token' => 'foo123'])
307
                    );
308
                },
309
            ]
310
        );
311
312
        $handler = HandlerStack::create($mockHandler);
313
314
        $authClient = new Client([
315
            'handler' => $handler,
316
        ]);
317
318
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
319
320
        $jwtManager = new JwtManager(
321
            $authClient,
322
            $authStrategy,
323
            null,
324
            ['token_url' => '/api/token', 'timeout' => 3]
325
        );
326
        $token = $jwtManager->getJwtToken();
327
328
        $this->assertInstanceOf(JwtToken::class, $token);
329
        $this->assertEquals('1453720507', $token->getToken());
330
331
        $token = $jwtManager->getJwtToken();
332
333
        $this->assertInstanceOf(JwtToken::class, $token);
334
        $this->assertEquals('1453720507', $token->getToken());
335
    }
336
337
    public function testGetTokenShouldUseTheCachedTokenIfItIsValidBasedOnExpField()
338
    {
339
        $jwtToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
340
            . '.eyJleHAiOiIzMjUwMzY4MDAwMCJ9'
341
            . '.k4YJmJooaa9B4pAM_U8Pi-4ss6RdKFtj9iQqLIAndVA';
342
343
        $mockHandler = new MockHandler(
344
            [
345
                function (RequestInterface $request) use ($jwtToken) {
346
347
                    $this->assertTrue($request->hasHeader('timeout'));
348
                    $this->assertEquals(
349
                        3,
350
                        $request->getHeaderLine('timeout')
351
                    );
352
353
                    return new Response(
354
                        200,
355
                        ['Content-Type' => 'application/json'],
356
                        json_encode(['token' => $jwtToken])
357
                    );
358
                },
359
                function (RequestInterface $request) {
360
361
                    $this->assertTrue($request->hasHeader('timeout'));
362
                    $this->assertEquals(
363
                        3,
364
                        $request->getHeaderLine('timeout')
365
                    );
366
367
                    return new Response(
368
                        200,
369
                        ['Content-Type' => 'application/json'],
370
                        json_encode(['token' => uniqid('token', true)])
371
                    );
372
                },
373
            ]
374
        );
375
376
        $handler = HandlerStack::create($mockHandler);
377
378
        $authClient = new Client([
379
            'handler' => $handler,
380
        ]);
381
382
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
383
384
        $jwtManager = new JwtManager(
385
            $authClient,
386
            $authStrategy,
387
            null,
388
            ['token_url' => '/api/token', 'timeout' => 3]
389
        );
390
        $token = $jwtManager->getJwtToken();
391
392
        $this->assertInstanceOf(JwtToken::class, $token);
393
        $this->assertEquals($jwtToken, $token->getToken());
394
395
        $token = $jwtManager->getJwtToken();
396
397
        $this->assertInstanceOf(JwtToken::class, $token);
398
        $this->assertEquals($jwtToken, $token->getToken());
399
    }
400
}
401