Merge pull request #354 from acelaya/feature/infection

Feature/infection
This commit is contained in:
Alejandro Celaya 2019-02-16 22:25:13 +01:00 committed by GitHub
commit fd7f1b32dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 213 additions and 57 deletions

View file

@ -48,6 +48,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
* [#330](https://github.com/shlinkio/shlink/issues/330) No longer allow failures on PHP 7.3 envs during project CI build. * [#330](https://github.com/shlinkio/shlink/issues/330) No longer allow failures on PHP 7.3 envs during project CI build.
* [#335](https://github.com/shlinkio/shlink/issues/335) Renamed functional test suite to database test suite, since that better describes what it actually does. * [#335](https://github.com/shlinkio/shlink/issues/335) Renamed functional test suite to database test suite, since that better describes what it actually does.
* [#346](https://github.com/shlinkio/shlink/issues/346) Extracted installer as an independent tool. * [#346](https://github.com/shlinkio/shlink/issues/346) Extracted installer as an independent tool.
* [#261](https://github.com/shlinkio/shlink/issues/261) Increased mutation score to 70%.
#### Deprecated #### Deprecated

View file

@ -53,8 +53,8 @@
"devster/ubench": "^2.0", "devster/ubench": "^2.0",
"doctrine/data-fixtures": "^1.3", "doctrine/data-fixtures": "^1.3",
"filp/whoops": "^2.0", "filp/whoops": "^2.0",
"infection/infection": "^0.11.0", "infection/infection": "^0.12.2",
"phpstan/phpstan": "^0.10.0", "phpstan/phpstan": "^0.10.8",
"phpunit/phpcov": "^6.0@dev || ^5.0", "phpunit/phpcov": "^6.0@dev || ^5.0",
"phpunit/phpunit": "^8.0 || ^7.5", "phpunit/phpunit": "^8.0 || ^7.5",
"roave/security-advisories": "dev-master", "roave/security-advisories": "dev-master",
@ -123,9 +123,9 @@
], ],
"test:unit:pretty": "phpdbg -qrr vendor/bin/phpunit --coverage-html build/coverage --order-by=random", "test:unit:pretty": "phpdbg -qrr vendor/bin/phpunit --coverage-html build/coverage --order-by=random",
"infect": "infection --threads=4 --min-msi=65 --log-verbosity=default --only-covered", "infect": "infection --threads=4 --min-msi=70 --log-verbosity=default --only-covered",
"infect:ci": "infection --threads=4 --min-msi=65 --log-verbosity=default --only-covered --coverage=build", "infect:ci": "infection --threads=4 --min-msi=70 --log-verbosity=default --only-covered --coverage=build",
"infect:show": "infection --threads=4 --min-msi=65 --log-verbosity=default --only-covered --show-mutations", "infect:show": "infection --threads=4 --min-msi=70 --log-verbosity=default --only-covered --show-mutations",
"infect:test": [ "infect:test": [
"@test:unit:ci", "@test:unit:ci",
"@infect:ci" "@infect:ci"

View file

@ -36,9 +36,7 @@ class ListKeysCommandTest extends TestCase
new ApiKey(), new ApiKey(),
])->shouldBeCalledOnce(); ])->shouldBeCalledOnce();
$this->commandTester->execute([ $this->commandTester->execute([]);
'command' => ListKeysCommand::NAME,
]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
$this->assertStringContainsString('Key', $output); $this->assertStringContainsString('Key', $output);
@ -57,7 +55,6 @@ class ListKeysCommandTest extends TestCase
])->shouldBeCalledOnce(); ])->shouldBeCalledOnce();
$this->commandTester->execute([ $this->commandTester->execute([
'command' => ListKeysCommand::NAME,
'--enabledOnly' => true, '--enabledOnly' => true,
]); ]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();

View file

@ -11,10 +11,11 @@ use Shlinkio\Shlink\Core\Exception;
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface; use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
use const PHP_EOL;
use function array_pop; use function array_pop;
use function sprintf; use function sprintf;
class DeleteShortCodeCommandTest extends TestCase class DeleteShortUrlCommandTest extends TestCase
{ {
/** @var CommandTester */ /** @var CommandTester */
private $commandTester; private $commandTester;
@ -32,10 +33,8 @@ class DeleteShortCodeCommandTest extends TestCase
$this->commandTester = new CommandTester($command); $this->commandTester = new CommandTester($command);
} }
/** /** @test */
* @test public function successMessageIsPrintedIfUrlIsProperlyDeleted(): void
*/
public function successMessageIsPrintedIfUrlIsProperlyDeleted()
{ {
$shortCode = 'abc123'; $shortCode = 'abc123';
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->will(function () { $deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->will(function () {
@ -51,10 +50,8 @@ class DeleteShortCodeCommandTest extends TestCase
$deleteByShortCode->shouldHaveBeenCalledOnce(); $deleteByShortCode->shouldHaveBeenCalledOnce();
} }
/** /** @test */
* @test public function invalidShortCodePrintsMessage(): void
*/
public function invalidShortCodePrintsMessage()
{ {
$shortCode = 'abc123'; $shortCode = 'abc123';
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow( $deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow(
@ -70,9 +67,13 @@ class DeleteShortCodeCommandTest extends TestCase
/** /**
* @test * @test
* @dataProvider provideRetryDeleteAnswers
*/ */
public function deleteIsRetriedWhenThresholdIsReachedAndQuestionIsAccepted() public function deleteIsRetriedWhenThresholdIsReachedAndQuestionIsAccepted(
{ array $retryAnswer,
int $expectedDeleteCalls,
string $expectedMessage
): void {
$shortCode = 'abc123'; $shortCode = 'abc123';
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, Argument::type('bool'))->will( $deleteByShortCode = $this->service->deleteByShortCode($shortCode, Argument::type('bool'))->will(
function (array $args) { function (array $args) {
@ -83,7 +84,7 @@ class DeleteShortCodeCommandTest extends TestCase
} }
} }
); );
$this->commandTester->setInputs(['yes']); $this->commandTester->setInputs($retryAnswer);
$this->commandTester->execute(['shortCode' => $shortCode]); $this->commandTester->execute(['shortCode' => $shortCode]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
@ -92,17 +93,19 @@ class DeleteShortCodeCommandTest extends TestCase
'It was not possible to delete the short URL with short code "%s" because it has more than 10 visits.', 'It was not possible to delete the short URL with short code "%s" because it has more than 10 visits.',
$shortCode $shortCode
), $output); ), $output);
$this->assertStringContainsString( $this->assertStringContainsString($expectedMessage, $output);
sprintf('Short URL with short code "%s" successfully deleted.', $shortCode), $deleteByShortCode->shouldHaveBeenCalledTimes($expectedDeleteCalls);
$output
);
$deleteByShortCode->shouldHaveBeenCalledTimes(2);
} }
/** public function provideRetryDeleteAnswers(): iterable
* @test {
*/ yield 'answering yes to retry' => [['yes'], 2, 'Short URL with short code "abc123" successfully deleted.'];
public function deleteIsNotRetriedWhenThresholdIsReachedAndQuestionIsDeclined() yield 'answering no to retry' => [['no'], 1, 'Short URL was not deleted.'];
yield 'answering default to retry' => [[PHP_EOL], 1, 'Short URL was not deleted.'];
}
/** @test */
public function deleteIsNotRetriedWhenThresholdIsReachedAndQuestionIsDeclined(): void
{ {
$shortCode = 'abc123'; $shortCode = 'abc123';
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow( $deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow(

View file

@ -3,11 +3,11 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Exception; namespace Shlinkio\Shlink\Rest\Exception;
use Exception; use Throwable;
class AuthenticationException extends RuntimeException class AuthenticationException extends RuntimeException
{ {
public static function expiredJWT(Exception $prev = null): self public static function expiredJWT(?Throwable $prev = null): self
{ {
return new self('The token has expired.', -1, $prev); return new self('The token has expired.', -1, $prev);
} }

View file

@ -58,7 +58,7 @@ class CreateShortUrlContentNegotiationMiddleware implements MiddlewareInterface
return self::JSON; return self::JSON;
} }
$format = strtolower((string) $query['format']); $format = strtolower($query['format']);
return $format === 'txt' ? self::PLAIN_TEXT : self::JSON; return $format === 'txt' ? self::PLAIN_TEXT : self::JSON;
} }

View file

@ -7,6 +7,8 @@ use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Rest\ErrorHandler\JsonErrorResponseGenerator; use Shlinkio\Shlink\Rest\ErrorHandler\JsonErrorResponseGenerator;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest; use Zend\Diactoros\ServerRequest;
use function array_map;
use function range;
class JsonErrorResponseGeneratorTest extends TestCase class JsonErrorResponseGeneratorTest extends TestCase
{ {
@ -18,27 +20,41 @@ class JsonErrorResponseGeneratorTest extends TestCase
$this->errorHandler = new JsonErrorResponseGenerator(); $this->errorHandler = new JsonErrorResponseGenerator();
} }
/** /** @test */
* @test public function noErrorStatusReturnsInternalServerError(): void
*/
public function noErrorStatusReturnsInternalServerError()
{ {
/** @var Response\JsonResponse $response */
$response = $this->errorHandler->__invoke(null, new ServerRequest(), new Response()); $response = $this->errorHandler->__invoke(null, new ServerRequest(), new Response());
$payload = $response->getPayload();
$this->assertInstanceOf(Response\JsonResponse::class, $response); $this->assertInstanceOf(Response\JsonResponse::class, $response);
$this->assertEquals(500, $response->getStatusCode()); $this->assertEquals(500, $response->getStatusCode());
$this->assertEquals('Internal Server Error', $payload['message']);
} }
/** /**
* @test * @test
* @dataProvider provideStatus
*/ */
public function errorStatusReturnsThatStatus() public function errorStatusReturnsThatStatus(int $status, string $message): void
{ {
/** @var Response\JsonResponse $response */
$response = $this->errorHandler->__invoke( $response = $this->errorHandler->__invoke(
null, null,
new ServerRequest(), new ServerRequest(),
(new Response())->withStatus(405) (new Response())->withStatus($status, $message)
); );
$payload = $response->getPayload();
$this->assertInstanceOf(Response\JsonResponse::class, $response); $this->assertInstanceOf(Response\JsonResponse::class, $response);
$this->assertEquals(405, $response->getStatusCode()); $this->assertEquals($status, $response->getStatusCode());
$this->assertEquals($message, $payload['message']);
}
public function provideStatus(): iterable
{
return array_map(function (int $status) {
return [$status, 'Some message'];
}, range(400, 500, 20));
} }
} }

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Exception;
use Exception;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Rest\Exception\AuthenticationException;
use Throwable;
class AuthenticationExceptionTest extends TestCase
{
/**
* @test
* @dataProvider providePrev
*/
public function expiredJWTCreatesExpectedException(?Throwable $prev): void
{
$e = AuthenticationException::expiredJWT($prev);
$this->assertEquals($prev, $e->getPrevious());
$this->assertEquals(-1, $e->getCode());
$this->assertEquals('The token has expired.', $e->getMessage());
}
public function providePrev()
{
yield 'with previous exception' => [new Exception('Prev')];
yield 'without previous exception' => [null];
}
}

View file

@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Exception;
use Exception;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
use Throwable;
use function array_map;
use function random_int;
use function range;
use function sprintf;
class VerifyAuthenticationExceptionTest extends TestCase
{
use StringUtilsTrait;
/**
* @test
* @dataProvider provideExceptionData
*/
public function withErrorCreatesExpectedException(string $code, string $message, ?Throwable $prev): void
{
$e = VerifyAuthenticationException::withError($code, $message, $prev);
$this->assertEquals(0, $e->getCode());
$this->assertEquals(
sprintf('Authentication verification failed with the public message "%s"', $message),
$e->getMessage()
);
$this->assertEquals($code, $e->getErrorCode());
$this->assertEquals($message, $e->getPublicMessage());
$this->assertEquals($prev, $e->getPrevious());
}
public function provideExceptionData(): iterable
{
return array_map(function () {
return [
$this->generateRandomString(),
$this->generateRandomString(50),
random_int(0, 1) === 1 ? new Exception('Prev') : null,
];
}, range(1, 10));
}
/**
* @test
* @dataProvider provideConstructorData
*/
public function constructCreatesExpectedException(
string $errorCode,
string $publicMessage,
string $message,
int $code,
?Throwable $prev
): void {
$e = new VerifyAuthenticationException($errorCode, $publicMessage, $message, $code, $prev);
$this->assertEquals($code, $e->getCode());
$this->assertEquals($message, $e->getMessage());
$this->assertEquals($errorCode, $e->getErrorCode());
$this->assertEquals($publicMessage, $e->getPublicMessage());
$this->assertEquals($prev, $e->getPrevious());
}
public function provideConstructorData(): iterable
{
return array_map(function (int $i) {
return [
$this->generateRandomString(),
$this->generateRandomString(30),
$this->generateRandomString(50),
$i,
random_int(0, 1) === 1 ? new Exception('Prev') : null,
];
}, range(10, 20));
}
/** @test */
public function defaultConstructorValuesAreKept(): void
{
$e = new VerifyAuthenticationException('foo', 'bar');
$this->assertEquals(0, $e->getCode());
$this->assertEquals('', $e->getMessage());
$this->assertEquals('foo', $e->getErrorCode());
$this->assertEquals('bar', $e->getPublicMessage());
$this->assertNull($e->getPrevious());
}
}

View file

@ -26,23 +26,40 @@ class BodyParserMiddlewareTest extends TestCase
/** /**
* @test * @test
* @dataProvider provideIgnoredRequestMethods
*/ */
public function requestsFromOtherMethodsJustFallbackToNextMiddleware() public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void
{ {
$request = (new ServerRequest())->withMethod('GET'); $request = (new ServerRequest())->withMethod($method);
$delegate = $this->prophesize(RequestHandlerInterface::class); $this->assertHandlingRequestJustFallsBackToNext($request);
/** @var MethodProphecy $process */
$process = $delegate->handle($request)->willReturn(new Response());
$this->middleware->process($request, $delegate->reveal());
$process->shouldHaveBeenCalledOnce();
} }
/** public function provideIgnoredRequestMethods(): iterable
* @test {
*/ yield 'with GET' => ['GET'];
public function jsonRequestsAreJsonDecoded() yield 'with HEAD' => ['HEAD'];
yield 'with OPTIONS' => ['OPTIONS'];
}
/** @test */
public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void
{
$request = (new ServerRequest())->withParsedBody(['foo' => 'bar'])->withMethod('POST');
$this->assertHandlingRequestJustFallsBackToNext($request);
}
private function assertHandlingRequestJustFallsBackToNext(ServerRequestInterface $request): void
{
$nextHandler = $this->prophesize(RequestHandlerInterface::class);
$handle = $nextHandler->handle($request)->willReturn(new Response());
$this->middleware->process($request, $nextHandler->reveal());
$handle->shouldHaveBeenCalledOnce();
}
/** @test */
public function jsonRequestsAreJsonDecoded(): void
{ {
$test = $this; $test = $this;
$body = new Stream('php://temp', 'wr'); $body = new Stream('php://temp', 'wr');
@ -71,10 +88,8 @@ class BodyParserMiddlewareTest extends TestCase
$process->shouldHaveBeenCalledOnce(); $process->shouldHaveBeenCalledOnce();
} }
/** /** @test */
* @test public function regularRequestsAreUrlDecoded(): void
*/
public function regularRequestsAreUrlDecoded()
{ {
$test = $this; $test = $this;
$body = new Stream('php://temp', 'wr'); $body = new Stream('php://temp', 'wr');