From 1bf56b658b2ebab356aa53f4319f91ee243302f3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 1 Dec 2019 10:14:29 +0100 Subject: [PATCH] Improved domain exception tests to cover more possible mutants --- .../Exception/DeleteShortUrlExceptionTest.php | 8 ++++ .../Exception/InvalidUrlExceptionTest.php | 13 +++++- .../Exception/NonUniqueSlugExceptionTest.php | 11 +++++ .../ShortUrlNotFoundExceptionTest.php | 11 +++++ .../Exception/TagNotFoundExceptionTest.php | 28 ++++++++++++ .../MissingAuthenticationException.php | 2 +- .../Middleware/AuthenticationTest.php | 2 +- .../RequestToAuthPluginTest.php | 2 +- .../MissingAuthenticationExceptionTest.php | 43 +++++++++++++++++++ 9 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 module/Core/test/Exception/TagNotFoundExceptionTest.php create mode 100644 module/Rest/test/Exception/MissingAuthenticationExceptionTest.php diff --git a/module/Core/test/Exception/DeleteShortUrlExceptionTest.php b/module/Core/test/Exception/DeleteShortUrlExceptionTest.php index 0e2012a2..f2207c54 100644 --- a/module/Core/test/Exception/DeleteShortUrlExceptionTest.php +++ b/module/Core/test/Exception/DeleteShortUrlExceptionTest.php @@ -27,6 +27,14 @@ class DeleteShortUrlExceptionTest extends TestCase $this->assertEquals($threshold, $e->getVisitsThreshold()); $this->assertEquals($expectedMessage, $e->getMessage()); + $this->assertEquals($expectedMessage, $e->getDetail()); + $this->assertEquals([ + 'shortCode' => $shortCode, + 'threshold' => $threshold, + ], $e->getAdditionalData()); + $this->assertEquals('Cannot delete short URL', $e->getTitle()); + $this->assertEquals('INVALID_SHORTCODE_DELETION', $e->getType()); + $this->assertEquals(422, $e->getStatus()); } public function provideThresholds(): array diff --git a/module/Core/test/Exception/InvalidUrlExceptionTest.php b/module/Core/test/Exception/InvalidUrlExceptionTest.php index 1b7de449..cb0a08bc 100644 --- a/module/Core/test/Exception/InvalidUrlExceptionTest.php +++ b/module/Core/test/Exception/InvalidUrlExceptionTest.php @@ -10,6 +10,8 @@ use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Throwable; +use function sprintf; + class InvalidUrlExceptionTest extends TestCase { /** @@ -18,10 +20,17 @@ class InvalidUrlExceptionTest extends TestCase */ public function properlyCreatesExceptionFromUrl(?Throwable $prev): void { - $e = InvalidUrlException::fromUrl('http://the_url.com', $prev); + $url = 'http://the_url.com'; + $expectedMessage = sprintf('Provided URL %s is invalid. Try with a different one.', $url); + $e = InvalidUrlException::fromUrl($url, $prev); - $this->assertEquals('Provided URL http://the_url.com is invalid. Try with a different one.', $e->getMessage()); + $this->assertEquals($expectedMessage, $e->getMessage()); + $this->assertEquals($expectedMessage, $e->getDetail()); + $this->assertEquals('Invalid URL', $e->getTitle()); + $this->assertEquals('INVALID_URL', $e->getType()); + $this->assertEquals(['url' => $url], $e->getAdditionalData()); $this->assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getCode()); + $this->assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getStatus()); $this->assertEquals($prev, $e->getPrevious()); } diff --git a/module/Core/test/Exception/NonUniqueSlugExceptionTest.php b/module/Core/test/Exception/NonUniqueSlugExceptionTest.php index d2008621..00efa3cf 100644 --- a/module/Core/test/Exception/NonUniqueSlugExceptionTest.php +++ b/module/Core/test/Exception/NonUniqueSlugExceptionTest.php @@ -15,8 +15,19 @@ class NonUniqueSlugExceptionTest extends TestCase */ public function properlyCreatesExceptionFromSlug(string $expectedMessage, string $slug, ?string $domain): void { + $expectedAdditional = ['customSlug' => $slug]; + if ($domain !== null) { + $expectedAdditional['domain'] = $domain; + } + $e = NonUniqueSlugException::fromSlug($slug, $domain); + $this->assertEquals($expectedMessage, $e->getMessage()); + $this->assertEquals($expectedMessage, $e->getDetail()); + $this->assertEquals('Invalid custom slug', $e->getTitle()); + $this->assertEquals('INVALID_SLUG', $e->getType()); + $this->assertEquals(400, $e->getStatus()); + $this->assertEquals($expectedAdditional, $e->getAdditionalData()); } public function provideMessages(): iterable diff --git a/module/Core/test/Exception/ShortUrlNotFoundExceptionTest.php b/module/Core/test/Exception/ShortUrlNotFoundExceptionTest.php index 6f0d58f4..be02a66c 100644 --- a/module/Core/test/Exception/ShortUrlNotFoundExceptionTest.php +++ b/module/Core/test/Exception/ShortUrlNotFoundExceptionTest.php @@ -18,8 +18,19 @@ class ShortUrlNotFoundExceptionTest extends TestCase string $shortCode, ?string $domain ): void { + $expectedAdditional = ['shortCode' => $shortCode]; + if ($domain !== null) { + $expectedAdditional['domain'] = $domain; + } + $e = ShortUrlNotFoundException::fromNotFoundShortCode($shortCode, $domain); + $this->assertEquals($expectedMessage, $e->getMessage()); + $this->assertEquals($expectedMessage, $e->getDetail()); + $this->assertEquals('Short URL not found', $e->getTitle()); + $this->assertEquals('INVALID_SHORTCODE', $e->getType()); + $this->assertEquals(404, $e->getStatus()); + $this->assertEquals($expectedAdditional, $e->getAdditionalData()); } public function provideMessages(): iterable diff --git a/module/Core/test/Exception/TagNotFoundExceptionTest.php b/module/Core/test/Exception/TagNotFoundExceptionTest.php new file mode 100644 index 00000000..ccee7b38 --- /dev/null +++ b/module/Core/test/Exception/TagNotFoundExceptionTest.php @@ -0,0 +1,28 @@ +assertEquals($expectedMessage, $e->getMessage()); + $this->assertEquals($expectedMessage, $e->getDetail()); + $this->assertEquals('Tag not found', $e->getTitle()); + $this->assertEquals('TAG_NOT_FOUND', $e->getType()); + $this->assertEquals(['tag' => $tag], $e->getAdditionalData()); + $this->assertEquals(404, $e->getStatus()); + } +} diff --git a/module/Rest/src/Exception/MissingAuthenticationException.php b/module/Rest/src/Exception/MissingAuthenticationException.php index 6ed76e2a..c00cb3e0 100644 --- a/module/Rest/src/Exception/MissingAuthenticationException.php +++ b/module/Rest/src/Exception/MissingAuthenticationException.php @@ -21,7 +21,7 @@ class MissingAuthenticationException extends RuntimeException implements Problem public static function fromExpectedTypes(array $expectedTypes): self { $e = new self(sprintf( - 'Expected one of the following authentication headers, but none were provided, ["%s"]', + 'Expected one of the following authentication headers, ["%s"], but none were provided', implode('", "', $expectedTypes) )); diff --git a/module/Rest/test-api/Middleware/AuthenticationTest.php b/module/Rest/test-api/Middleware/AuthenticationTest.php index 3526c5f5..d92f4a44 100644 --- a/module/Rest/test-api/Middleware/AuthenticationTest.php +++ b/module/Rest/test-api/Middleware/AuthenticationTest.php @@ -17,7 +17,7 @@ class AuthenticationTest extends ApiTestCase public function authorizationErrorIsReturnedIfNoApiKeyIsSent(): void { $expectedDetail = sprintf( - 'Expected one of the following authentication headers, but none were provided, ["%s"]', + 'Expected one of the following authentication headers, ["%s"], but none were provided', implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS) ); diff --git a/module/Rest/test/Authentication/RequestToAuthPluginTest.php b/module/Rest/test/Authentication/RequestToAuthPluginTest.php index 8d11b4d8..a49a4e19 100644 --- a/module/Rest/test/Authentication/RequestToAuthPluginTest.php +++ b/module/Rest/test/Authentication/RequestToAuthPluginTest.php @@ -37,7 +37,7 @@ class RequestToAuthPluginTest extends TestCase $this->expectException(MissingAuthenticationException::class); $this->expectExceptionMessage(sprintf( - 'Expected one of the following authentication headers, but none were provided, ["%s"]', + 'Expected one of the following authentication headers, ["%s"], but none were provided', implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS) )); diff --git a/module/Rest/test/Exception/MissingAuthenticationExceptionTest.php b/module/Rest/test/Exception/MissingAuthenticationExceptionTest.php new file mode 100644 index 00000000..84c72e75 --- /dev/null +++ b/module/Rest/test/Exception/MissingAuthenticationExceptionTest.php @@ -0,0 +1,43 @@ +assertEquals($expectedMessage, $e->getMessage()); + $this->assertEquals($expectedMessage, $e->getDetail()); + $this->assertEquals('Invalid authorization', $e->getTitle()); + $this->assertEquals('INVALID_AUTHORIZATION', $e->getType()); + $this->assertEquals(401, $e->getStatus()); + $this->assertEquals(['expectedTypes' => $expectedTypes], $e->getAdditionalData()); + } + + public function provideExpectedTypes(): iterable + { + yield [['foo', 'bar']]; + yield [['something']]; + yield [[]]; + yield [['foo', 'bar', 'baz']]; + } +}