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

View File

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

View File

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

View File

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

View File

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