Completed
Push — master ( 13ada5...8d8bc4 )
by Luís
10s
created

getValidationConstraintsShouldReturnTheConfiguredValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Lcobucci\JWT;
5
6
use Lcobucci\Jose\Parsing;
7
use Lcobucci\JWT\Signer\Key;
8
use Lcobucci\JWT\Signer\None;
9
use Lcobucci\JWT\Token\Builder as BuilderImpl;
10
use Lcobucci\JWT\Validation\Constraint;
11
use PHPUnit\Framework\TestCase;
12
13
final class ConfigurationTest extends TestCase
14
{
15
    /**
16
     * @var Parser|\PHPUnit_Framework_MockObject_MockObject
17
     */
18
    private $parser;
19
20
    /**
21
     * @var Signer|\PHPUnit_Framework_MockObject_MockObject
22
     */
23
    private $signer;
24
25
    /**
26
     * @var Parsing\Encoder|\PHPUnit_Framework_MockObject_MockObject
27
     */
28
    private $encoder;
29
30
    /**
31
     * @var Parsing\Decoder|\PHPUnit_Framework_MockObject_MockObject
32
     */
33
    private $decoder;
34
35
    /**
36
     * @var Validator|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $validator;
39
40
    /**
41
     * @var Constraint|\PHPUnit_Framework_MockObject_MockObject
42
     */
43
    private $validationConstraints;
44
45
    /**
46
     * @before
47
     */
48
    public function createDependencies(): void
49
    {
50
        $this->signer                = $this->createMock(Signer::class);
51
        $this->encoder               = $this->createMock(Parsing\Encoder::class);
52
        $this->decoder               = $this->createMock(Parsing\Decoder::class);
53
        $this->parser                = $this->createMock(Parser::class);
54
        $this->validator             = $this->createMock(Validator::class);
55
        $this->validationConstraints = $this->createMock(Constraint::class);
56
    }
57
58
    /**
59
     * @test
60
     *
61
     * @covers \Lcobucci\JWT\Configuration::forAsymmetricSigner
62
     * @covers \Lcobucci\JWT\Configuration::__construct
63
     *
64
     * @uses \Lcobucci\JWT\Signer\Key
65
     */
66
    public function forAsymmetricSignerShouldConfigureSignerAndBothKeys(): void
67
    {
68
        $signingKey      = new Key('private');
69
        $verificationKey = new Key('public');
70
71
        $config = Configuration::forAsymmetricSigner($this->signer, $signingKey, $verificationKey);
72
73
        self::assertAttributeSame($this->signer, 'signer', $config);
74
        self::assertAttributeSame($signingKey, 'signingKey', $config);
75
        self::assertAttributeSame($verificationKey, 'verificationKey', $config);
76
    }
77
78
    /**
79
     * @test
80
     *
81
     * @covers \Lcobucci\JWT\Configuration::forSymmetricSigner
82
     * @covers \Lcobucci\JWT\Configuration::__construct
83
     *
84
     * @uses \Lcobucci\JWT\Signer\Key
85
     */
86
    public function forSymmetricSignerShouldConfigureSignerAndBothKeys(): void
87
    {
88
        $key    = new Key('private');
89
        $config = Configuration::forSymmetricSigner($this->signer, $key);
90
91
        self::assertAttributeSame($this->signer, 'signer', $config);
92
        self::assertAttributeSame($key, 'signingKey', $config);
93
        self::assertAttributeSame($key, 'verificationKey', $config);
94
    }
95
96
    /**
97
     * @test
98
     *
99
     * @covers \Lcobucci\JWT\Configuration::forUnsecuredSigner
100
     * @covers \Lcobucci\JWT\Configuration::__construct
101
     *
102
     * @uses \Lcobucci\JWT\Signer\Key
103
     */
104
    public function forUnsecuredSignerShouldConfigureSignerAndBothKeys(): void
105
    {
106
        $key    = new Key('');
107
        $config = Configuration::forUnsecuredSigner();
108
109
        self::assertAttributeInstanceOf(None::class, 'signer', $config);
110
        self::assertAttributeEquals($key, 'signingKey', $config);
111
        self::assertAttributeEquals($key, 'verificationKey', $config);
112
    }
113
114
    /**
115
     * @test
116
     *
117
     * @covers \Lcobucci\JWT\Configuration::createBuilder
118
     * @covers \Lcobucci\JWT\Configuration::getEncoder
119
     *
120
     * @uses \Lcobucci\JWT\Configuration::forUnsecuredSigner
121
     * @uses \Lcobucci\JWT\Configuration::__construct
122
     * @uses \Lcobucci\JWT\Token\Builder
123
     * @uses \Lcobucci\JWT\Token\Parser
124
     * @uses \Lcobucci\JWT\Signer\None
125
     * @uses \Lcobucci\JWT\Signer\Key
126
     */
127
    public function createBuilderShouldCreateABuilderWithDefaultEncoderAndClaimFactory(): void
128
    {
129
        $config  = Configuration::forUnsecuredSigner();
130
        $builder = $config->createBuilder();
131
132
        self::assertInstanceOf(BuilderImpl::class, $builder);
133
        self::assertAttributeNotSame($this->encoder, 'encoder', $builder);
134
    }
135
136
    /**
137
     * @test
138
     *
139
     * @covers \Lcobucci\JWT\Configuration::createBuilder
140
     * @covers \Lcobucci\JWT\Configuration::setEncoder
141
     * @covers \Lcobucci\JWT\Configuration::getEncoder
142
     *
143
     * @uses \Lcobucci\JWT\Configuration::forUnsecuredSigner
144
     * @uses \Lcobucci\JWT\Configuration::__construct
145
     * @uses \Lcobucci\JWT\Token\Builder
146
     * @uses \Lcobucci\JWT\Signer\None
147
     * @uses \Lcobucci\JWT\Signer\Key
148
     */
149
    public function createBuilderShouldCreateABuilderWithCustomizedEncoderAndClaimFactory(): void
150
    {
151
        $config = Configuration::forUnsecuredSigner();
152
        $config->setEncoder($this->encoder);
153
154
        $builder = $config->createBuilder();
155
156
        self::assertInstanceOf(BuilderImpl::class, $builder);
157
        self::assertAttributeSame($this->encoder, 'encoder', $builder);
158
    }
159
160
    /**
161
     * @test
162
     *
163
     * @covers \Lcobucci\JWT\Configuration::getParser
164
     * @covers \Lcobucci\JWT\Configuration::getDecoder
165
     *
166
     * @uses \Lcobucci\JWT\Configuration::forUnsecuredSigner
167
     * @uses \Lcobucci\JWT\Configuration::__construct
168
     * @uses \Lcobucci\JWT\Token\Parser
169
     * @uses \Lcobucci\JWT\Signer\None
170
     * @uses \Lcobucci\JWT\Signer\Key
171
     */
172
    public function getParserShouldReturnAParserWithDefaultDecoder(): void
173
    {
174
        $config = Configuration::forUnsecuredSigner();
175
        $parser = $config->getParser();
176
177
        self::assertInstanceOf(Parser::class, $parser);
178
        self::assertAttributeNotSame($this->decoder, 'decoder', $parser);
179
    }
180
181
    /**
182
     * @test
183
     *
184
     * @covers \Lcobucci\JWT\Configuration::getParser
185
     * @covers \Lcobucci\JWT\Configuration::setDecoder
186
     * @covers \Lcobucci\JWT\Configuration::getDecoder
187
     *
188
     * @uses \Lcobucci\JWT\Configuration::forUnsecuredSigner
189
     * @uses \Lcobucci\JWT\Configuration::__construct
190
     * @uses \Lcobucci\JWT\Token\Parser
191
     * @uses \Lcobucci\JWT\Signer\None
192
     * @uses \Lcobucci\JWT\Signer\Key
193
     */
194
    public function getParserShouldReturnAParserWithCustomizedDecoder(): void
195
    {
196
        $config = Configuration::forUnsecuredSigner();
197
        $config->setDecoder($this->decoder);
198
199
        $parser = $config->getParser();
200
201
        self::assertInstanceOf(Parser::class, $parser);
202
        self::assertAttributeSame($this->decoder, 'decoder', $parser);
203
    }
204
205
    /**
206
     * @test
207
     *
208
     * @covers \Lcobucci\JWT\Configuration::getParser
209
     * @covers \Lcobucci\JWT\Configuration::setParser
210
     *
211
     * @uses \Lcobucci\JWT\Configuration::forUnsecuredSigner
212
     * @uses \Lcobucci\JWT\Configuration::__construct
213
     * @uses \Lcobucci\JWT\Token\Parser
214
     * @uses \Lcobucci\JWT\Signer\None
215
     * @uses \Lcobucci\JWT\Signer\Key
216
     */
217
    public function getParserShouldNotCreateAnInstanceIfItWasConfigured(): void
218
    {
219
        $config = Configuration::forUnsecuredSigner();
220
        $config->setParser($this->parser);
221
222
        self::assertSame($this->parser, $config->getParser());
223
    }
224
225
    /**
226
     * @test
227
     *
228
     * @covers \Lcobucci\JWT\Configuration::getSigner
229
     *
230
     * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner
231
     * @uses \Lcobucci\JWT\Configuration::__construct
232
     * @uses \Lcobucci\JWT\Signer\None
233
     * @uses \Lcobucci\JWT\Signer\Key
234
     */
235
    public function getSignerShouldReturnTheConfiguredSigner(): void
236
    {
237
        $config = Configuration::forSymmetricSigner($this->signer, new Key(''));
238
239
        self::assertSame($this->signer, $config->getSigner());
240
    }
241
242
    /**
243
     * @test
244
     *
245
     * @covers \Lcobucci\JWT\Configuration::getSigningKey()
246
     *
247
     * @uses \Lcobucci\JWT\Configuration::forAsymmetricSigner
248
     * @uses \Lcobucci\JWT\Configuration::__construct
249
     * @uses \Lcobucci\JWT\Signer\Key
250
     */
251
    public function getSigningKeyShouldReturnTheConfiguredKey(): void
252
    {
253
        $signingKey      = new Key('private');
254
        $verificationKey = new Key('public');
255
256
        $config = Configuration::forAsymmetricSigner($this->signer, $signingKey, $verificationKey);
257
258
        self::assertSame($signingKey, $config->getSigningKey());
259
    }
260
261
    /**
262
     * @test
263
     *
264
     * @covers \Lcobucci\JWT\Configuration::getVerificationKey()
265
     *
266
     * @uses \Lcobucci\JWT\Configuration::forAsymmetricSigner
267
     * @uses \Lcobucci\JWT\Configuration::__construct
268
     * @uses \Lcobucci\JWT\Signer\Key
269
     */
270
    public function getVerificationKeyShouldReturnTheConfiguredKey(): void
271
    {
272
        $signingKey      = new Key('private');
273
        $verificationKey = new Key('public');
274
275
        $config = Configuration::forAsymmetricSigner($this->signer, $signingKey, $verificationKey);
276
277
        self::assertSame($verificationKey, $config->getVerificationKey());
278
    }
279
280
    /**
281
     * @test
282
     *
283
     * @covers \Lcobucci\JWT\Configuration::getValidator
284
     *
285
     * @uses \Lcobucci\JWT\Configuration::forUnsecuredSigner
286
     * @uses \Lcobucci\JWT\Configuration::__construct
287
     * @uses \Lcobucci\JWT\Signer\None
288
     * @uses \Lcobucci\JWT\Signer\Key
289
     */
290
    public function getValidatorShouldReturnTheDefaultWhenItWasNotConfigured(): void
291
    {
292
        $config    = Configuration::forUnsecuredSigner();
293
        $validator = $config->getValidator();
294
295
        self::assertNotSame($this->validator, $validator);
296
    }
297
298
    /**
299
     * @test
300
     *
301
     * @covers \Lcobucci\JWT\Configuration::getValidator
302
     * @covers \Lcobucci\JWT\Configuration::setValidator
303
     *
304
     * @uses \Lcobucci\JWT\Configuration::forUnsecuredSigner
305
     * @uses \Lcobucci\JWT\Configuration::__construct
306
     * @uses \Lcobucci\JWT\Signer\None
307
     * @uses \Lcobucci\JWT\Signer\Key
308
     */
309
    public function getValidatorShouldReturnTheConfiguredValidator(): void
310
    {
311
        $config = Configuration::forUnsecuredSigner();
312
        $config->setValidator($this->validator);
313
314
        self::assertSame($this->validator, $config->getValidator());
315
    }
316
317
    /**
318
     * @test
319
     *
320
     * @covers \Lcobucci\JWT\Configuration::getValidationConstraints
321
     *
322
     * @uses \Lcobucci\JWT\Configuration::forUnsecuredSigner
323
     * @uses \Lcobucci\JWT\Configuration::__construct
324
     * @uses \Lcobucci\JWT\Signer\None
325
     * @uses \Lcobucci\JWT\Signer\Key
326
     */
327
    public function getValidationConstraintsShouldReturnAnEmptyArrayWhenItWasNotConfigured(): void
328
    {
329
        $config = Configuration::forUnsecuredSigner();
330
331
        self::assertSame([], $config->getValidationConstraints());
332
    }
333
334
    /**
335
     * @test
336
     *
337
     * @covers \Lcobucci\JWT\Configuration::getValidationConstraints
338
     * @covers \Lcobucci\JWT\Configuration::setValidationConstraints
339
     *
340
     * @uses \Lcobucci\JWT\Configuration::forUnsecuredSigner
341
     * @uses \Lcobucci\JWT\Configuration::__construct
342
     * @uses \Lcobucci\JWT\Signer\None
343
     * @uses \Lcobucci\JWT\Signer\Key
344
     */
345
    public function getValidationConstraintsShouldReturnTheConfiguredValidator(): void
346
    {
347
        $config = Configuration::forUnsecuredSigner();
348
        $config->setValidationConstraints($this->validationConstraints);
349
350
        self::assertSame([$this->validationConstraints], $config->getValidationConstraints());
351
    }
352
}
353