Skip to content

Commit a995570

Browse files
committed
Add custom assert to AbstractTestCase that compares JWT.
1 parent 5232450 commit a995570

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

tests/unit/AbstractTestCase.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@
33
namespace Pdsinterop\Solid\Auth;
44

55
use ArgumentCountError;
6+
use Pdsinterop\Solid\Auth\Utils\Base64Url;
67
use PHPUnit\Framework\TestCase;
78

89
abstract class AbstractTestCase extends TestCase
910
{
1011
////////////////////////////// CUSTOM ASSERTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
1112

13+
public function assertJwtEquals(array $expected, string $actual): void
14+
{
15+
$encoded = explode('.', $actual);
16+
17+
$decoded = array_map([Base64Url::class, 'decode'], $encoded);
18+
19+
// We can not easily compare the signatures in PHP, as the numeric
20+
// representation of the binary string is INF (infinity). So unless
21+
// something is passed to compare, we discard the signature
22+
if (count($decoded) === 3 && count($expected) === 2) {
23+
unset ($decoded[2]);
24+
}
25+
26+
$this->assertEquals($expected, $decoded);
27+
}
28+
1229
public function expectArgumentCountError(int $argumentCount): void
1330
{
1431
$this->expectException(ArgumentCountError::class);

tests/unit/TokenGeneratorTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,12 @@ final public function testRegistrationAccessTokenGeneration(): void
179179

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

182-
$expected = <<<JWT
183-
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJtb2NrIGlzc3VlciIsImF1ZCI6Im1vY2sgY2xpZW50IElEIiwic3ViIjoibW9jayBjbGllbnQgSUQifQ.hNpQ3R3krigm6iQ-wWXDzy1bq_txFAlymXJZ4VZd9otTqSNZ8qWB1b2MW6RnFjwlTJrbPzD-yksAn0R7Wwhl5NYnvm-YNlQ6mqKybo-DIfFV6uw6b3b8R4BmfcxVhHuiGDYbBvKiO955KGA4t8FLlBBVLPdREz5w-LYRndDSdho3E62eKX5eKndufPRBOO8bzBJBSbrFLf6kj1hape9uyLvNEmeP9Rq26K98GmDRN-BxAoQlC_EodIgqajqETtKSzsf5bBasu6EpSOOOFGRijS8RCqBt4f1Kkb5ltE_t9TBp0ceBSOB6lGJEShieulEf5prBwLrYMkSFCVTpn5Gwow
184-
JWT;
185-
$this->assertEquals($expected, $actual);
182+
$expected = [
183+
'{"typ":"JWT","alg":"RS256"}',
184+
'{"iss":"mock issuer","aud":"mock client ID","sub":"mock client ID"}',
185+
];
186+
187+
$this->assertJwtEquals($expected, $actual);
186188
}
187189

188190
/**

0 commit comments

Comments
 (0)