Completed
Push — master ( 4be4b5...14df4a )
by Guillaume
07:45 queued 07:45
created

JwtManagerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 199
Duplicated Lines 95.48 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 9
dl 190
loc 199
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B testGetToken() 37 37 1
B testGetTokenWithTokenKeyOption() 37 37 1
A testGetTokenShouldGetNewTokenIfCachedTokenIsNotValid() 58 58 1
A testGetTokenShouldUseTheCachedTokenIfItIsValid() 58 58 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * testGetToken.
21
     */
22 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...
23
    {
24
        $mockHandler = new MockHandler([
25
            function (RequestInterface $request) {
26
27
                $this->assertTrue($request->hasHeader('timeout'));
28
                $this->assertEquals(
29
                    3,
30
                    $request->getHeaderLine('timeout')
31
                );
32
33
                return new Response(
34
                    200,
35
                    ['Content-Type' => 'application/json'],
36
                    json_encode(['token' => '1453720507'])
37
                );
38
            },
39
        ]);
40
41
        $handler = HandlerStack::create($mockHandler);
42
43
        $authClient = new Client([
44
            'handler' => $handler,
45
        ]);
46
47
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
48
49
        $jwtManager = new JwtManager(
50
            $authClient,
51
            $authStrategy,
52
            ['token_url' => '/api/token', 'timeout' => 3]
53
        );
54
        $token = $jwtManager->getJwtToken();
55
56
        $this->assertInstanceOf(JwtToken::class, $token);
57
        $this->assertEquals('1453720507', $token->getToken());
58
    }
59
60 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...
61
    {
62
        $mockHandler = new MockHandler([
63
            function (RequestInterface $request) {
64
65
                $this->assertTrue($request->hasHeader('timeout'));
66
                $this->assertEquals(
67
                    3,
68
                    $request->getHeaderLine('timeout')
69
                );
70
71
                return new Response(
72
                    200,
73
                    ['Content-Type' => 'application/json'],
74
                    json_encode(['tokenkey' => '1453720507'])
75
                );
76
            },
77
        ]);
78
79
        $handler = HandlerStack::create($mockHandler);
80
81
        $authClient = new Client([
82
            'handler' => $handler,
83
        ]);
84
85
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
86
87
        $jwtManager = new JwtManager(
88
            $authClient,
89
            $authStrategy,
90
            ['token_url' => '/api/token', 'timeout' => 3, 'token_key' => 'tokenkey']
91
        );
92
        $token = $jwtManager->getJwtToken();
93
94
        $this->assertInstanceOf(JwtToken::class, $token);
95
        $this->assertEquals('1453720507', $token->getToken());
96
    }
97
98 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...
99
    {
100
        $mockHandler = new MockHandler(
101
            [
102
                function (RequestInterface $request) {
103
104
                    $this->assertTrue($request->hasHeader('timeout'));
105
                    $this->assertEquals(
106
                        3,
107
                        $request->getHeaderLine('timeout')
108
                    );
109
110
                    return new Response(
111
                        200,
112
                        ['Content-Type' => 'application/json'],
113
                        json_encode(['token' => '1453720507'])
114
                    );
115
                },
116
                function (RequestInterface $request) {
117
118
                    $this->assertTrue($request->hasHeader('timeout'));
119
                    $this->assertEquals(
120
                        3,
121
                        $request->getHeaderLine('timeout')
122
                    );
123
124
                    return new Response(
125
                        200,
126
                        ['Content-Type' => 'application/json'],
127
                        json_encode(['token' => 'foo123'])
128
                    );
129
                },
130
            ]
131
        );
132
133
        $handler = HandlerStack::create($mockHandler);
134
135
        $authClient = new Client([
136
            'handler' => $handler,
137
        ]);
138
139
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
140
141
        $jwtManager = new JwtManager(
142
            $authClient,
143
            $authStrategy,
144
            ['token_url' => '/api/token', 'timeout' => 3]
145
        );
146
        $token = $jwtManager->getJwtToken();
147
148
        $this->assertInstanceOf(JwtToken::class, $token);
149
        $this->assertEquals('1453720507', $token->getToken());
150
151
        $token = $jwtManager->getJwtToken();
152
153
        $this->assertInstanceOf(JwtToken::class, $token);
154
        $this->assertEquals('foo123', $token->getToken());
155
    }
156
157 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...
158
    {
159
        $mockHandler = new MockHandler(
160
            [
161
                function (RequestInterface $request) {
162
163
                    $this->assertTrue($request->hasHeader('timeout'));
164
                    $this->assertEquals(
165
                        3,
166
                        $request->getHeaderLine('timeout')
167
                    );
168
169
                    return new Response(
170
                        200,
171
                        ['Content-Type' => 'application/json'],
172
                        json_encode(['token' => '1453720507', 'expires_in' => 3600])
173
                    );
174
                },
175
                function (RequestInterface $request) {
176
177
                    $this->assertTrue($request->hasHeader('timeout'));
178
                    $this->assertEquals(
179
                        3,
180
                        $request->getHeaderLine('timeout')
181
                    );
182
183
                    return new Response(
184
                        200,
185
                        ['Content-Type' => 'application/json'],
186
                        json_encode(['token' => 'foo123'])
187
                    );
188
                },
189
            ]
190
        );
191
192
        $handler = HandlerStack::create($mockHandler);
193
194
        $authClient = new Client([
195
            'handler' => $handler,
196
        ]);
197
198
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
199
200
        $jwtManager = new JwtManager(
201
            $authClient,
202
            $authStrategy,
203
            ['token_url' => '/api/token', 'timeout' => 3]
204
        );
205
        $token = $jwtManager->getJwtToken();
206
207
        $this->assertInstanceOf(JwtToken::class, $token);
208
        $this->assertEquals('1453720507', $token->getToken());
209
210
        $token = $jwtManager->getJwtToken();
211
212
        $this->assertInstanceOf(JwtToken::class, $token);
213
        $this->assertEquals('1453720507', $token->getToken());
214
    }
215
}
216