From 4872bd3a92682ed9ea5905809dc41cf4a3e17850 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 22 Oct 2022 13:42:46 +0200 Subject: [PATCH] Migrated ListShortUrlsCommandTest to use PHPUnit mocks --- .../ShortUrl/ListShortUrlsCommandTest.php | 98 +++++++++---------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php index 5659059a..dfd1a028 100644 --- a/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php @@ -6,9 +6,8 @@ namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl; use Cake\Chronos\Chronos; use Pagerfanta\Adapter\ArrayAdapter; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\CLI\Command\ShortUrl\ListShortUrlsCommand; use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; @@ -31,12 +30,12 @@ class ListShortUrlsCommandTest extends TestCase use CliTestUtilsTrait; private CommandTester $commandTester; - private ObjectProphecy $shortUrlService; + private MockObject $shortUrlService; protected function setUp(): void { - $this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class); - $command = new ListShortUrlsCommand($this->shortUrlService->reveal(), new ShortUrlDataTransformer( + $this->shortUrlService = $this->createMock(ShortUrlServiceInterface::class); + $command = new ListShortUrlsCommand($this->shortUrlService, new ShortUrlDataTransformer( new ShortUrlStringifier([]), )); $this->commandTester = $this->testerForCommand($command); @@ -51,9 +50,8 @@ class ListShortUrlsCommandTest extends TestCase $data[] = ShortUrl::withLongUrl('url_' . $i); } - $this->shortUrlService->listShortUrls(Argument::cetera()) - ->will(fn () => new Paginator(new ArrayAdapter($data))) - ->shouldBeCalledTimes(3); + $this->shortUrlService->expects($this->exactly(3))->method('listShortUrls')->withAnyParameters() + ->willReturnCallback(fn () => new Paginator(new ArrayAdapter($data))); $this->commandTester->setInputs(['y', 'y', 'n']); $this->commandTester->execute([]); @@ -74,9 +72,9 @@ class ListShortUrlsCommandTest extends TestCase $data[] = ShortUrl::withLongUrl('url_' . $i); } - $this->shortUrlService->listShortUrls(ShortUrlsParams::emptyInstance()) - ->willReturn(new Paginator(new ArrayAdapter($data))) - ->shouldBeCalledOnce(); + $this->shortUrlService->expects($this->once())->method('listShortUrls')->with( + $this->equalTo(ShortUrlsParams::emptyInstance()) + )->willReturn(new Paginator(new ArrayAdapter($data))); $this->commandTester->setInputs(['n']); $this->commandTester->execute([]); @@ -95,9 +93,9 @@ class ListShortUrlsCommandTest extends TestCase public function passingPageWillMakeListStartOnThatPage(): void { $page = 5; - $this->shortUrlService->listShortUrls(ShortUrlsParams::fromRawData(['page' => $page])) - ->willReturn(new Paginator(new ArrayAdapter([]))) - ->shouldBeCalledOnce(); + $this->shortUrlService->expects($this->once())->method('listShortUrls')->with( + $this->equalTo(ShortUrlsParams::fromRawData(['page' => $page])), + )->willReturn(new Paginator(new ArrayAdapter([]))); $this->commandTester->setInputs(['y']); $this->commandTester->execute(['--page' => $page]); @@ -113,15 +111,15 @@ class ListShortUrlsCommandTest extends TestCase array $notExpectedContents, ApiKey $apiKey, ): void { - $this->shortUrlService->listShortUrls(ShortUrlsParams::emptyInstance()) - ->willReturn(new Paginator(new ArrayAdapter([ - ShortUrl::fromMeta(ShortUrlCreation::fromRawData([ - 'longUrl' => 'foo.com', - 'tags' => ['foo', 'bar', 'baz'], - 'apiKey' => $apiKey, - ])), - ]))) - ->shouldBeCalledOnce(); + $this->shortUrlService->expects($this->once())->method('listShortUrls')->with( + $this->equalTo(ShortUrlsParams::emptyInstance()), + )->willReturn(new Paginator(new ArrayAdapter([ + ShortUrl::fromMeta(ShortUrlCreation::fromRawData([ + 'longUrl' => 'foo.com', + 'tags' => ['foo', 'bar', 'baz'], + 'apiKey' => $apiKey, + ])), + ]))); $this->commandTester->setInputs(['y']); $this->commandTester->execute($input); @@ -189,19 +187,19 @@ class ListShortUrlsCommandTest extends TestCase ?string $startDate = null, ?string $endDate = null, ): void { - $listShortUrls = $this->shortUrlService->listShortUrls(ShortUrlsParams::fromRawData([ - 'page' => $page, - 'searchTerm' => $searchTerm, - 'tags' => $tags, - 'tagsMode' => $tagsMode, - 'startDate' => $startDate !== null ? Chronos::parse($startDate)->toAtomString() : null, - 'endDate' => $endDate !== null ? Chronos::parse($endDate)->toAtomString() : null, - ]))->willReturn(new Paginator(new ArrayAdapter([]))); + $this->shortUrlService->expects($this->once())->method('listShortUrls')->with( + $this->equalTo(ShortUrlsParams::fromRawData([ + 'page' => $page, + 'searchTerm' => $searchTerm, + 'tags' => $tags, + 'tagsMode' => $tagsMode, + 'startDate' => $startDate !== null ? Chronos::parse($startDate)->toAtomString() : null, + 'endDate' => $endDate !== null ? Chronos::parse($endDate)->toAtomString() : null, + ])), + )->willReturn(new Paginator(new ArrayAdapter([]))); $this->commandTester->setInputs(['n']); $this->commandTester->execute($commandArgs); - - $listShortUrls->shouldHaveBeenCalledOnce(); } public function provideArgs(): iterable @@ -251,14 +249,14 @@ class ListShortUrlsCommandTest extends TestCase */ public function orderByIsProperlyComputed(array $commandArgs, ?string $expectedOrderBy): void { - $listShortUrls = $this->shortUrlService->listShortUrls(ShortUrlsParams::fromRawData([ - 'orderBy' => $expectedOrderBy, - ]))->willReturn(new Paginator(new ArrayAdapter([]))); + $this->shortUrlService->expects($this->once())->method('listShortUrls')->with( + $this->equalTo(ShortUrlsParams::fromRawData([ + 'orderBy' => $expectedOrderBy, + ])), + )->willReturn(new Paginator(new ArrayAdapter([]))); $this->commandTester->setInputs(['n']); $this->commandTester->execute($commandArgs); - - $listShortUrls->shouldHaveBeenCalledOnce(); } public function provideOrderBy(): iterable @@ -273,19 +271,19 @@ class ListShortUrlsCommandTest extends TestCase /** @test */ public function requestingAllElementsWillSetItemsPerPage(): void { - $listShortUrls = $this->shortUrlService->listShortUrls(ShortUrlsParams::fromRawData([ - 'page' => 1, - 'searchTerm' => null, - 'tags' => [], - 'tagsMode' => TagsMode::ANY->value, - 'startDate' => null, - 'endDate' => null, - 'orderBy' => null, - 'itemsPerPage' => Paginator::ALL_ITEMS, - ]))->willReturn(new Paginator(new ArrayAdapter([]))); + $this->shortUrlService->expects($this->once())->method('listShortUrls')->with( + $this->equalTo(ShortUrlsParams::fromRawData([ + 'page' => 1, + 'searchTerm' => null, + 'tags' => [], + 'tagsMode' => TagsMode::ANY->value, + 'startDate' => null, + 'endDate' => null, + 'orderBy' => null, + 'itemsPerPage' => Paginator::ALL_ITEMS, + ])), + )->willReturn(new Paginator(new ArrayAdapter([]))); $this->commandTester->execute(['--all' => true]); - - $listShortUrls->shouldHaveBeenCalledOnce(); } }