3
3
namespace Ahc \Jwt \Test ;
4
4
5
5
use Ahc \Jwt \JWT ;
6
- use Ahc \Jwt \JWTException ;
7
6
8
7
/** @coversDefaultClass \Ahc\Jwt\JWT */
9
8
class JWTTest extends \PHPUnit \Framework \TestCase
10
9
{
11
10
/** @dataProvider data1 */
12
- public function test_decode_encoded_token (string $ key , string $ algo , int $ age , int $ leeway , array $ payload , array $ header = [])
11
+ public function test_decode_encoded_token ($ key , $ algo , $ age , $ leeway , array $ payload , array $ header = [])
13
12
{
14
13
$ jwt = new JWT ($ key , $ algo , $ age , $ leeway );
15
14
$ token = $ jwt ->encode ($ payload , $ header );
@@ -26,26 +25,28 @@ public function test_decode_encoded_token(string $key, string $algo, int $age, i
26
25
$ this ->assertSame ($ payload , $ decoded );
27
26
}
28
27
28
+ /**
29
+ * @expectedException \Ahc\Jwt\JWTException
30
+ */
29
31
public function test_json_fail ()
30
32
{
31
- $ this ->expectException (JWTException::class);
32
-
33
33
$ jwt = new JWT ('very^secre7 ' );
34
34
35
35
try {
36
- $ jwt ->encode ([random_bytes ( 10 )]);
36
+ $ jwt ->encode ([base64_decode ( ' mF6u28o4K2cD3w== ' )]);
37
37
} catch (\Exception $ e ) {
38
38
$ this ->assertSame ($ e ->getCode (), JWT ::ERROR_JSON_FAILED , $ e ->getMessage ());
39
39
40
40
throw $ e ;
41
41
}
42
42
}
43
43
44
- /** @dataProvider data2 */
45
- public function test_decode_fail (string $ key , string $ algo , int $ age , int $ leeway , int $ offset , int $ error , $ token )
44
+ /**
45
+ * @dataProvider data2
46
+ * @expectedException \Ahc\Jwt\JWTException
47
+ */
48
+ public function test_decode_fail ($ key , $ algo , $ age , $ leeway , $ offset , $ error , $ token )
46
49
{
47
- $ this ->expectException (JWTException::class);
48
-
49
50
$ jwt = new JWT ($ key , $ algo , $ age , $ leeway );
50
51
$ token = is_string ($ token ) ? $ token : $ jwt ->encode ($ token );
51
52
@@ -63,7 +64,7 @@ public function test_decode_fail(string $key, string $algo, int $age, int $leewa
63
64
}
64
65
65
66
/** @dataProvider data1 */
66
- public function test_rs_decode_encoded (string $ key , string $ algo , int $ age , int $ leeway , array $ payload , array $ header = [])
67
+ public function test_rs_decode_encoded ($ key , $ algo , $ age , $ leeway , array $ payload , array $ header = [])
67
68
{
68
69
$ key = __DIR__ . '/stubs/priv.key ' ;
69
70
$ jwt = new JWT ($ key , str_replace ('HS ' , 'RS ' , $ algo ), $ age , $ leeway );
@@ -81,11 +82,12 @@ public function test_rs_decode_encoded(string $key, string $algo, int $age, int
81
82
$ this ->assertSame ($ payload , $ decoded );
82
83
}
83
84
84
- /** @dataProvider data3 */
85
- public function test_rs_invalid_key (string $ method , string $ key , $ arg )
85
+ /**
86
+ * @dataProvider data3
87
+ * @expectedException \Ahc\Jwt\JWTException
88
+ */
89
+ public function test_rs_invalid_key ($ method , $ key , $ arg )
86
90
{
87
- $ this ->expectException (JWTException::class);
88
-
89
91
$ jwt = new JWT ($ key , 'RS256 ' );
90
92
91
93
try {
@@ -109,19 +111,20 @@ public function test_kid()
109
111
return $ jwt ;
110
112
}
111
113
114
+ /**
115
+ * @expectedException \Ahc\Jwt\JWTException
116
+ * @expectedExceptionMessage Invalid token: Unknown key ID
117
+ */
112
118
public function test_kid_invalid ()
113
119
{
114
120
// keys can be sent as array too
115
121
$ jwt = new JWT (['key1 ' => 'secret1 ' , 'key2 ' => 'secret2 ' ], 'HS256 ' );
116
122
117
- $ this ->expectException (JWTException::class);
118
- $ this ->expectExceptionMessage ('Invalid token: Unknown key ID ' );
119
-
120
123
// Use key3
121
124
$ jwt ->encode (['a ' => 1 , 'exp ' => time () + 1000 ], ['kid ' => 'key3 ' ]);
122
125
}
123
126
124
- public function data1 () : array
127
+ public function data1 ()
125
128
{
126
129
return [
127
130
['secret ' , 'HS256 ' , rand (10 , 1000 ), rand (1 , 10 ), [
@@ -152,7 +155,7 @@ public function data1() : array
152
155
];
153
156
}
154
157
155
- public function data2 () : array
158
+ public function data2 ()
156
159
{
157
160
return [
158
161
['topsecret ' , 'HS256 ' , 5 , 0 , 0 , JWT ::ERROR_TOKEN_INVALID , 'a.b ' ],
0 commit comments