From 6c0893cdf8bb4078592d9d9399ab5828ca420001 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 16 Feb 2019 21:24:32 +0100 Subject: [PATCH] Improved tests to increase MSI to 69% --- composer.json | 4 +- .../test/Command/Api/ListKeysCommandTest.php | 5 +- ...Test.php => DeleteShortUrlCommandTest.php} | 45 +++++++++-------- ...teShortUrlContentNegotiationMiddleware.php | 2 +- .../Middleware/BodyParserMiddlewareTest.php | 49 ++++++++++++------- 5 files changed, 60 insertions(+), 45 deletions(-) rename module/CLI/test/Command/ShortUrl/{DeleteShortCodeCommandTest.php => DeleteShortUrlCommandTest.php} (79%) diff --git a/composer.json b/composer.json index a8f78431..ffdc2652 100644 --- a/composer.json +++ b/composer.json @@ -53,8 +53,8 @@ "devster/ubench": "^2.0", "doctrine/data-fixtures": "^1.3", "filp/whoops": "^2.0", - "infection/infection": "^0.11.0", - "phpstan/phpstan": "^0.10.0", + "infection/infection": "^0.12.2", + "phpstan/phpstan": "^0.11.2", "phpunit/phpcov": "^6.0@dev || ^5.0", "phpunit/phpunit": "^8.0 || ^7.5", "roave/security-advisories": "dev-master", diff --git a/module/CLI/test/Command/Api/ListKeysCommandTest.php b/module/CLI/test/Command/Api/ListKeysCommandTest.php index c7b081f3..6d979aa2 100644 --- a/module/CLI/test/Command/Api/ListKeysCommandTest.php +++ b/module/CLI/test/Command/Api/ListKeysCommandTest.php @@ -36,9 +36,7 @@ class ListKeysCommandTest extends TestCase new ApiKey(), ])->shouldBeCalledOnce(); - $this->commandTester->execute([ - 'command' => ListKeysCommand::NAME, - ]); + $this->commandTester->execute([]); $output = $this->commandTester->getDisplay(); $this->assertStringContainsString('Key', $output); @@ -57,7 +55,6 @@ class ListKeysCommandTest extends TestCase ])->shouldBeCalledOnce(); $this->commandTester->execute([ - 'command' => ListKeysCommand::NAME, '--enabledOnly' => true, ]); $output = $this->commandTester->getDisplay(); diff --git a/module/CLI/test/Command/ShortUrl/DeleteShortCodeCommandTest.php b/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php similarity index 79% rename from module/CLI/test/Command/ShortUrl/DeleteShortCodeCommandTest.php rename to module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php index ee514109..0eb156ef 100644 --- a/module/CLI/test/Command/ShortUrl/DeleteShortCodeCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/DeleteShortUrlCommandTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl; +use const PHP_EOL; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; @@ -14,7 +15,7 @@ use Symfony\Component\Console\Tester\CommandTester; use function array_pop; use function sprintf; -class DeleteShortCodeCommandTest extends TestCase +class DeleteShortUrlCommandTest extends TestCase { /** @var CommandTester */ private $commandTester; @@ -32,10 +33,8 @@ class DeleteShortCodeCommandTest extends TestCase $this->commandTester = new CommandTester($command); } - /** - * @test - */ - public function successMessageIsPrintedIfUrlIsProperlyDeleted() + /** @test */ + public function successMessageIsPrintedIfUrlIsProperlyDeleted(): void { $shortCode = 'abc123'; $deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->will(function () { @@ -51,10 +50,8 @@ class DeleteShortCodeCommandTest extends TestCase $deleteByShortCode->shouldHaveBeenCalledOnce(); } - /** - * @test - */ - public function invalidShortCodePrintsMessage() + /** @test */ + public function invalidShortCodePrintsMessage(): void { $shortCode = 'abc123'; $deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow( @@ -70,9 +67,13 @@ class DeleteShortCodeCommandTest extends TestCase /** * @test + * @dataProvider provideRetryDeleteAnswers */ - public function deleteIsRetriedWhenThresholdIsReachedAndQuestionIsAccepted() - { + public function deleteIsRetriedWhenThresholdIsReachedAndQuestionIsAccepted( + array $retryAnswer, + int $expectedDeleteCalls, + string $expectedMessage + ): void { $shortCode = 'abc123'; $deleteByShortCode = $this->service->deleteByShortCode($shortCode, Argument::type('bool'))->will( function (array $args) { @@ -83,7 +84,7 @@ class DeleteShortCodeCommandTest extends TestCase } } ); - $this->commandTester->setInputs(['yes']); + $this->commandTester->setInputs($retryAnswer); $this->commandTester->execute(['shortCode' => $shortCode]); $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.', $shortCode ), $output); - $this->assertStringContainsString( - sprintf('Short URL with short code "%s" successfully deleted.', $shortCode), - $output - ); - $deleteByShortCode->shouldHaveBeenCalledTimes(2); + $this->assertStringContainsString($expectedMessage, $output); + $deleteByShortCode->shouldHaveBeenCalledTimes($expectedDeleteCalls); } - /** - * @test - */ - public function deleteIsNotRetriedWhenThresholdIsReachedAndQuestionIsDeclined() + public function provideRetryDeleteAnswers(): iterable + { + yield 'answering yes to retry' => [['yes'], 2, 'Short URL with short code "abc123" successfully deleted.']; + 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'; $deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow( diff --git a/module/Rest/src/Middleware/ShortUrl/CreateShortUrlContentNegotiationMiddleware.php b/module/Rest/src/Middleware/ShortUrl/CreateShortUrlContentNegotiationMiddleware.php index f5a9c956..bc182071 100644 --- a/module/Rest/src/Middleware/ShortUrl/CreateShortUrlContentNegotiationMiddleware.php +++ b/module/Rest/src/Middleware/ShortUrl/CreateShortUrlContentNegotiationMiddleware.php @@ -58,7 +58,7 @@ class CreateShortUrlContentNegotiationMiddleware implements MiddlewareInterface return self::JSON; } - $format = strtolower((string) $query['format']); + $format = strtolower($query['format']); return $format === 'txt' ? self::PLAIN_TEXT : self::JSON; } diff --git a/module/Rest/test/Middleware/BodyParserMiddlewareTest.php b/module/Rest/test/Middleware/BodyParserMiddlewareTest.php index 31adc609..b127fd8b 100644 --- a/module/Rest/test/Middleware/BodyParserMiddlewareTest.php +++ b/module/Rest/test/Middleware/BodyParserMiddlewareTest.php @@ -26,23 +26,40 @@ class BodyParserMiddlewareTest extends TestCase /** * @test + * @dataProvider provideIgnoredRequestMethods */ - public function requestsFromOtherMethodsJustFallbackToNextMiddleware() + public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void { - $request = (new ServerRequest())->withMethod('GET'); - $delegate = $this->prophesize(RequestHandlerInterface::class); - /** @var MethodProphecy $process */ - $process = $delegate->handle($request)->willReturn(new Response()); - - $this->middleware->process($request, $delegate->reveal()); - - $process->shouldHaveBeenCalledOnce(); + $request = (new ServerRequest())->withMethod($method); + $this->assertHandlingRequestJustFallsBackToNext($request); } - /** - * @test - */ - public function jsonRequestsAreJsonDecoded() + public function provideIgnoredRequestMethods(): iterable + { + yield 'with GET' => ['GET']; + 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; $body = new Stream('php://temp', 'wr'); @@ -71,10 +88,8 @@ class BodyParserMiddlewareTest extends TestCase $process->shouldHaveBeenCalledOnce(); } - /** - * @test - */ - public function regularRequestsAreUrlDecoded() + /** @test */ + public function regularRequestsAreUrlDecoded(): void { $test = $this; $body = new Stream('php://temp', 'wr');