From 32417e40cbe527d9e520594fb74fe6a80110cc53 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 22 Oct 2022 14:40:35 +0200 Subject: [PATCH] Migrated ShlinkTableTest to use PHPUnit mocks --- module/CLI/test/Util/ProcessRunnerTest.php | 6 +-- module/CLI/test/Util/ShlinkTableTest.php | 43 ++++++++++------------ 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/module/CLI/test/Util/ProcessRunnerTest.php b/module/CLI/test/Util/ProcessRunnerTest.php index 276b5abb..3e2e5ed0 100644 --- a/module/CLI/test/Util/ProcessRunnerTest.php +++ b/module/CLI/test/Util/ProcessRunnerTest.php @@ -40,7 +40,7 @@ class ProcessRunnerTest extends TestCase $this->output->expects($this->exactly(2))->method('isVeryVerbose')->with()->willReturn(false); $this->output->expects($this->once())->method('isDebug')->with()->willReturn(false); $this->output->expects($this->never())->method('write'); - $this->process->expects($this->once())->method('mustRun')->withAnyParameters()->willReturn($this->process); + $this->process->expects($this->once())->method('mustRun')->withAnyParameters()->willReturnSelf(); $this->process->expects($this->never())->method('isSuccessful'); $this->process->expects($this->never())->method('getCommandLine'); $this->helper->expects($this->never())->method('wrapCallback'); @@ -56,7 +56,7 @@ class ProcessRunnerTest extends TestCase $this->output->expects($this->exactly(2))->method('isVeryVerbose')->with()->willReturn(true); $this->output->expects($this->once())->method('isDebug')->with()->willReturn(false); $this->output->expects($this->exactly(2))->method('write')->withAnyParameters(); - $this->process->expects($this->once())->method('mustRun')->withAnyParameters()->willReturn($this->process); + $this->process->expects($this->once())->method('mustRun')->withAnyParameters()->willReturnSelf(); $this->process->expects($this->exactly(2))->method('isSuccessful')->with()->willReturn(true); $this->process->expects($this->once())->method('getCommandLine')->with()->willReturn('true'); $this->formatter->expects($this->once())->method('start')->withAnyParameters()->willReturn(''); @@ -72,7 +72,7 @@ class ProcessRunnerTest extends TestCase $this->output->expects($this->exactly(2))->method('isVeryVerbose')->with()->willReturn(false); $this->output->expects($this->once())->method('isDebug')->with()->willReturn(true); $this->output->expects($this->never())->method('write'); - $this->process->expects($this->once())->method('mustRun')->withAnyParameters()->willReturn($this->process); + $this->process->expects($this->once())->method('mustRun')->withAnyParameters()->willReturnSelf(); $this->process->expects($this->never())->method('isSuccessful'); $this->process->expects($this->never())->method('getCommandLine'); $this->helper->expects($this->once())->method('wrapCallback')->withAnyParameters()->willReturn( diff --git a/module/CLI/test/Util/ShlinkTableTest.php b/module/CLI/test/Util/ShlinkTableTest.php index ffe1f30d..a9a6f3f5 100644 --- a/module/CLI/test/Util/ShlinkTableTest.php +++ b/module/CLI/test/Util/ShlinkTableTest.php @@ -4,10 +4,8 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\CLI\Util; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; -use Prophecy\PhpUnit\ProphecyTrait; -use Prophecy\Prophecy\ObjectProphecy; use ReflectionObject; use Shlinkio\Shlink\CLI\Util\ShlinkTable; use Symfony\Component\Console\Helper\Table; @@ -16,15 +14,13 @@ use Symfony\Component\Console\Output\OutputInterface; class ShlinkTableTest extends TestCase { - use ProphecyTrait; - private ShlinkTable $shlinkTable; - private ObjectProphecy $baseTable; + private MockObject $baseTable; protected function setUp(): void { - $this->baseTable = $this->prophesize(Table::class); - $this->shlinkTable = ShlinkTable::fromBaseTable($this->baseTable->reveal()); + $this->baseTable = $this->createMock(Table::class); + $this->shlinkTable = ShlinkTable::fromBaseTable($this->baseTable); } /** @test */ @@ -35,29 +31,28 @@ class ShlinkTableTest extends TestCase $headerTitle = 'Header'; $footerTitle = 'Footer'; - $setStyle = $this->baseTable->setStyle(Argument::type(TableStyle::class))->willReturn( - $this->baseTable->reveal(), - ); - $setHeaders = $this->baseTable->setHeaders($headers)->willReturn($this->baseTable->reveal()); - $setRows = $this->baseTable->setRows($rows)->willReturn($this->baseTable->reveal()); - $setFooterTitle = $this->baseTable->setFooterTitle($footerTitle)->willReturn($this->baseTable->reveal()); - $setHeaderTitle = $this->baseTable->setHeaderTitle($headerTitle)->willReturn($this->baseTable->reveal()); - $render = $this->baseTable->render()->willReturn($this->baseTable->reveal()); + $this->baseTable->expects($this->once())->method('setStyle')->with( + $this->isInstanceOf(TableStyle::class) + )->willReturnSelf(); + $this->baseTable->expects($this->once())->method('setHeaders')->with( + $this->equalTo($headers), + )->willReturnSelf(); + $this->baseTable->expects($this->once())->method('setRows')->with($this->equalTo($rows))->willReturnSelf(); + $this->baseTable->expects($this->once())->method('setFooterTitle')->with( + $this->equalTo($footerTitle), + )->willReturnSelf(); + $this->baseTable->expects($this->once())->method('setHeaderTitle')->with( + $this->equalTo($headerTitle), + )->willReturnSelf(); + $this->baseTable->expects($this->once())->method('render')->with()->willReturnSelf(); $this->shlinkTable->render($headers, $rows, $footerTitle, $headerTitle); - - $setStyle->shouldHaveBeenCalledOnce(); - $setHeaders->shouldHaveBeenCalledOnce(); - $setRows->shouldHaveBeenCalledOnce(); - $setFooterTitle->shouldHaveBeenCalledOnce(); - $setHeaderTitle->shouldHaveBeenCalledOnce(); - $render->shouldHaveBeenCalledOnce(); } /** @test */ public function newTableIsCreatedForFactoryMethod(): void { - $instance = ShlinkTable::default($this->prophesize(OutputInterface::class)->reveal()); + $instance = ShlinkTable::default($this->createMock(OutputInterface::class)); $ref = new ReflectionObject($instance); $baseTable = $ref->getProperty('baseTable');