Skip to content

Commit 0f45dd7

Browse files
committed
Change AbstractTestCase::assertJwtEquals() to compare array data instead of string contents.
1 parent a1fa9be commit 0f45dd7

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

tests/unit/AbstractTestCase.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,18 @@ public function assertJwtEquals(array $expected, string $actual): void
1414
{
1515
$encoded = explode('.', $actual);
1616

17-
$decoded = array_map([Base64Url::class, 'decode'], $encoded);
17+
$decoded = array_map(function ($encoded) {
18+
$decoded = Base64Url::decode($encoded);
19+
20+
try {
21+
$decoded = json_decode($decoded, true, 512, JSON_THROW_ON_ERROR);
22+
} catch (\JsonException $e) {
23+
// Not (valid) JSON, return Base64Url decoded value
24+
}
25+
26+
return $decoded;
27+
}, $encoded);
28+
1829

1930
// We can not easily compare the signatures in PHP, as the numeric
2031
// representation of the binary string is INF (infinity). So unless

tests/unit/TokenGeneratorTest.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,14 @@ final public function testRegistrationAccessTokenGeneration(): void
155155

156156
$actual = $tokenGenerator->generateRegistrationAccessToken('mock client ID', $privateKey);
157157

158-
$expected = [
159-
'{"typ":"JWT","alg":"RS256"}',
160-
'{"iss":"mock issuer","aud":"mock client ID","sub":"mock client ID"}',
161-
];
162-
163-
$this->assertJwtEquals($expected, $actual);
158+
$this->assertJwtEquals([[
159+
"alg" => "RS256",
160+
"typ" => "JWT",
161+
], [
162+
"iss" => "mock issuer",
163+
"aud" => "mock client ID",
164+
"sub" => "mock client ID",
165+
]], $actual);
164166
}
165167

166168
/**
@@ -298,7 +300,7 @@ final public function testIdTokenGeneration(): void
298300

299301
$now = new \DateTimeImmutable('1234-01-01 12:34:56.789');
300302

301-
$idToken = $tokenGenerator->generateIdToken(
303+
$actual = $tokenGenerator->generateIdToken(
302304
'mock access token',
303305
'mock clientId',
304306
'mock subject',
@@ -308,14 +310,11 @@ final public function testIdTokenGeneration(): void
308310
$now
309311
);
310312

311-
[$header, $body,] = explode('.', $idToken);
312-
313-
$header = Base64Url::decode($header);
314-
$body = json_decode(Base64Url::decode($body), true);
315-
316-
$this->assertEquals('{"typ":"JWT","alg":"RS256","kid":"0c3932ca20f3a00ad2eb72035f6cc9cb"}', $header);
317-
318-
$this->assertEquals([
313+
$this->assertJwtEquals([[
314+
"alg"=>"RS256",
315+
"kid"=>"0c3932ca20f3a00ad2eb72035f6cc9cb",
316+
"typ"=>"JWT",
317+
],[
319318
'aud' => 'mock clientId',
320319
'azp' => 'mock clientId',
321320
'c_hash' => '1EZBnvsFWlK8ESkgHQsrIQ',
@@ -328,6 +327,6 @@ final public function testIdTokenGeneration(): void
328327
'nbf' => -23225829905.789,
329328
'nonce' => 'mock nonce',
330329
'sub' => 'mock subject',
331-
], $body);
330+
]], $actual);
332331
}
333332
}

0 commit comments

Comments
 (0)