Skip to content

Commit 51b4123

Browse files
authored
Merge pull request Strobotti#3 from Strobotti/test-coverage
Increase test-coverage
2 parents 3beeb8b + 4ccb647 commit 51b4123

File tree

8 files changed

+171
-51
lines changed

8 files changed

+171
-51
lines changed

.php_cs.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
22

33
return \PhpCsFixer\Config::create()
4-
->setFinder(\PhpCsFixer\Finder::create()->in(__DIR__ . '/src'))
4+
->setFinder(
5+
\PhpCsFixer\Finder::create()
6+
->in(__DIR__ . '/src')
7+
->in(__DIR__ . '/tests')
8+
)
59
->setRiskyAllowed(true)
610
->setRules([
711
'@PhpCsFixer' => true,

tests/Key/AbstractKeyTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Strobotti\JWK\Key\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Strobotti\JWK\Key\AbstractKey;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class AbstractKeyTest extends TestCase
14+
{
15+
public function testCreateFromJSON(): void
16+
{
17+
$json = <<<'EOT'
18+
{
19+
"kty": "RSA",
20+
"use": "sig",
21+
"alg": "RS256",
22+
"kid": "86D88Kf"
23+
}
24+
EOT;
25+
26+
$key = AbstractKeyTest__AbstractKey__Mock::createFromJSON($json);
27+
28+
static::assertSame($json, "{$key}");
29+
}
30+
}
31+
32+
final class AbstractKeyTest__AbstractKey__Mock extends AbstractKey
33+
{
34+
}

tests/Key/RsaTest.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use PHPUnit\Framework\TestCase;
88
use Strobotti\JWK\Key\Rsa;
99

10-
final class KeyTest extends TestCase
10+
/**
11+
* @internal
12+
*/
13+
final class RsaTest extends TestCase
1114
{
1215
/**
13-
* @param array $expected
14-
* @param string $input
15-
*
1616
* @dataProvider provideCreateFromJSON
1717
*/
1818
public function testCreateFromJSON(array $expected, string $input): void
@@ -27,9 +27,6 @@ public function testCreateFromJSON(array $expected, string $input): void
2727
static::assertSame($expected['e'], $key->getExponent());
2828
}
2929

30-
/**
31-
* @return \Generator
32-
*/
3330
public function provideCreateFromJSON(): \Generator
3431
{
3532
yield [
@@ -48,7 +45,8 @@ public function provideCreateFromJSON(): \Generator
4845
"use": "sig",
4946
"alg": "RS256",
5047
"n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ",
51-
"e": "AQAB"
48+
"e": "AQAB",
49+
"unsupported": "ignored"
5250
}
5351
EOT
5452
];
@@ -69,6 +67,6 @@ public function testToString(): void
6967

7068
$key = Rsa::createFromJSON($json);
7169

72-
$this->assertSame($json, "$key");
70+
static::assertSame($json, "{$key}");
7371
}
7472
}

tests/KeyConverterTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
use Strobotti\JWK\Key\Rsa;
1111
use Strobotti\JWK\KeyConverter;
1212

13+
/**
14+
* @internal
15+
*/
1316
final class KeyConverterTest extends TestCase
1417
{
1518
/**
16-
* @param KeyInterface $key
17-
* @param string $expected
18-
*
1919
* @dataProvider provideKeyToPem
2020
*/
2121
public function testKeyToPem(KeyInterface $key, string $expected): void
@@ -27,9 +27,6 @@ public function testKeyToPem(KeyInterface $key, string $expected): void
2727
);
2828
}
2929

30-
/**
31-
* @return \Generator
32-
*/
3330
public function provideKeyToPem(): \Generator
3431
{
3532
yield [
@@ -55,20 +52,21 @@ public function provideKeyToPem(): \Generator
5552
];
5653
}
5754

