Completed
Push — master ( ff92f6...3ef1d3 )
by John
09:08
created

JwtTokenTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 5
dl 21
loc 126
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A willDecodeClaimsOnConstruction() 0 9 1
A willDecodeHeadersOnConstruction() 0 8 1
A willDecodeWithArray() 0 19 1
A willResultNullWhenKidOmitted() 0 5 1
A canGetKidWhenPresent() 0 5 1
A canGetSubject() 0 4 1
A willFailWhenSignatureValidationIsUnsuccessful() 10 10 1
A willNitFailWhenSignatureValidationIsSuccessful() 11 11 1
A willFailWhenTokenDoesNotContain3Segments() 0 6 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
 * This file is part of the KleijnWeb\JwtBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\JwtBundle\Tests\Jwt;
9
10
use KleijnWeb\JwtBundle\Jwt\JwtToken;
11
use KleijnWeb\JwtBundle\Jwt\SignatureValidator\SignatureValidator;
12
13
/**
14
 * @author John Kleijn <[email protected]>
15
 */
16
class JwtTokenTest extends \PHPUnit_Framework_TestCase
17
{
18
    // @codingStandardsIgnoreStart
19
    const EXAMPLE_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ';
20
    const KID_TOKEN     = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleU9uZSJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.Zhhr_UtsTzjrBZmi8AAgJYqCINiiEc45v94_3nvxW1A';
21
    // @codingStandardsIgnoreEnd
22
23
    /**
24
     * @test
25
     */
26
    public function willDecodeClaimsOnConstruction()
27
    {
28
        $token = new JwtToken(self::EXAMPLE_TOKEN);
29
        $this->assertSame([
30
            'sub'   => '1234567890',
31
            'name'  => 'John Doe',
32
            'admin' => true,
33
        ], $token->getClaims());
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function willDecodeHeadersOnConstruction()
40
    {
41
        $token = new JwtToken(self::EXAMPLE_TOKEN);
42
        $this->assertSame([
43
            'alg' => 'HS256',
44
            'typ' => 'JWT',
45
        ], $token->getHeader());
46
    }
47
48
    /**
49
     * @test
50
     * @return JwtToken
51
     */
52
    public function willDecodeWithArray()
53
    {
54
        $token = new JwtToken([
55
            'header' => [
56
                'alg' => 'HS256',
57
                'typ' => 'JWT',
58
            ],
59
            'claims' => [
60
                'sub'   => '1234567890',
61
                'name'  => 'John Doe',
62
                'admin' => true,
63
            ],
64
            'secret' => 'secret'
65
        ]);
66
67
        $this->assertSame(self::EXAMPLE_TOKEN, $token->getTokenString());
68
69
        return $token;
70
    }
71
72
73
    /**
74
     * @test
75
     */
76
    public function willResultNullWhenKidOmitted()
77
    {
78
        $token = new JwtToken(self::EXAMPLE_TOKEN);
79
        $this->assertNull($token->getKeyId());
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function canGetKidWhenPresent()
86
    {
87
        $token = new JwtToken(self::KID_TOKEN);
88
        $this->assertSame('keyOne', $token->getKeyId());
89
    }
90
91
    /**
92
     * @test
93
     */
94
    public function canGetSubject()
95
    {
96
        $this->assertSame('1234567890', $this->willDecodeWithArray()->getSubject());
97
    }
98
99
100
    /**
101
     * @test
102
     * @expectedException \InvalidArgumentException
103
     */
104 View Code Duplication
    public function willFailWhenSignatureValidationIsUnsuccessful()
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...
105
    {
106
        $validator = $this
107
            ->getMockBuilder(SignatureValidator::class)
108
            ->getMockForAbstractClass();
109
110
        $token = new JwtToken(self::EXAMPLE_TOKEN);
111
        $validator->expects($this->once())->method('isValid')->willReturn(false);
112
        $token->validateSignature('foobar', $validator);
113
    }
114
115
116
    /**
117
     * @test
118
     */
119 View Code Duplication
    public function willNitFailWhenSignatureValidationIsSuccessful()
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...
120
    {
121
        $validator = $this
122
            ->getMockBuilder(SignatureValidator::class)
123
            ->getMockForAbstractClass();
124
125
        $token = new JwtToken(self::EXAMPLE_TOKEN);
126
127
        $validator->expects($this->once())->method('isValid')->willReturn(true);
128
        $token->validateSignature('foobar', $validator);
129
    }
130
131
    /**
132
     * @test
133
     * @expectedException \InvalidArgumentException
134
     */
135
    public function willFailWhenTokenDoesNotContain3Segments()
136
    {
137
        $segments = explode('.', self::EXAMPLE_TOKEN);
138
139
        new JwtToken("{$segments[0]}.{$segments[1]}");
140
    }
141
}
142