Improved domain exception tests to cover more possible mutants

This commit is contained in:
Alejandro Celaya 2019-12-01 10:14:29 +01:00
parent 8cc4d3e6d5
commit 1bf56b658b
9 changed files with 115 additions and 5 deletions

View File

@ -27,6 +27,14 @@ class DeleteShortUrlExceptionTest extends TestCase
$this->assertEquals($threshold, $e->getVisitsThreshold()); $this->assertEquals($threshold, $e->getVisitsThreshold());
$this->assertEquals($expectedMessage, $e->getMessage()); $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 public function provideThresholds(): array

View File

@ -10,6 +10,8 @@ use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Throwable; use Throwable;
use function sprintf;
class InvalidUrlExceptionTest extends TestCase class InvalidUrlExceptionTest extends TestCase
{ {
/** /**
@ -18,10 +20,17 @@ class InvalidUrlExceptionTest extends TestCase
*/ */
public function properlyCreatesExceptionFromUrl(?Throwable $prev): void 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->getCode());
$this->assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getStatus());
$this->assertEquals($prev, $e->getPrevious()); $this->assertEquals($prev, $e->getPrevious());
} }

View File

@ -15,8 +15,19 @@ class NonUniqueSlugExceptionTest extends TestCase
*/ */
public function properlyCreatesExceptionFromSlug(string $expectedMessage, string $slug, ?string $domain): void public function properlyCreatesExceptionFromSlug(string $expectedMessage, string $slug, ?string $domain): void
{ {
$expectedAdditional = ['customSlug' => $slug];
if ($domain !== null) {
$expectedAdditional['domain'] = $domain;
}
$e = NonUniqueSlugException::fromSlug($slug, $domain); $e = NonUniqueSlugException::fromSlug($slug, $domain);
$this->assertEquals($expectedMessage, $e->getMessage()); $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 public function provideMessages(): iterable

View File

@ -18,8 +18,19 @@ class ShortUrlNotFoundExceptionTest extends TestCase
string $shortCode, string $shortCode,
?string $domain ?string $domain
): void { ): void {
$expectedAdditional = ['shortCode' => $shortCode];
if ($domain !== null) {
$expectedAdditional['domain'] = $domain;
}
$e = ShortUrlNotFoundException::fromNotFoundShortCode($shortCode, $domain); $e = ShortUrlNotFoundException::fromNotFoundShortCode($shortCode, $domain);
$this->assertEquals($expectedMessage, $e->getMessage()); $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 public function provideMessages(): iterable

View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Exception;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
use function sprintf;
class TagNotFoundExceptionTest extends TestCase
{
/** @test */
public function properlyCreatesExceptionFromNotFoundTag(): void
{
$tag = 'foo';
$expectedMessage = sprintf('Tag with name "%s" could not be found', $tag);
$e = TagNotFoundException::fromTag($tag);
$this->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());
}
}

View File

@ -21,7 +21,7 @@ class MissingAuthenticationException extends RuntimeException implements Problem
public static function fromExpectedTypes(array $expectedTypes): self public static function fromExpectedTypes(array $expectedTypes): self
{ {
$e = new self(sprintf( $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) implode('", "', $expectedTypes)
)); ));

View File

@ -17,7 +17,7 @@ class AuthenticationTest extends ApiTestCase
public function authorizationErrorIsReturnedIfNoApiKeyIsSent(): void public function authorizationErrorIsReturnedIfNoApiKeyIsSent(): void
{ {
$expectedDetail = sprintf( $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) implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)
); );

View File

@ -37,7 +37,7 @@ class RequestToAuthPluginTest extends TestCase
$this->expectException(MissingAuthenticationException::class); $this->expectException(MissingAuthenticationException::class);
$this->expectExceptionMessage(sprintf( $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) implode('", "', RequestToHttpAuthPlugin::SUPPORTED_AUTH_HEADERS)
)); ));

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Exception;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Rest\Exception\MissingAuthenticationException;
use function implode;
use function sprintf;
class MissingAuthenticationExceptionTest extends TestCase
{
/**
* @test
* @dataProvider provideExpectedTypes
*/
public function exceptionIsProperlyCreatedFromExpectedTypes(array $expectedTypes): void
{
$expectedMessage = sprintf(
'Expected one of the following authentication headers, ["%s"], but none were provided',
implode('", "', $expectedTypes)
);
$e = MissingAuthenticationException::fromExpectedTypes($expectedTypes);
$this->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']];
}
}