33namespace Firebase \JWT ;
44
55use ArrayObject ;
6- use PHPUnit \Framework \TestCase ;
76use DomainException ;
87use InvalidArgumentException ;
8+ use PHPUnit \Framework \TestCase ;
9+ use stdClass ;
910use TypeError ;
1011use UnexpectedValueException ;
11- use stdClass ;
1212
1313class JWTTest extends TestCase
1414{
@@ -37,7 +37,8 @@ public function testExpiredToken()
3737 $ this ->expectException (ExpiredException::class);
3838 $ payload = [
3939 'message ' => 'abc ' ,
40- 'exp ' => time () - 20 ]; // time in the past
40+ 'exp ' => time () - 20 , // time in the past
41+ ];
4142 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
4243 JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
4344 }
@@ -47,7 +48,8 @@ public function testBeforeValidTokenWithNbf()
4748 $ this ->expectException (BeforeValidException::class);
4849 $ payload = [
4950 'message ' => 'abc ' ,
50- "nbf " => time () + 20 ]; // time in the future
51+ 'nbf ' => time () + 20 , // time in the future
52+ ];
5153 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
5254 JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
5355 }
@@ -57,7 +59,8 @@ public function testBeforeValidTokenWithIat()
5759 $ this ->expectException (BeforeValidException::class);
5860 $ payload = [
5961 'message ' => 'abc ' ,
60- "iat " => time () + 20 ]; // time in the future
62+ 'iat ' => time () + 20 , // time in the future
63+ ];
6164 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
6265 JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
6366 }
@@ -66,7 +69,8 @@ public function testValidToken()
6669 {
6770 $ payload = [
6871 'message ' => 'abc ' ,
69- 'exp ' => time () + JWT ::$ leeway + 20 ]; // time in the future
72+ 'exp ' => time () + JWT ::$ leeway + 20 , // time in the future
73+ ];
7074 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
7175 $ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
7276 $ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -77,7 +81,8 @@ public function testValidTokenWithLeeway()
7781 JWT ::$ leeway = 60 ;
7882 $ payload = [
7983 'message ' => 'abc ' ,
80- 'exp ' => time () - 20 ]; // time in the past
84+ 'exp ' => time () - 20 , // time in the past
85+ ];
8186 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
8287 $ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
8388 $ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -89,7 +94,8 @@ public function testExpiredTokenWithLeeway()
8994 JWT ::$ leeway = 60 ;
9095 $ payload = [
9196 'message ' => 'abc ' ,
92- 'exp ' => time () - 70 ]; // time far in the past
97+ 'exp ' => time () - 70 , // time far in the past
98+ ];
9399 $ this ->expectException (ExpiredException::class);
94100 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
95101 $ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
@@ -101,9 +107,10 @@ public function testValidTokenWithNbf()
101107 {
102108 $ payload = [
103109 'message ' => 'abc ' ,
104- " iat " => time (),
110+ ' iat ' => time (),
105111 'exp ' => time () + 20 , // time in the future
106- "nbf " => time () - 20 ];
112+ 'nbf ' => time () - 20
113+ ];
107114 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
108115 $ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
109116 $ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -114,7 +121,8 @@ public function testValidTokenWithNbfLeeway()
114121 JWT ::$ leeway = 60 ;
115122 $ payload = [
116123 'message ' => 'abc ' ,
117- "nbf " => time () + 20 ]; // not before in near (leeway) future
124+ 'nbf ' => time () + 20 , // not before in near (leeway) future
125+ ];
118126 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
119127 $ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
120128 $ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -126,7 +134,8 @@ public function testInvalidTokenWithNbfLeeway()
126134 JWT ::$ leeway = 60 ;
127135 $ payload = [
128136 'message ' => 'abc ' ,
129- "nbf " => time () + 65 ]; // not before too far in future
137+ 'nbf ' => time () + 65 , // not before too far in future
138+ ];
130139 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
131140 $ this ->expectException (BeforeValidException::class);
132141 JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
@@ -138,7 +147,8 @@ public function testValidTokenWithIatLeeway()
138147 JWT ::$ leeway = 60 ;
139148 $ payload = [
140149 'message ' => 'abc ' ,
141- "iat " => time () + 20 ]; // issued in near (leeway) future
150+ 'iat ' => time () + 20 , // issued in near (leeway) future
151+ ];
142152 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
143153 $ decoded = JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
144154 $ this ->assertEquals ($ decoded ->message , 'abc ' );
@@ -150,7 +160,8 @@ public function testInvalidTokenWithIatLeeway()
150160 JWT ::$ leeway = 60 ;
151161 $ payload = [
152162 'message ' => 'abc ' ,
153- "iat " => time () + 65 ]; // issued too far in future
163+ 'iat ' => time () + 65 , // issued too far in future
164+ ];
154165 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
155166 $ this ->expectException (BeforeValidException::class);
156167 JWT ::decode ($ encoded , new Key ('my_key ' , 'HS256 ' ));
@@ -161,7 +172,8 @@ public function testInvalidToken()
161172 {
162173 $ payload = [
163174 'message ' => 'abc ' ,
164- 'exp ' => time () + 20 ]; // time in the future
175+ 'exp ' => time () + 20 , // time in the future
176+ ];
165177 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
166178 $ this ->expectException (SignatureInvalidException::class);
167179 JWT ::decode ($ encoded , new Key ('my_key2 ' , 'HS256 ' ));
@@ -171,7 +183,8 @@ public function testNullKeyFails()
171183 {
172184 $ payload = [
173185 'message ' => 'abc ' ,
174- 'exp ' => time () + JWT ::$ leeway + 20 ]; // time in the future
186+ 'exp ' => time () + JWT ::$ leeway + 20 , // time in the future
187+ ];
175188 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
176189 $ this ->expectException (TypeError::class);
177190 JWT ::decode ($ encoded , new Key (null , 'HS256 ' ));
@@ -181,7 +194,8 @@ public function testEmptyKeyFails()
181194 {
182195 $ payload = [
183196 'message ' => 'abc ' ,
184- 'exp ' => time () + JWT ::$ leeway + 20 ]; // time in the future
197+ 'exp ' => time () + JWT ::$ leeway + 20 , // time in the future
198+ ];
185199 $ encoded = JWT ::encode ($ payload , 'my_key ' , 'HS256 ' );
186200 $ this ->expectException (InvalidArgumentException::class);
187201 JWT ::decode ($ encoded , new Key ('' , 'HS256 ' ));
@@ -250,7 +264,7 @@ public function testInvalidSegmentCount()
250264
251265 public function testInvalidSignatureEncoding ()
252266 {
253- $ msg = " eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx " ;
267+ $ msg = ' eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwibmFtZSI6ImZvbyJ9.Q4Kee9E8o0Xfo4ADXvYA8t7dN_X_bU9K5w6tXuiSjlUxx ' ;
254268 $ this ->expectException (UnexpectedValueException::class);
255269 JWT ::decode ($ msg , new Key ('secret ' , 'HS256 ' ));
256270 }
@@ -333,7 +347,7 @@ public function testDecodesEmptyArrayAsObject()
333347 public function testDecodesArraysInJWTAsArray ()
334348 {
335349 $ key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk ' ;
336- $ payload = ['foo ' => [1 ,2 , 3 ]];
350+ $ payload = ['foo ' => [1 , 2 , 3 ]];
337351 $ jwt = JWT ::encode ($ payload , $ key , 'HS256 ' );
338352 $ decoded = JWT ::decode ($ jwt , new Key ($ key , 'HS256 ' ));
339353 $ this ->assertEquals ($ payload ['foo ' ], $ decoded ->foo );
0 commit comments