66use PHPUnit \Framework \TestCase ;
77use DomainException ;
88use InvalidArgumentException ;
9+ use TypeError ;
910use UnexpectedValueException ;
1011use stdClass ;
1112
1213class JWTTest extends TestCase
1314{
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-
2615 public function testUrlSafeCharacters ()
2716 {
2817 $ encoded = JWT ::encode (['message ' => 'f? ' ], 'a ' , 'HS256 ' );
@@ -33,19 +22,19 @@ public function testUrlSafeCharacters()
3322
3423 public function testMalformedUtf8StringsFail ()
3524 {
36- $ this ->setExpectedException (DomainException::class);
25+ $ this ->expectException (DomainException::class);
3726 JWT ::encode (['message ' => pack ('c ' , 128 )], 'a ' , 'HS256 ' );
3827 }
3928
4029 public function testMalformedJsonThrowsException ()
4130 {
42- $ this ->setExpectedException (DomainException::class);
31+ $ this ->expectException (DomainException::class);
4332 JWT ::jsonDecode ('this is not valid JSON string ' );
4433 }
4534
4635 public function testExpiredToken ()
4736 {
48- $ this ->setExpectedException (ExpiredException::class);
37+ $ this ->expectException (ExpiredException::class);
4938 $ payload = [
5039 "message " => "abc " ,
5140 "exp " => time () - 20 ]; // time in the past
@@ -55,7 +44,7 @@ public function testExpiredToken()
5544
5645 public function testBeforeValidTokenWithNbf ()
5746 {
58- $ this ->setExpectedException (BeforeValidException::class);
47+ $ this ->expectException (BeforeValidException::class);
5948 $ payload = [
6049 "message " => "abc " ,
6150 "nbf " => time () + 20 ]; // time in the future
@@ -65,7 +54,7 @@ public function testBeforeValidTokenWithNbf()
6554
6655 public function testBeforeValidTokenWithIat ()
6756 {
68- $ this ->setExpectedException (BeforeValidException::class);
57+ $ this ->expectException (BeforeValidException::class);
6958 $ payload = [
7059 "message " => "abc " ,
7160 "iat " => time () + 20 ]; // time in the future
@@ -101,7 +90,7 @@ public function testExpiredTokenWithLeeway()
10190 $ payload = [
10291 "message " => "abc " ,
10392 "exp " => time () - 70 ]; // time far in the past
104- $ this ->setExpectedException (ExpiredException::class);
93+ $ this ->expectException (ExpiredException::class);
10594 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
10695 $ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
10796 $ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -139,7 +128,7 @@ public function testInvalidTokenWithNbfLeeway()
139128 "message " => "abc " ,
140129 "nbf " => time () + 65 ]; // not before too far in future
141130 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
142- $ this ->setExpectedException (BeforeValidException::class);
131+ $ this ->expectException (BeforeValidException::class);
143132 JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
144133 JWT ::$ leeway = 0 ;
145134 }
@@ -163,7 +152,7 @@ public function testInvalidTokenWithIatLeeway()
163152 "message " => "abc " ,
164153 "iat " => time () + 65 ]; // issued too far in future
165154 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
166- $ this ->setExpectedException (BeforeValidException::class);
155+ $ this ->expectException (BeforeValidException::class);
167156 JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
168157 JWT ::$ leeway = 0 ;
169158 }
@@ -174,7 +163,7 @@ public function testInvalidToken()
174163 "message " => "abc " ,
175164 "exp " => time () + 20 ]; // time in the future
176165 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
177- $ this ->setExpectedException (SignatureInvalidException::class);
166+ $ this ->expectException (SignatureInvalidException::class);
178167 JWT ::decode ($ encoded , new Key ('my_key2 ' , 'HS256 ' ));
179168 }
180169
@@ -184,7 +173,7 @@ public function testNullKeyFails()
184173 "message " => "abc " ,
185174 "exp " => time () + JWT ::$ leeway + 20 ]; // time in the future
186175 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
187- $ this ->setExpectedException ( ' TypeError ' );
176+ $ this ->expectException ( TypeError::class );
188177 JWT ::decode ($ encoded , new Key (null , 'HS256 ' ));
189178 }
190179
@@ -194,7 +183,7 @@ public function testEmptyKeyFails()
194183 "message " => "abc " ,
195184 "exp " => time () + JWT ::$ leeway + 20 ]; // time in the future
196185 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
197- $ this ->setExpectedException (InvalidArgumentException::class);
186+ $ this ->expectException (InvalidArgumentException::class);
198187 JWT ::decode ($ encoded , new Key ('' , 'HS256 ' ));
199188 }
200189
@@ -227,21 +216,21 @@ public function testArrayAccessKIDChooser()
227216 public function testNoneAlgorithm ()
228217 {
229218 $ msg = JWT ::encode (['message ' => 'abc ' ], 'my_key ' , 'HS256 ' );
230- $ this ->setExpectedException (UnexpectedValueException::class);
219+ $ this ->expectException (UnexpectedValueException::class);
231220 JWT ::decode ($ msg , new Key ('my_key ' , 'none ' ));
232221 }
233222
234223 public function testIncorrectAlgorithm ()
235224 {
236225 $ msg = JWT ::encode (['message ' => 'abc ' ], 'my_key ' , 'HS256 ' );
237- $ this ->setExpectedException (UnexpectedValueException::class);
226+ $ this ->expectException (UnexpectedValueException::class);
238227 JWT ::decode ($ msg , new Key ('my_key ' , 'RS256 ' ));
239228 }
240229
241230 public function testEmptyAlgorithm ()
242231 {
243232 $ msg = JWT ::encode (['message ' => 'abc ' ], 'my_key ' , 'HS256 ' );
244- $ this ->setExpectedException (InvalidArgumentException::class);
233+ $ this ->expectException (InvalidArgumentException::class);
245234 JWT ::decode ($ msg , new Key ('my_key ' , '' ));
246235 }
247236
@@ -255,14 +244,14 @@ public function testAdditionalHeaders()
255244
256245 public function testInvalidSegmentCount ()
257246 {
258- $ this ->setExpectedException (UnexpectedValueException::class);
247+ $ this ->expectException (UnexpectedValueException::class);
259248 JWT ::decode ('brokenheader.brokenbody ' , new Key ('my_key ' , 'HS256 ' ));
260249 }
261250
262251 public function testInvalidSignatureEncoding ()
263252 {
264253 $ msg = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx " ;
265- $ this ->setExpectedException (UnexpectedValueException::class);
254+ $ this ->expectException (UnexpectedValueException::class);
266255 JWT ::decode ($ msg , new Key ('secret ' , 'HS256 ' ));
267256 }
268257
@@ -312,7 +301,7 @@ public function testInvalidEdDsaEncodeDecode()
312301 // Generate a different key.
313302 $ keyPair = sodium_crypto_sign_keypair ();
314303 $ pubKey = base64_encode (sodium_crypto_sign_publickey ($ keyPair ));
315- $ this ->setExpectedException (SignatureInvalidException::class);
304+ $ this ->expectException (SignatureInvalidException::class);
316305 JWT ::decode ($ msg , new Key ($ pubKey , 'EdDSA ' ));
317306 }
318307
0 commit comments