Migrated ListShortUrlsCommandTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-22 13:42:46 +02:00
parent 8b675f55cc
commit 4872bd3a92

View File

@ -6,9 +6,8 @@ namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Pagerfanta\Adapter\ArrayAdapter; use Pagerfanta\Adapter\ArrayAdapter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\ShortUrl\ListShortUrlsCommand; use Shlinkio\Shlink\CLI\Command\ShortUrl\ListShortUrlsCommand;
use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
@ -31,12 +30,12 @@ class ListShortUrlsCommandTest extends TestCase
use CliTestUtilsTrait; use CliTestUtilsTrait;
private CommandTester $commandTester; private CommandTester $commandTester;
private ObjectProphecy $shortUrlService; private MockObject $shortUrlService;
protected function setUp(): void protected function setUp(): void
{ {
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class); $this->shortUrlService = $this->createMock(ShortUrlServiceInterface::class);
$command = new ListShortUrlsCommand($this->shortUrlService->reveal(), new ShortUrlDataTransformer( $command = new ListShortUrlsCommand($this->shortUrlService, new ShortUrlDataTransformer(
new ShortUrlStringifier([]), new ShortUrlStringifier([]),
)); ));
$this->commandTester = $this->testerForCommand($command); $this->commandTester = $this->testerForCommand($command);
@ -51,9 +50,8 @@ class ListShortUrlsCommandTest extends TestCase
$data[] = ShortUrl::withLongUrl('url_' . $i); $data[] = ShortUrl::withLongUrl('url_' . $i);
} }
$this->shortUrlService->listShortUrls(Argument::cetera()) $this->shortUrlService->expects($this->exactly(3))->method('listShortUrls')->withAnyParameters()
->will(fn () => new Paginator(new ArrayAdapter($data))) ->willReturnCallback(fn () => new Paginator(new ArrayAdapter($data)));
->shouldBeCalledTimes(3);
$this->commandTester->setInputs(['y', 'y', 'n']); $this->commandTester->setInputs(['y', 'y', 'n']);
$this->commandTester->execute([]); $this->commandTester->execute([]);
@ -74,9 +72,9 @@ class ListShortUrlsCommandTest extends TestCase
$data[] = ShortUrl::withLongUrl('url_' . $i); $data[] = ShortUrl::withLongUrl('url_' . $i);
} }
$this->shortUrlService->listShortUrls(ShortUrlsParams::emptyInstance()) $this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
->willReturn(new Paginator(new ArrayAdapter($data))) $this->equalTo(ShortUrlsParams::emptyInstance())
->shouldBeCalledOnce(); )->willReturn(new Paginator(new ArrayAdapter($data)));
$this->commandTester->setInputs(['n']); $this->commandTester->setInputs(['n']);
$this->commandTester->execute([]); $this->commandTester->execute([]);
@ -95,9 +93,9 @@ class ListShortUrlsCommandTest extends TestCase
public function passingPageWillMakeListStartOnThatPage(): void public function passingPageWillMakeListStartOnThatPage(): void
{ {
$page = 5; $page = 5;
$this->shortUrlService->listShortUrls(ShortUrlsParams::fromRawData(['page' => $page])) $this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
->willReturn(new Paginator(new ArrayAdapter([]))) $this->equalTo(ShortUrlsParams::fromRawData(['page' => $page])),
->shouldBeCalledOnce(); )->willReturn(new Paginator(new ArrayAdapter([])));
$this->commandTester->setInputs(['y']); $this->commandTester->setInputs(['y']);
$this->commandTester->execute(['--page' => $page]); $this->commandTester->execute(['--page' => $page]);
@ -113,15 +111,15 @@ class ListShortUrlsCommandTest extends TestCase
array $notExpectedContents, array $notExpectedContents,
ApiKey $apiKey, ApiKey $apiKey,
): void { ): void {
$this->shortUrlService->listShortUrls(ShortUrlsParams::emptyInstance()) $this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
->willReturn(new Paginator(new ArrayAdapter([ $this->equalTo(ShortUrlsParams::emptyInstance()),
ShortUrl::fromMeta(ShortUrlCreation::fromRawData([ )->willReturn(new Paginator(new ArrayAdapter([
'longUrl' => 'foo.com', ShortUrl::fromMeta(ShortUrlCreation::fromRawData([
'tags' => ['foo', 'bar', 'baz'], 'longUrl' => 'foo.com',
'apiKey' => $apiKey, 'tags' => ['foo', 'bar', 'baz'],
])), 'apiKey' => $apiKey,
]))) ])),
->shouldBeCalledOnce(); ])));
$this->commandTester->setInputs(['y']); $this->commandTester->setInputs(['y']);
$this->commandTester->execute($input); $this->commandTester->execute($input);
@ -189,19 +187,19 @@ class ListShortUrlsCommandTest extends TestCase
?string $startDate = null, ?string $startDate = null,
?string $endDate = null, ?string $endDate = null,
): void { ): void {
$listShortUrls = $this->shortUrlService->listShortUrls(ShortUrlsParams::fromRawData([ $this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
'page' => $page, $this->equalTo(ShortUrlsParams::fromRawData([
'searchTerm' => $searchTerm, 'page' => $page,
'tags' => $tags, 'searchTerm' => $searchTerm,
'tagsMode' => $tagsMode, 'tags' => $tags,
'startDate' => $startDate !== null ? Chronos::parse($startDate)->toAtomString() : null, 'tagsMode' => $tagsMode,
'endDate' => $endDate !== null ? Chronos::parse($endDate)->toAtomString() : null, 'startDate' => $startDate !== null ? Chronos::parse($startDate)->toAtomString() : null,
]))->willReturn(new Paginator(new ArrayAdapter([]))); 'endDate' => $endDate !== null ? Chronos::parse($endDate)->toAtomString() : null,
])),
)->willReturn(new Paginator(new ArrayAdapter([])));
$this->commandTester->setInputs(['n']); $this->commandTester->setInputs(['n']);
$this->commandTester->execute($commandArgs); $this->commandTester->execute($commandArgs);
$listShortUrls->shouldHaveBeenCalledOnce();
} }
public function provideArgs(): iterable public function provideArgs(): iterable
@ -251,14 +249,14 @@ class ListShortUrlsCommandTest extends TestCase
*/ */
public function orderByIsProperlyComputed(array $commandArgs, ?string $expectedOrderBy): void public function orderByIsProperlyComputed(array $commandArgs, ?string $expectedOrderBy): void
{ {
$listShortUrls = $this->shortUrlService->listShortUrls(ShortUrlsParams::fromRawData([ $this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
'orderBy' => $expectedOrderBy, $this->equalTo(ShortUrlsParams::fromRawData([
]))->willReturn(new Paginator(new ArrayAdapter([]))); 'orderBy' => $expectedOrderBy,
])),
)->willReturn(new Paginator(new ArrayAdapter([])));
$this->commandTester->setInputs(['n']); $this->commandTester->setInputs(['n']);
$this->commandTester->execute($commandArgs); $this->commandTester->execute($commandArgs);
$listShortUrls->shouldHaveBeenCalledOnce();
} }
public function provideOrderBy(): iterable public function provideOrderBy(): iterable
@ -273,19 +271,19 @@ class ListShortUrlsCommandTest extends TestCase
/** @test */ /** @test */
public function requestingAllElementsWillSetItemsPerPage(): void public function requestingAllElementsWillSetItemsPerPage(): void
{ {
$listShortUrls = $this->shortUrlService->listShortUrls(ShortUrlsParams::fromRawData([ $this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
'page' => 1, $this->equalTo(ShortUrlsParams::fromRawData([
'searchTerm' => null, 'page' => 1,
'tags' => [], 'searchTerm' => null,
'tagsMode' => TagsMode::ANY->value, 'tags' => [],
'startDate' => null, 'tagsMode' => TagsMode::ANY->value,
'endDate' => null, 'startDate' => null,
'orderBy' => null, 'endDate' => null,
'itemsPerPage' => Paginator::ALL_ITEMS, 'orderBy' => null,
]))->willReturn(new Paginator(new ArrayAdapter([]))); 'itemsPerPage' => Paginator::ALL_ITEMS,
])),
)->willReturn(new Paginator(new ArrayAdapter([])));
$this->commandTester->execute(['--all' => true]); $this->commandTester->execute(['--all' => true]);
$listShortUrls->shouldHaveBeenCalledOnce();
} }
} }