From 5ffcd9fe52e82c42f2f6cf0dae7589368a22b235 Mon Sep 17 00:00:00 2001 From: Juha Jantunen Date: Fri, 27 Mar 2020 04:49:04 +0400 Subject: [PATCH 1/2] Increase test-coverage --- .php_cs.php | 6 +- tests/Key/RsaTest.php | 13 ++-- tests/KeyConverterTest.php | 18 +++-- tests/KeyFactoryTest.php | 16 ++--- tests/KeySetFactoryTest.php | 13 ++-- tests/KeySetTest.php | 100 ++++++++++++++++++++++++++ tests/Util/Base64UrlConverterTest.php | 19 ++--- 7 files changed, 135 insertions(+), 50 deletions(-) create mode 100644 tests/KeySetTest.php diff --git a/.php_cs.php b/.php_cs.php index 332fbb2..168e358 100644 --- a/.php_cs.php +++ b/.php_cs.php @@ -1,7 +1,11 @@ setFinder(\PhpCsFixer\Finder::create()->in(__DIR__ . '/src')) + ->setFinder( + \PhpCsFixer\Finder::create() + ->in(__DIR__ . '/src') + ->in(__DIR__ . '/tests') + ) ->setRiskyAllowed(true) ->setRules([ '@PhpCsFixer' => true, diff --git a/tests/Key/RsaTest.php b/tests/Key/RsaTest.php index 44dd185..48b8df2 100644 --- a/tests/Key/RsaTest.php +++ b/tests/Key/RsaTest.php @@ -7,12 +7,12 @@ use PHPUnit\Framework\TestCase; use Strobotti\JWK\Key\Rsa; -final class KeyTest extends TestCase +/** + * @internal + */ +final class RsaTest extends TestCase { /** - * @param array $expected - * @param string $input - * * @dataProvider provideCreateFromJSON */ public function testCreateFromJSON(array $expected, string $input): void @@ -27,9 +27,6 @@ public function testCreateFromJSON(array $expected, string $input): void static::assertSame($expected['e'], $key->getExponent()); } - /** - * @return \Generator - */ public function provideCreateFromJSON(): \Generator { yield [ @@ -69,6 +66,6 @@ public function testToString(): void $key = Rsa::createFromJSON($json); - $this->assertSame($json, "$key"); + static::assertSame($json, "{$key}"); } } diff --git a/tests/KeyConverterTest.php b/tests/KeyConverterTest.php index 6f69d73..c48beb0 100644 --- a/tests/KeyConverterTest.php +++ b/tests/KeyConverterTest.php @@ -10,12 +10,12 @@ use Strobotti\JWK\Key\Rsa; use Strobotti\JWK\KeyConverter; +/** + * @internal + */ final class KeyConverterTest extends TestCase { /** - * @param KeyInterface $key - * @param string $expected - * * @dataProvider provideKeyToPem */ public function testKeyToPem(KeyInterface $key, string $expected): void @@ -27,9 +27,6 @@ public function testKeyToPem(KeyInterface $key, string $expected): void ); } - /** - * @return \Generator - */ public function provideKeyToPem(): \Generator { yield [ @@ -55,20 +52,21 @@ public function provideKeyToPem(): \Generator ]; } - public function testUnsupportedKeyTypeRaisesException() + public function testUnsupportedKeyTypeRaisesException(): void { /** @var KeyInterface|MockObject $key */ $key = $this->getMockBuilder(KeyInterface::class)->getMock(); $converter = new KeyConverter(); + try { $converter->keyToPem($key); - $this->fail('converting an unsupported key to PEM should throw an exception'); + static::fail('converting an unsupported key to PEM should throw an exception'); } catch (\InvalidArgumentException $e) { - $this->assertTrue(true); + static::assertTrue(true); } catch (\Throwable $e) { - $this->fail(sprintf('converting an unsupported key to PEM threw an unexpected exception %s', get_class($e))); + static::fail(\sprintf('converting an unsupported key to PEM threw an unexpected exception %s', \get_class($e))); } } } diff --git a/tests/KeyFactoryTest.php b/tests/KeyFactoryTest.php index 9264a99..4372057 100644 --- a/tests/KeyFactoryTest.php +++ b/tests/KeyFactoryTest.php @@ -8,27 +8,23 @@ use Strobotti\JWK\Key\Rsa; use Strobotti\JWK\KeyFactory; +/** + * @internal + */ final class KeyFactoryTest extends TestCase { /** - * @param string $pem - * @param array $options - * @param array $json - * * @dataProvider provideCreateFromPem */ - public function testCreateFromPem(string $pem, array $options, array $json, string $expectedInstance) + public function testCreateFromPem(string $pem, array $options, array $json, string $expectedInstance): void { $factory = new KeyFactory(); $key = $factory->createFromPem($pem, $options); - $this->assertInstanceOf($expectedInstance, $key); - static::assertEquals($json, $key->jsonSerialize()); + static::assertInstanceOf($expectedInstance, $key); + static::assertSame($json, $key->jsonSerialize()); } - /** - * @return \Generator - */ public function provideCreateFromPem(): \Generator { yield [ diff --git a/tests/KeySetFactoryTest.php b/tests/KeySetFactoryTest.php index 32bd36a..6300a16 100644 --- a/tests/KeySetFactoryTest.php +++ b/tests/KeySetFactoryTest.php @@ -7,10 +7,12 @@ use PHPUnit\Framework\TestCase; use Strobotti\JWK\KeySetFactory; +/** + * @internal + */ final class KeySetFactoryTest extends TestCase { /** - * @param string $input * @dataProvider provideCreateFromJSON */ public function testCreateFromJSON(string $input): void @@ -20,12 +22,9 @@ public function testCreateFromJSON(string $input): void $keys = $factory->createFromJSON($input); $json = $keys->jsonSerialize(); - static::assertEquals(\json_decode($input, true), $json); + static::assertSame(\json_decode($input, true), $json); } - /** - * @return \Generator - */ public function provideCreateFromJSON(): \Generator { yield [ @@ -34,17 +33,17 @@ public function provideCreateFromJSON(): \Generator "keys": [ { "kty": "RSA", - "kid": "86D88Kf", "use": "sig", "alg": "RS256", + "kid": "86D88Kf", "n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ", "e": "AQAB" }, { "kty": "RSA", - "kid": "eXaunmL", "use": "sig", "alg": "RS256", + "kid": "eXaunmL", "n": "4dGQ7bQK8LgILOdLsYzfZjkEAoQeVC_aqyc8GC6RX7dq_KvRAQAWPvkam8VQv4GK5T4ogklEKEvj5ISBamdDNq1n52TpxQwI2EqxSk7I9fKPKhRt4F8-2yETlYvye-2s6NeWJim0KBtOVrk0gWvEDgd6WOqJl_yt5WBISvILNyVg1qAAM8JeX6dRPosahRVDjA52G2X-Tip84wqwyRpUlq2ybzcLh3zyhCitBOebiRWDQfG26EH9lTlJhll-p_Dg8vAXxJLIJ4SNLcqgFeZe4OfHLgdzMvxXZJnPp_VgmkcpUdRotazKZumj6dBPcXI_XID4Z4Z3OM1KrZPJNdUhxw", "e": "AQAB" } diff --git a/tests/KeySetTest.php b/tests/KeySetTest.php new file mode 100644 index 0000000..2130c1c --- /dev/null +++ b/tests/KeySetTest.php @@ -0,0 +1,100 @@ + <<<'EOT' +{ + "keys": [ + { + "kty": "RSA", + "use": "sig", + "alg": "RS256", + "kid": "86D88Kf", + "n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ", + "e": "AQAB" + } + ] +} +EOT + , + 'keySet' => (new KeySet()) + ->addKey(Rsa::createFromJSON($keyJson)), + ]; + } + + public function testAddKeyThrowsErrorOnDuplicateKid(): void + { + $this->expectException(\InvalidArgumentException::class); + + $keyJson = <<<'EOT' +{ + "kty": "RSA", + "use": "sig", + "alg": "RS256", + "kid": "86D88Kf", + "n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ", + "e": "AQAB" +} +EOT; + $keySet = new KeySet(); + $keySet->addKey(Rsa::createFromJSON($keyJson)) + ->addKey(Rsa::createFromJSON($keyJson)) + ; + } + + public function testGetKeyById(): void + { + $keyJson = <<<'EOT' +{ + "kty": "RSA", + "use": "sig", + "alg": "RS256", + "kid": "86D88Kf", + "n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ", + "e": "AQAB" +} +EOT; + + $key = Rsa::createFromJSON($keyJson); + + $keySet = new KeySet(); + $keySet->addKey($key); + + static::assertSame($key, $keySet->getKeyById('86D88Kf')); + + static::assertNull($keySet->getKeyById('asdf')); + } +} diff --git a/tests/Util/Base64UrlConverterTest.php b/tests/Util/Base64UrlConverterTest.php index 17591c8..4e5a022 100644 --- a/tests/Util/Base64UrlConverterTest.php +++ b/tests/Util/Base64UrlConverterTest.php @@ -7,24 +7,21 @@ use PHPUnit\Framework\TestCase; use Strobotti\JWK\Util\Base64UrlConverter; +/** + * @internal + */ final class Base64UrlConverterTest extends TestCase { /** - * @param string $expected - * @param string $input - * * @dataProvider provideDecode */ public function testDecode(string $expected, string $input): void { $converter = new Base64UrlConverter(); - $this->assertSame($expected, $converter->decode($input)); + static::assertSame($expected, $converter->decode($input)); } - /** - * @return \Generator - */ public function provideDecode(): \Generator { yield [ @@ -34,21 +31,15 @@ public function provideDecode(): \Generator } /** - * @param string $expected - * @param string $input - * * @dataProvider provideEncode */ public function testEncode(string $expected, string $input): void { $converter = new Base64UrlConverter(); - $this->assertSame($expected, $converter->encode($input)); + static::assertSame($expected, $converter->encode($input)); } - /** - * @return \Generator - */ public function provideEncode(): \Generator { yield [ From 4ccb647e39b8cfabc8c6ec7be799a414faf18f1f Mon Sep 17 00:00:00 2001 From: Juha Jantunen Date: Fri, 27 Mar 2020 05:02:57 +0400 Subject: [PATCH 2/2] Increase test-coverage --- tests/Key/AbstractKeyTest.php | 34 ++++++++++++++++++++++++++++++++++ tests/Key/RsaTest.php | 3 ++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/Key/AbstractKeyTest.php diff --git a/tests/Key/AbstractKeyTest.php b/tests/Key/AbstractKeyTest.php new file mode 100644 index 0000000..93fff55 --- /dev/null +++ b/tests/Key/AbstractKeyTest.php @@ -0,0 +1,34 @@ +