Failed Conditions
Push — master ( f4dd73...badc21 )
by Luís
15s
created

BuilderTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 96
dl 0
loc 215
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeDependencies() 0 5 1
A audienceShouldBeFormattedAsArrayWhenMultipleValuesAreUsed() 0 23 1
A claimsMustBeFormattedWhileEncoding() 0 45 1
A getTokenShouldReturnACompletelyConfigureToken() 0 42 1
A withClaimShouldRaiseExceptionWhenTryingToConfigureARegisteredClaim() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Lcobucci\JWT\Token;
5
6
use DateTimeImmutable;
7
use InvalidArgumentException;
8
use Lcobucci\Jose\Parsing\Encoder;
9
use Lcobucci\JWT\Signer;
10
use Lcobucci\JWT\Signer\Key;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use PHPUnit\Framework\TestCase;
13
14
final class BuilderTest extends TestCase
15
{
16
    /**
17
     * @var Encoder|MockObject
18
     */
19
    protected $encoder;
20
21
    /**
22
     * @var Signer|MockObject
23
     */
24
    private $signer;
25
26
    /**
27
     * @before
28
     */
29
    public function initializeDependencies(): void
30
    {
31
        $this->encoder = $this->createMock(Encoder::class);
32
        $this->signer  = $this->createMock(Signer::class);
33
        $this->signer->method('getAlgorithmId')->willReturn('RS256');
34
    }
35
36
    /**
37
     * @test
38
     *
39
     * @covers \Lcobucci\JWT\Token\Builder::__construct
40
     * @covers \Lcobucci\JWT\Token\Builder::withClaim
41
     */
42
    public function withClaimShouldRaiseExceptionWhenTryingToConfigureARegisteredClaim(): void
43
    {
44
        $builder = new Builder($this->encoder);
45
46
        $this->expectException(InvalidArgumentException::class);
47
        $this->expectExceptionMessage('You should use the correct methods to set registered claims');
48
49
        $builder->withClaim(RegisteredClaims::ISSUER, 'me');
50
    }
51
52
    /**
53
     * @test
54
     *
55
     * @covers \Lcobucci\JWT\Token\Builder::__construct
56
     * @covers \Lcobucci\JWT\Token\Builder::getToken
57
     * @covers \Lcobucci\JWT\Token\Builder::encode
58
     * @covers \Lcobucci\JWT\Token\Builder::formatClaims
59
     * @covers \Lcobucci\JWT\Token\Builder::convertDate
60
     * @covers \Lcobucci\JWT\Token\Builder::withClaim
61
     * @covers \Lcobucci\JWT\Token\Builder::withHeader
62
     * @covers \Lcobucci\JWT\Token\Builder::identifiedBy
63
     * @covers \Lcobucci\JWT\Token\Builder::setClaim
64
     * @covers \Lcobucci\JWT\Token\Builder::issuedBy
65
     * @covers \Lcobucci\JWT\Token\Builder::issuedAt
66
     * @covers \Lcobucci\JWT\Token\Builder::relatedTo
67
     * @covers \Lcobucci\JWT\Token\Builder::canOnlyBeUsedAfter
68
     * @covers \Lcobucci\JWT\Token\Builder::expiresAt
69
     * @covers \Lcobucci\JWT\Token\Builder::permittedFor
70
     *
71
     * @uses \Lcobucci\JWT\Signer\Key
72
     * @uses \Lcobucci\JWT\Token\Plain
73
     * @uses \Lcobucci\JWT\Token\Signature
74
     * @uses \Lcobucci\JWT\Token\DataSet
75
     */
76
    public function claimsMustBeFormattedWhileEncoding(): void
77
    {
78
        $issuedAt   = new DateTimeImmutable('@1487285080');
79
        $notBefore  = DateTimeImmutable::createFromFormat('U.u', '1487285080.000123');
80
        $expiration = DateTimeImmutable::createFromFormat('U.u', '1487285080.123456');
81
82
        self::assertInstanceOf(DateTimeImmutable::class, $notBefore);
83
        self::assertInstanceOf(DateTimeImmutable::class, $expiration);
84
85
        $headers = ['typ' => 'JWT', 'alg' => 'RS256', 'userId' => 2];
86
        $claims  = [
87
            RegisteredClaims::ID => '123456',
88
            RegisteredClaims::ISSUER => 'https://issuer.com',
89
            RegisteredClaims::ISSUED_AT => 1487285080,
90
            RegisteredClaims::NOT_BEFORE => '1487285080.000123',
91
            RegisteredClaims::EXPIRATION_TIME => '1487285080.123456',
92
            RegisteredClaims::SUBJECT => 'subject',
93
            RegisteredClaims::AUDIENCE => 'test1',
94
            'test' => 123,
95
        ];
96
97
        $this->signer->method('sign')->willReturn('testing');
98
99
        $this->encoder->expects(self::exactly(2))
100
                     ->method('jsonEncode')
101
                      ->withConsecutive([self::identicalTo($headers)], [self::identicalTo($claims)])
102
                      ->willReturnOnConsecutiveCalls('1', '2');
103
104
        $this->encoder->expects(self::exactly(3))
105
                      ->method('base64UrlEncode')
106
                      ->withConsecutive(['1'], ['2'], ['testing'])
107
                      ->willReturnOnConsecutiveCalls('1', '2', '3');
108
109
        $builder = new Builder($this->encoder);
110
111
        $builder->identifiedBy('123456')
112
                ->issuedBy('https://issuer.com')
113
                ->issuedAt($issuedAt)
114
                ->canOnlyBeUsedAfter($notBefore)
115
                ->expiresAt($expiration)
116
                ->relatedTo('subject')
117
                ->permittedFor('test1')
118
                ->withClaim('test', 123)
119
                ->withHeader('userId', 2)
120
                ->getToken($this->signer, new Key('123'));
121
    }
122
123
    /**
124
     * @test
125
     *
126
     * @covers \Lcobucci\JWT\Token\Builder::__construct
127
     * @covers \Lcobucci\JWT\Token\Builder::encode
128
     * @covers \Lcobucci\JWT\Token\Builder::formatClaims
129
     * @covers \Lcobucci\JWT\Token\Builder::setClaim
130
     * @covers \Lcobucci\JWT\Token\Builder::permittedFor
131
     *
132
     * @uses \Lcobucci\JWT\Token\Builder::getToken
133
     * @uses \Lcobucci\JWT\Signer\Key
134
     * @uses \Lcobucci\JWT\Token\Plain
135
     * @uses \Lcobucci\JWT\Token\Signature
136
     * @uses \Lcobucci\JWT\Token\DataSet
137
     */
138
    public function audienceShouldBeFormattedAsArrayWhenMultipleValuesAreUsed(): void
139
    {
140
        $headers = ['typ' => 'JWT', 'alg' => 'RS256'];
141
        $claims  = [RegisteredClaims::AUDIENCE => ['test1', 'test2']];
142
143
        $this->signer->method('sign')->willReturn('testing');
144
145
        $this->encoder->expects(self::exactly(2))
146
                     ->method('jsonEncode')
147
                      ->withConsecutive([self::identicalTo($headers)], [self::identicalTo($claims)])
148
                      ->willReturnOnConsecutiveCalls('1', '2');
149
150
        $this->encoder->expects(self::exactly(3))
151
                      ->method('base64UrlEncode')
152
                      ->withConsecutive(['1'], ['2'], ['testing'])
153
                      ->willReturnOnConsecutiveCalls('1', '2', '3');
154
155
        $builder = new Builder($this->encoder);
156
157
        $builder->permittedFor('test1')
158
                ->permittedFor('test2')
159
                ->permittedFor('test2') // should not be added since it's duplicated
160
                ->getToken($this->signer, new Key('123'));
161
    }
162
163
    /**
164
     * @test
165
     *
166
     * @covers \Lcobucci\JWT\Token\Builder::__construct
167
     * @covers \Lcobucci\JWT\Token\Builder::getToken
168
     * @covers \Lcobucci\JWT\Token\Builder::encode
169
     * @covers \Lcobucci\JWT\Token\Builder::formatClaims
170
     * @covers \Lcobucci\JWT\Token\Builder::convertDate
171
     * @covers \Lcobucci\JWT\Token\Builder::withClaim
172
     * @covers \Lcobucci\JWT\Token\Builder::withHeader
173
     * @covers \Lcobucci\JWT\Token\Builder::identifiedBy
174
     * @covers \Lcobucci\JWT\Token\Builder::setClaim
175
     * @covers \Lcobucci\JWT\Token\Builder::issuedBy
176
     * @covers \Lcobucci\JWT\Token\Builder::issuedAt
177
     * @covers \Lcobucci\JWT\Token\Builder::relatedTo
178
     * @covers \Lcobucci\JWT\Token\Builder::canOnlyBeUsedAfter
179
     * @covers \Lcobucci\JWT\Token\Builder::expiresAt
180
     * @covers \Lcobucci\JWT\Token\Builder::permittedFor
181
     *
182
     * @uses \Lcobucci\JWT\Signer\Key
183
     * @uses \Lcobucci\JWT\Token\Plain
184
     * @uses \Lcobucci\JWT\Token\Signature
185
     * @uses \Lcobucci\JWT\Token\DataSet
186
     */
187
    public function getTokenShouldReturnACompletelyConfigureToken(): void
188
    {
189
        $issuedAt   = new DateTimeImmutable('@1487285080');
190
        $notBefore  = DateTimeImmutable::createFromFormat('U.u', '1487285080.000123');
191
        $expiration = DateTimeImmutable::createFromFormat('U.u', '1487285080.123456');
192
193
        self::assertInstanceOf(DateTimeImmutable::class, $notBefore);
194
        self::assertInstanceOf(DateTimeImmutable::class, $expiration);
195
196
        $this->encoder->expects(self::exactly(2))
197
                     ->method('jsonEncode')
198
                      ->willReturnOnConsecutiveCalls('1', '2');
199
200
        $this->encoder->expects(self::exactly(3))
201
                      ->method('base64UrlEncode')
202
                      ->willReturnOnConsecutiveCalls('1', '2', '3');
203
204
        $builder = new Builder($this->encoder);
205
        $token   = $builder->identifiedBy('123456')
206
                           ->issuedBy('https://issuer.com')
207
                           ->issuedAt($issuedAt)
208
                           ->canOnlyBeUsedAfter($notBefore)
209
                           ->expiresAt($expiration)
210
                           ->relatedTo('subject')
211
                           ->permittedFor('test1')
212
                           ->permittedFor('test2')
213
                           ->withClaim('test', 123)
214
                           ->withHeader('userId', 2)
215
                           ->getToken($this->signer, new Key('123'));
216
217
        self::assertSame('JWT', $token->headers()->get('typ'));
218
        self::assertSame('RS256', $token->headers()->get('alg'));
219
        self::assertSame(2, $token->headers()->get('userId'));
220
        self::assertSame(123, $token->claims()->get('test'));
221
        self::assertSame($issuedAt, $token->claims()->get(RegisteredClaims::ISSUED_AT));
222
        self::assertSame($notBefore, $token->claims()->get(RegisteredClaims::NOT_BEFORE));
223
        self::assertSame($expiration, $token->claims()->get(RegisteredClaims::EXPIRATION_TIME));
224
        self::assertSame('123456', $token->claims()->get(RegisteredClaims::ID));
225
        self::assertSame('https://issuer.com', $token->claims()->get(RegisteredClaims::ISSUER));
226
        self::assertSame('subject', $token->claims()->get(RegisteredClaims::SUBJECT));
227
        self::assertSame(['test1', 'test2'], $token->claims()->get(RegisteredClaims::AUDIENCE));
228
        self::assertSame('3', (string) $token->signature());
229
    }
230
}
231