6
6
use PHPUnit \Framework \TestCase ;
7
7
use DomainException ;
8
8
use InvalidArgumentException ;
9
+ use TypeError ;
9
10
use UnexpectedValueException ;
10
11
use stdClass ;
11
12
12
13
class JWTTest extends TestCase
13
14
{
14
- /*
15
- * For compatibility with PHPUnit 4.8 and PHP < 5.6
16
- */
17
- public function setExpectedException ($ exceptionName , $ message = '' , $ code = null )
18
- {
19
- if (method_exists ($ this , 'expectException ' )) {
20
- $ this ->expectException ($ exceptionName );
21
- } else {
22
- parent ::setExpectedException ($ exceptionName , $ message , $ code );
23
- }
24
- }
25
-
26
15
public function testUrlSafeCharacters ()
27
16
{
28
17
$ encoded = JWT ::encode (['message ' => 'f? ' ], 'a ' , 'HS256 ' );
@@ -33,19 +22,19 @@ public function testUrlSafeCharacters()
33
22
34
23
public function testMalformedUtf8StringsFail ()
35
24
{
36
- $ this ->setExpectedException (DomainException::class);
25
+ $ this ->expectException (DomainException::class);
37
26
JWT ::encode (['message ' => pack ('c ' , 128 )], 'a ' , 'HS256 ' );
38
27
}
39
28
40
29
public function testMalformedJsonThrowsException ()
41
30
{
42
- $ this ->setExpectedException (DomainException::class);
31
+ $ this ->expectException (DomainException::class);
43
32
JWT ::jsonDecode ('this is not valid JSON string ' );
44
33
}
45
34
46
35
public function testExpiredToken ()
47
36
{
48
- $ this ->setExpectedException (ExpiredException::class);
37
+ $ this ->expectException (ExpiredException::class);
49
38
$ payload = [
50
39
"message " => "abc " ,
51
40
"exp " => time () - 20 ]; // time in the past
@@ -55,7 +44,7 @@ public function testExpiredToken()
55
44
56
45
public function testBeforeValidTokenWithNbf ()
57
46
{
58
- $ this ->setExpectedException (BeforeValidException::class);
47
+ $ this ->expectException (BeforeValidException::class);
59
48
$ payload = [
60
49
"message " => "abc " ,
61
50
"nbf " => time () + 20 ]; // time in the future
@@ -65,7 +54,7 @@ public function testBeforeValidTokenWithNbf()
65
54
66
55
public function testBeforeValidTokenWithIat ()
67
56
{
68
- $ this ->setExpectedException (BeforeValidException::class);
57
+ $ this ->expectException (BeforeValidException::class);
69
58
$ payload = [
70
59
"message " => "abc " ,
71
60
"iat " => time () + 20 ]; // time in the future
@@ -101,7 +90,7 @@ public function testExpiredTokenWithLeeway()
101
90
$ payload = [
102
91
"message " => "abc " ,
103
92
"exp " => time () - 70 ]; // time far in the past
104
- $ this ->setExpectedException (ExpiredException::class);
93
+ $ this ->expectException (ExpiredException::class);
105
94
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
106
95
$ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
107
96
$ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -139,7 +128,7 @@ public function testInvalidTokenWithNbfLeeway()
139
128
"message " => "abc " ,
140
129
"nbf " => time () + 65 ]; // not before too far in future
141
130
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
142
- $ this ->setExpectedException (BeforeValidException::class);
131
+ $ this ->expectException (BeforeValidException::class);
143
132
JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
144
133
JWT ::$ leeway = 0 ;
145
134
}
@@ -163,7 +152,7 @@ public function testInvalidTokenWithIatLeeway()
163
152
"message " => "abc " ,
164
153
"iat " => time () + 65 ]; // issued too far in future
165
154
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
166
- $ this ->setExpectedException (BeforeValidException::class);
155
+ $ this ->expectException (BeforeValidException::class);
167
156
JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
168
157
JWT ::$ leeway = 0 ;
169
158
}
@@ -174,7 +163,7 @@ public function testInvalidToken()
174
163
"message " => "abc " ,
175
164
"exp " => time () + 20 ]; // time in the future
176
165
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
177
- $ this ->setExpectedException (SignatureInvalidException::class);
166
+ $ this ->expectException (SignatureInvalidException::class);
178
167
JWT ::decode ($ encoded , new Key ('my_key2 ' , 'HS256 ' ));
179
168
}
180
169
@@ -184,7 +173,7 @@ public function testNullKeyFails()
184
173
"message " => "abc " ,
185
174
"exp " => time () + JWT ::$ leeway + 20 ]; // time in the future
186
175
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
187
- $ this ->setExpectedException ( ' TypeError ' );
176
+ $ this ->expectException ( TypeError::class );
188
177
JWT ::decode ($ encoded , new Key (null , 'HS256 ' ));
189
178
}
190
179
@@ -194,7 +183,7 @@ public function testEmptyKeyFails()
194
183
"message " => "abc " ,
195
184
"exp " => time () + JWT ::$ leeway + 20 ]; // time in the future
196
185
$ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
197
- $ this ->setExpectedException (InvalidArgumentException::class);
186
+ $ this ->expectException (InvalidArgumentException::class);
198
187
JWT ::decode ($ encoded , new Key ('' , 'HS256 ' ));
199
188
}
200
189
@@ -227,21 +216,21 @@ public function testArrayAccessKIDChooser()
227
216
public function testNoneAlgorithm ()
228
217
{
229
218
$ msg = JWT ::encode (['message ' => 'abc ' ], 'my_key ' , 'HS256 ' );
230
- $ this ->setExpectedException (UnexpectedValueException::class);
219
+ $ this ->expectException (UnexpectedValueException::class);
231
220
JWT ::decode ($ msg , new Key ('my_key ' , 'none ' ));
232
221
}
233
222
234
223
public function testIncorrectAlgorithm ()
235
224
{
236
225
$ msg = JWT ::encode (['message ' => 'abc ' ], 'my_key ' , 'HS256 ' );
237
- $ this ->setExpectedException (UnexpectedValueException::class);
226
+ $ this ->expectException (UnexpectedValueException::class);
238
227
JWT ::decode ($ msg , new Key ('my_key ' , 'RS256 ' ));
239
228
}
240
229
241
230
public function testEmptyAlgorithm ()
242
231
{
243
232
$ msg = JWT ::encode (['message ' => 'abc ' ], 'my_key ' , 'HS256 ' );
244
- $ this ->setExpectedException (InvalidArgumentException::class);
233
+ $ this ->expectException (InvalidArgumentException::class);
245
234
JWT ::decode ($ msg , new Key ('my_key ' , '' ));
246
235
}
247
236
@@ -255,14 +244,14 @@ public function testAdditionalHeaders()
255
244
256
245
public function testInvalidSegmentCount ()
257
246
{
258
- $ this ->setExpectedException (UnexpectedValueException::class);
247
+ $ this ->expectException (UnexpectedValueException::class);
259
248
JWT ::decode ('brokenheader.brokenbody ' , new Key ('my_key ' , 'HS256 ' ));
260
249
}
261
250
262
251
public function testInvalidSignatureEncoding ()
263
252
{
264
253
$ msg = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx " ;
265
- $ this ->setExpectedException (UnexpectedValueException::class);
254
+ $ this ->expectException (UnexpectedValueException::class);
266
255
JWT ::decode ($ msg , new Key ('secret ' , 'HS256 ' ));
267
256
}
268
257
@@ -312,7 +301,7 @@ public function testInvalidEdDsaEncodeDecode()
312
301
// Generate a different key.
313
302
$ keyPair = sodium_crypto_sign_keypair ();
314
303
$ pubKey = base64_encode (sodium_crypto_sign_publickey ($ keyPair ));
315
- $ this ->setExpectedException (SignatureInvalidException::class);
304
+ $ this ->expectException (SignatureInvalidException::class);
316
305
JWT ::decode ($ msg , new Key ($ pubKey , 'EdDSA ' ));
317
306
}
318
307
0 commit comments