Improved tests to increase MSI to 69%

This commit is contained in:
Alejandro Celaya 2019-02-16 21:24:32 +01:00
parent 25927a296d
commit 6c0893cdf8
5 changed files with 60 additions and 45 deletions

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.11.2",
"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",

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

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl; namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
use const PHP_EOL;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
@ -14,7 +15,7 @@ use Symfony\Component\Console\Tester\CommandTester;
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

@ -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

@ -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');