Skip to content

Commit 958938b

Browse files
authored
chore: remove phpunit shim (#401)
1 parent f8550f8 commit 958938b

File tree

2 files changed

+31
-63
lines changed

2 files changed

+31
-63
lines changed

tests/JWKTest.php

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,26 @@ class JWKTest extends TestCase
1414

1515
public function testMissingKty()
1616
{
17-
$this->setExpectedException(
18-
UnexpectedValueException::class,
19-
'JWK must contain a "kty" parameter'
20-
);
17+
$this->expectException(UnexpectedValueException::class);
18+
$this->expectExceptionMessage('JWK must contain a "kty" parameter');
2119

2220
$badJwk = ['kid' => 'foo'];
2321
$keys = JWK::parseKeySet(['keys' => [$badJwk]]);
2422
}
2523

2624
public function testInvalidAlgorithm()
2725
{
28-
$this->setExpectedException(
29-
UnexpectedValueException::class,
30-
'No supported algorithms found in JWK Set'
31-
);
26+
$this->expectException(UnexpectedValueException::class);
27+
$this->expectExceptionMessage('No supported algorithms found in JWK Set');
3228

3329
$badJwk = ['kty' => 'BADTYPE', 'alg' => 'RSA256'];
3430
$keys = JWK::parseKeySet(['keys' => [$badJwk]]);
3531
}
3632

3733
public function testParsePrivateKey()
3834
{
39-
$this->setExpectedException(
40-
UnexpectedValueException::class,
41-
'RSA private keys are not supported'
42-
);
35+
$this->expectException(UnexpectedValueException::class);
36+
$this->expectExceptionMessage('RSA private keys are not supported');
4337

4438
$jwkSet = json_decode(
4539
file_get_contents(__DIR__ . '/data/rsa-jwkset.json'),
@@ -52,10 +46,8 @@ public function testParsePrivateKey()
5246

5347
public function testParsePrivateKeyWithoutAlg()
5448
{
55-
$this->setExpectedException(
56-
UnexpectedValueException::class,
57-
'JWK must contain an "alg" parameter'
58-
);
49+
$this->expectException(UnexpectedValueException::class);
50+
$this->expectExceptionMessage('JWK must contain an "alg" parameter');
5951

6052
$jwkSet = json_decode(
6153
file_get_contents(__DIR__ . '/data/rsa-jwkset.json'),
@@ -94,14 +86,16 @@ public function testParseJwkKeySet()
9486

9587
public function testParseJwkKey_empty()
9688
{
97-
$this->setExpectedException(InvalidArgumentException::class, 'JWK must not be empty');
89+
$this->expectException(InvalidArgumentException::class);
90+
$this->expectExceptionMessage('JWK must not be empty');
9891

9992
JWK::parseKeySet(['keys' => [[]]]);
10093
}
10194

10295
public function testParseJwkKeySet_empty()
10396
{
104-
$this->setExpectedException(InvalidArgumentException::class, 'JWK Set did not contain any keys');
97+
$this->expectException(InvalidArgumentException::class);
98+
$this->expectExceptionMessage('JWK Set did not contain any keys');
10599

106100
JWK::parseKeySet(['keys' => []]);
107101
}
@@ -115,7 +109,7 @@ public function testDecodeByJwkKeySetTokenExpired()
115109
$payload = ['exp' => strtotime('-1 hour')];
116110
$msg = JWT::encode($payload, $privKey1, 'RS256', 'jwk1');
117111

118-
$this->setExpectedException(ExpiredException::class);
112+
$this->expectException(ExpiredException::class);
119113

120114
JWT::decode($msg, self::$keys);
121115
}
@@ -147,19 +141,4 @@ public function testDecodeByMultiJwkKeySet()
147141

148142
$this->assertEquals("bar", $result->sub);
149143
}
150-
151-
/*
152-
* For compatibility with PHPUnit 4.8 and PHP < 5.6
153-
*/
154-
public function setExpectedException($exceptionName, $message = '', $code = null)
155-
{
156-
if (method_exists($this, 'expectException')) {
157-
$this->expectException($exceptionName);
158-
if ($message) {
159-
$this->expectExceptionMessage($message);
160-
}
161-
} else {
162-
parent::setExpectedException($exceptionName, $message, $code);
163-
}
164-
}
165144
}

tests/JWTTest.php

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,12 @@
66
use PHPUnit\Framework\TestCase;
77
use DomainException;
88
use InvalidArgumentException;
9+
use TypeError;
910
use UnexpectedValueException;
1011
use stdClass;
1112

1213
class 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

Comments
 (0)