Completed
Push — develop ( 82de8f...bd9a33 )
by Wisoot
02:14
created

JwtServiceTest::testGetUserIdFromToken()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 16
rs 9.4285
cc 2
eloc 10
nc 1
nop 0
1
<?php
2
3
use Carbon\Carbon;
4
use Illuminate\Support\Facades\Config;
5
use WWON\JwtGuard\Contract\TokenManager;
6
use WWON\JwtGuard\JwtService;
7
8
class JwtServiceTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    /**
12
     * @var Mockery\MockInterface
13
     */
14
    private $tokenManager;
15
    
16
    /**
17
     * @var JwtService
18
     */
19
    private $jwtService;
20
21
    /**
22
     * setUp method
23
     */
24
    public function setUp()
25
    {
26
        parent::setUp();
27
28
        Config::shouldReceive('get')
29
            ->once()
30
            ->with('jwt.secret')
31
            ->andReturn('abcdefg');
32
33
        Config::shouldReceive('get')
34
            ->once()
35
            ->with('jwt.leeway')
36
            ->andReturn(0);
37
38
        $this->tokenManager = Mockery::mock(TokenManager::class);
39
        $this->jwtService = new JwtService($this->tokenManager);
40
    }
41
42
    /**
43
     * tearDown method
44
     */
45
    public function tearDown()
46
    {
47
        unset($this->jwtService);
48
49
        parent::tearDown();
50
    }
51
52
    /**
53
     * testGetTokenForUser method
54
     */
55
    public function testGetTokenForUser()
56
    {
57
        $now = Carbon::now()->timestamp;
58
59
        $token = $this->getToken();
60
61
        $items = explode('.', $token);
62
        $claimBody = json_decode(base64_decode($items[1]));
63
64
        $this->assertEquals('http://www.test.com', $claimBody->iss);
65
        $this->assertEquals($now, $claimBody->iat);
66
        $this->assertEquals($now + 6000, $claimBody->exp);
67
        $this->assertEquals($now + 6000, $claimBody->nat);
68
    }
69
70
    /**
71
     * testGetUserIdFromToken method
72
     */
73 View Code Duplication
    public function testGetUserIdFromToken()
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...
74
    {
75
        $token = $this->getToken();
76
77
        $this->tokenManager->shouldReceive('check')
78
            ->once()
79
            ->with(Mockery::on(function($claim) {
80
                return $claim->sub == 5
81
                    && $claim->iss == 'http://www.test.com';
82
            }))
83
            ->andReturn(true);
84
85
        $userId = $this->jwtService->getUserIdFromToken($token);
86
87
        $this->assertEquals(5, $userId);
88
    }
89
90
    /**
91
     * testGetUserIdFromTokenWithInvalidClaim method
92
     */
93 View Code Duplication
    public function testGetUserIdFromTokenWithInvalidClaim()
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...
94
    {
95
        $token = $this->getToken();
96
97
        $this->tokenManager->shouldReceive('check')
98
            ->once()
99
            ->with(Mockery::on(function($claim) {
100
                return $claim->sub == 5
101
                && $claim->iss == 'http://www.test.com';
102
            }))
103
            ->andReturn(false);
104
105
        $this->setExpectedException(\WWON\JwtGuard\Exceptions\InvalidTokenException::class);
106
107
        $userId = $this->jwtService->getUserIdFromToken($token);
0 ignored issues
show
Unused Code introduced by
$userId 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...
108
    }
109
110
    /**
111
     * getToken method
112
     *
113
     * @param int $ttl
114
     * @return string
115
     */
116
    protected function getToken($ttl = 100)
117
    {
118
        Config::shouldReceive('get')
119
            ->once()
120
            ->with('app.url')
121
            ->andReturn('http://www.test.com');
122
123
        Config::shouldReceive('get')
124
            ->once()
125
            ->with('jwt.ttl')
126
            ->andReturn($ttl);
127
128
        Config::shouldReceive('get')
129
            ->once()
130
            ->with('jwt.ttl')
131
            ->andReturn($ttl);
132
133
        Config::shouldReceive('get')
134
            ->once()
135
            ->with('jwt.leeway')
136
            ->andReturn(0);
137
138
        Config::shouldReceive('get')
139
            ->once()
140
            ->with('jwt.algo')
141
            ->andReturn('HS256');
142
143
        $this->tokenManager->shouldReceive('add')
144
            ->once()
145
            ->with(Mockery::on(function($claim) {
146
                return $claim->sub == 5
147
                && $claim->iss == 'http://www.test.com';
148
            }));
149
150
        $token = $this->jwtService->getTokenForUser(new User());
151
152
        return $token;
153
    }
154
155
}