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

testGetTokenShouldGetNewTokenIfCachedTokenIsNotValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 34

Duplication

Lines 58
Ratio 100 %

Importance

Changes 0
Metric Value
dl 58
loc 58
rs 9.639
c 0
b 0
f 0
cc 1
eloc 34
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
     * 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