58-
public function testUnsupportedKeyTypeRaisesException()
55+
public function testUnsupportedKeyTypeRaisesException(): void
5956
{
6057
/** @var KeyInterface|MockObject $key */
6158
$key = $this->getMockBuilder(KeyInterface::class)->getMock();
6259

6360
$converter = new KeyConverter();
61+
6462
try {
6563
$converter->keyToPem($key);
6664

67-
$this->fail('converting an unsupported key to PEM should throw an exception');
65+
static::fail('converting an unsupported key to PEM should throw an exception');
6866
} catch (\InvalidArgumentException $e) {
69-
$this->assertTrue(true);
67+
static::assertTrue(true);
7068
} catch (\Throwable $e) {
71-
$this->fail(sprintf('converting an unsupported key to PEM threw an unexpected exception %s', get_class($e)));
69+
static::fail(\sprintf('converting an unsupported key to PEM threw an unexpected exception %s', \get_class($e)));
7270
}
7371
}
7472
}

tests/KeyFactoryTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,23 @@
88
use Strobotti\JWK\Key\Rsa;
99
use Strobotti\JWK\KeyFactory;
1010

11+
/**
12+
* @internal
13+
*/
1114
final class KeyFactoryTest extends TestCase
1215
{
1316
/**
14-
* @param string $pem
15-
* @param array $options
16-
* @param array $json
17-
*
1817
* @dataProvider provideCreateFromPem
1918
*/
20-
public function testCreateFromPem(string $pem, array $options, array $json, string $expectedInstance)
19+
public function testCreateFromPem(string $pem, array $options, array $json, string $expectedInstance): void
2120
{
2221
$factory = new KeyFactory();
2322
$key = $factory->createFromPem($pem, $options);
2423

25-
$this->assertInstanceOf($expectedInstance, $key);
26-
static::assertEquals($json, $key->jsonSerialize());
24+
static::assertInstanceOf($expectedInstance, $key);
25+
static::assertSame($json, $key->jsonSerialize());
2726
}
2827

29-
/**
30-
* @return \Generator
31-
*/
3228
public function provideCreateFromPem(): \Generator
3329
{
3430
yield [

tests/KeySetFactoryTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use PHPUnit\Framework\TestCase;
88
use Strobotti\JWK\KeySetFactory;
99

10+
/**
11+
* @internal
12+
*/
1013
final class KeySetFactoryTest extends TestCase
1114
{
1215
/**
13-
* @param string $input
1416
* @dataProvider provideCreateFromJSON
1517
*/
1618
public function testCreateFromJSON(string $input): void
@@ -20,12 +22,9 @@ public function testCreateFromJSON(string $input): void
2022
$keys = $factory->createFromJSON($input);
2123
$json = $keys->jsonSerialize();
2224

23-
static::assertEquals(\json_decode($input, true), $json);
25+
static::assertSame(\json_decode($input, true), $json);
2426
}
2527

26-
/**
27-
* @return \Generator
28-
*/
2928
public function provideCreateFromJSON(): \Generator
3029
{
3130
yield [
@@ -34,17 +33,17 @@ public function provideCreateFromJSON(): \Generator
3433
"keys": [
3534
{
3635
"kty": "RSA",
37-
"kid": "86D88Kf",
3836
"use": "sig",
3937
"alg": "RS256",
38+
"kid": "86D88Kf",
4039
"n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ",
4140
"e": "AQAB"
4241
},
4342
{
4443
"kty": "RSA",
45-
"kid": "eXaunmL",
4644
"use": "sig",
4745
"alg": "RS256",
46+
"kid": "eXaunmL",
4847
"n": "4dGQ7bQK8LgILOdLsYzfZjkEAoQeVC_aqyc8GC6RX7dq_KvRAQAWPvkam8VQv4GK5T4ogklEKEvj5ISBamdDNq1n52TpxQwI2EqxSk7I9fKPKhRt4F8-2yETlYvye-2s6NeWJim0KBtOVrk0gWvEDgd6WOqJl_yt5WBISvILNyVg1qAAM8JeX6dRPosahRVDjA52G2X-Tip84wqwyRpUlq2ybzcLh3zyhCitBOebiRWDQfG26EH9lTlJhll-p_Dg8vAXxJLIJ4SNLcqgFeZe4OfHLgdzMvxXZJnPp_VgmkcpUdRotazKZumj6dBPcXI_XID4Z4Z3OM1KrZPJNdUhxw",
4948
"e": "AQAB"
5049
}

tests/KeySetTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Strobotti\JWK\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Strobotti\JWK\Key\Rsa;
9+
use Strobotti\JWK\KeySet;
10+
11+
/**
12+
* @internal
13+
*/
14+
final class KeySetTest extends TestCase
15+
{
16+
/**
17+
* @dataProvider provideCreateFromJSON
18+
*/
19+
public function testToString(string $expected, KeySet $keySet): void
20+
{
21+
static::assertSame($expected, "{$keySet}");
22+
}
23+
24+
public function provideCreateFromJSON(): \Generator
25+
{
26+
$keyJson = <<<'EOT'
27+
{
28+
"kty": "RSA",
29+
"use": "sig",
30+
"alg": "RS256",
31+
"kid": "86D88Kf",
32+
"n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ",
33+
"e": "AQAB"
34+
}
35+
EOT;
36+
37+
yield [
38+
'expected' => <<<'EOT'
39+
{
40+
"keys": [
41+
{
42+
"kty": "RSA",
43+
"use": "sig",
44+
"alg": "RS256",
45+
"kid": "86D88Kf",
46+
"n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ",
47+
"e": "AQAB"
48+
}
49+
]
50+
}
51+
EOT
52+
,
53+
'keySet' => (new KeySet())
54+
->addKey(Rsa::createFromJSON($keyJson)),
55+
];
56+
}
57+
58+
public function testAddKeyThrowsErrorOnDuplicateKid(): void
59+
{
60+
$this->expectException(\InvalidArgumentException::class);
61+
62+
$keyJson = <<<'EOT'
63+
{
64+
"kty": "RSA",
65+
"use": "sig",
66+
"alg": "RS256",
67+
"kid": "86D88Kf",
68+
"n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ",
69+
"e": "AQAB"
70+
}
71+
EOT;
72+
$keySet = new KeySet();
73+
$keySet->addKey(Rsa::createFromJSON($keyJson))
74+
->addKey(Rsa::createFromJSON($keyJson))
75+
;
76+
}
77+
78+
public function testGetKeyById(): void
79+
{
80+
$keyJson = <<<'EOT'
81+
{
82+
"kty": "RSA",
83+
"use": "sig",
84+
"alg": "RS256",
85+
"kid": "86D88Kf",
86+
"n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ",
87+
"e": "AQAB"
88+
}
89+
EOT;
90+
91+
$key = Rsa::createFromJSON($keyJson);
92+
93+
$keySet = new KeySet();
94+
$keySet->addKey($key);
95+
96+
static::assertSame($key, $keySet->getKeyById('86D88Kf'));
97+
98+
static::assertNull($keySet->getKeyById('asdf'));
99+
}
100+
}

tests/Util/Base64UrlConverterTest.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@
77
use PHPUnit\Framework\TestCase;
88
use Strobotti\JWK\Util\Base64UrlConverter;
99

10+
/**
11+
* @internal
12+
*/
1013
final class Base64UrlConverterTest extends TestCase
1114
{
1215
/**
13-
* @param string $expected
14-
* @param string $input
15-
*
1616
* @dataProvider provideDecode
1717
*/
1818
public function testDecode(string $expected, string $input): void
1919
{
2020
$converter = new Base64UrlConverter();
2121

22-
$this->assertSame($expected, $converter->decode($input));
22+
static::assertSame($expected, $converter->decode($input));
2323
}
2424

25-
/**
26-
* @return \Generator
27-
*/
2825
public function provideDecode(): \Generator
2926
{
3027
yield [
@@ -34,21 +31,15 @@ public function provideDecode(): \Generator
3431
}
3532

3633
/**
37-
* @param string $expected
38-
* @param string $input
39-
*
4034
* @dataProvider provideEncode
4135
*/
4236
public function testEncode(string $expected, string $input): void
4337
{
4438
$converter = new Base64UrlConverter();
4539

46-
$this->assertSame($expected, $converter->encode($input));
40+
static::assertSame($expected, $converter->encode($input));
4741
}
4842

49-
/**
50-
* @return \Generator
51-
*/
5243
public function provideEncode(): \Generator
5344
{
5445
yield [

0 commit comments

Comments
 (0)