Completed
Push — master ( a2a7f2...f21cde )
by John
9s
created

JwtTokenTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 18.97 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 2
cbo 5
dl 22
loc 116
rs 10

8 Methods

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