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

JwtTokenTest::willDecodeClaimsOnConstruction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
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