Skip to content

Commit

Permalink
Merge pull request #3 from Strobotti/test-coverage
Browse files Browse the repository at this point in the history
Increase test-coverage
  • Loading branch information
Strobotti authored Mar 28, 2020
2 parents 3beeb8b + 4ccb647 commit 51b4123
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 51 deletions.
6 changes: 5 additions & 1 deletion .php_cs.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

return \PhpCsFixer\Config::create()
->setFinder(\PhpCsFixer\Finder::create()->in(__DIR__ . '/src'))
->setFinder(
\PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
)
->setRiskyAllowed(true)
->setRules([
'@PhpCsFixer' => true,
Expand Down
34 changes: 34 additions & 0 deletions tests/Key/AbstractKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Strobotti\JWK\Key\Tests;

use PHPUnit\Framework\TestCase;
use Strobotti\JWK\Key\AbstractKey;

/**
* @internal
*/
final class AbstractKeyTest extends TestCase
{
public function testCreateFromJSON(): void
{
$json = <<<'EOT'
{
"kty": "RSA",
"use": "sig",
"alg": "RS256",
"kid": "86D88Kf"
}
EOT;

$key = AbstractKeyTest__AbstractKey__Mock::createFromJSON($json);

static::assertSame($json, "{$key}");
}
}

final class AbstractKeyTest__AbstractKey__Mock extends AbstractKey
{
}
16 changes: 7 additions & 9 deletions tests/Key/RsaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [
Expand All @@ -48,7 +45,8 @@ public function provideCreateFromJSON(): \Generator
"use": "sig",
"alg": "RS256",
"n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ",
"e": "AQAB"
"e": "AQAB",
"unsupported": "ignored"
}
EOT
];
Expand All @@ -69,6 +67,6 @@ public function testToString(): void

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

$this->assertSame($json, "$key");
static::assertSame($json, "{$key}");
}
}
18 changes: 8 additions & 10 deletions tests/KeyConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,9 +27,6 @@ public function testKeyToPem(KeyInterface $key, string $expected): void
);
}

/**
* @return \Generator
*/
public function provideKeyToPem(): \Generator
{
yield [
Expand All @@ -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)));
}
}
}
16 changes: 6 additions & 10 deletions tests/KeyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
13 changes: 6 additions & 7 deletions tests/KeySetFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [
Expand All @@ -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"
}
Expand Down
100 changes: 100 additions & 0 deletions tests/KeySetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Strobotti\JWK\Tests;

use PHPUnit\Framework\TestCase;
use Strobotti\JWK\Key\Rsa;
use Strobotti\JWK\KeySet;

/**
* @internal
*/
final class KeySetTest extends TestCase
{
/**
* @dataProvider provideCreateFromJSON
*/
public function testToString(string $expected, KeySet $keySet): void
{
static::assertSame($expected, "{$keySet}");
}

public function provideCreateFromJSON(): \Generator
{
$keyJson = <<<'EOT'
{
"kty": "RSA",
"use": "sig",
"alg": "RS256",
"kid": "86D88Kf",
"n": "iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ",
"e": "AQAB"
}
EOT;

yield [
'expected' => <<<'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'));
}
}
19 changes: 5 additions & 14 deletions tests/Util/Base64UrlConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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 [
Expand Down

0 comments on commit 51b4123

Please sign in to comment.