Migrated ShlinkTableTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-22 14:40:35 +02:00
parent 4cb44be9a0
commit 32417e40cb
2 changed files with 22 additions and 27 deletions

View File

@ -40,7 +40,7 @@ class ProcessRunnerTest extends TestCase
$this->output->expects($this->exactly(2))->method('isVeryVerbose')->with()->willReturn(false); $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->once())->method('isDebug')->with()->willReturn(false);
$this->output->expects($this->never())->method('write'); $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('isSuccessful');
$this->process->expects($this->never())->method('getCommandLine'); $this->process->expects($this->never())->method('getCommandLine');
$this->helper->expects($this->never())->method('wrapCallback'); $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->exactly(2))->method('isVeryVerbose')->with()->willReturn(true);
$this->output->expects($this->once())->method('isDebug')->with()->willReturn(false); $this->output->expects($this->once())->method('isDebug')->with()->willReturn(false);
$this->output->expects($this->exactly(2))->method('write')->withAnyParameters(); $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->exactly(2))->method('isSuccessful')->with()->willReturn(true);
$this->process->expects($this->once())->method('getCommandLine')->with()->willReturn('true'); $this->process->expects($this->once())->method('getCommandLine')->with()->willReturn('true');
$this->formatter->expects($this->once())->method('start')->withAnyParameters()->willReturn(''); $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->exactly(2))->method('isVeryVerbose')->with()->willReturn(false);
$this->output->expects($this->once())->method('isDebug')->with()->willReturn(true); $this->output->expects($this->once())->method('isDebug')->with()->willReturn(true);
$this->output->expects($this->never())->method('write'); $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('isSuccessful');
$this->process->expects($this->never())->method('getCommandLine'); $this->process->expects($this->never())->method('getCommandLine');
$this->helper->expects($this->once())->method('wrapCallback')->withAnyParameters()->willReturn( $this->helper->expects($this->once())->method('wrapCallback')->withAnyParameters()->willReturn(

View File

@ -4,10 +4,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Util; namespace ShlinkioTest\Shlink\CLI\Util;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use ReflectionObject; use ReflectionObject;
use Shlinkio\Shlink\CLI\Util\ShlinkTable; use Shlinkio\Shlink\CLI\Util\ShlinkTable;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
@ -16,15 +14,13 @@ use Symfony\Component\Console\Output\OutputInterface;
class ShlinkTableTest extends TestCase class ShlinkTableTest extends TestCase
{ {
use ProphecyTrait;
private ShlinkTable $shlinkTable; private ShlinkTable $shlinkTable;
private ObjectProphecy $baseTable; private MockObject $baseTable;
protected function setUp(): void protected function setUp(): void
{ {
$this->baseTable = $this->prophesize(Table::class); $this->baseTable = $this->createMock(Table::class);
$this->shlinkTable = ShlinkTable::fromBaseTable($this->baseTable->reveal()); $this->shlinkTable = ShlinkTable::fromBaseTable($this->baseTable);
} }
/** @test */ /** @test */
@ -35,29 +31,28 @@ class ShlinkTableTest extends TestCase
$headerTitle = 'Header'; $headerTitle = 'Header';
$footerTitle = 'Footer'; $footerTitle = 'Footer';
$setStyle = $this->baseTable->setStyle(Argument::type(TableStyle::class))->willReturn( $this->baseTable->expects($this->once())->method('setStyle')->with(
$this->baseTable->reveal(), $this->isInstanceOf(TableStyle::class)
); )->willReturnSelf();
$setHeaders = $this->baseTable->setHeaders($headers)->willReturn($this->baseTable->reveal()); $this->baseTable->expects($this->once())->method('setHeaders')->with(
$setRows = $this->baseTable->setRows($rows)->willReturn($this->baseTable->reveal()); $this->equalTo($headers),
$setFooterTitle = $this->baseTable->setFooterTitle($footerTitle)->willReturn($this->baseTable->reveal()); )->willReturnSelf();
$setHeaderTitle = $this->baseTable->setHeaderTitle($headerTitle)->willReturn($this->baseTable->reveal()); $this->baseTable->expects($this->once())->method('setRows')->with($this->equalTo($rows))->willReturnSelf();
$render = $this->baseTable->render()->willReturn($this->baseTable->reveal()); $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); $this->shlinkTable->render($headers, $rows, $footerTitle, $headerTitle);
$setStyle->shouldHaveBeenCalledOnce();
$setHeaders->shouldHaveBeenCalledOnce();
$setRows->shouldHaveBeenCalledOnce();
$setFooterTitle->shouldHaveBeenCalledOnce();
$setHeaderTitle->shouldHaveBeenCalledOnce();
$render->shouldHaveBeenCalledOnce();
} }
/** @test */ /** @test */
public function newTableIsCreatedForFactoryMethod(): void public function newTableIsCreatedForFactoryMethod(): void
{ {
$instance = ShlinkTable::default($this->prophesize(OutputInterface::class)->reveal()); $instance = ShlinkTable::default($this->createMock(OutputInterface::class));
$ref = new ReflectionObject($instance); $ref = new ReflectionObject($instance);
$baseTable = $ref->getProperty('baseTable'); $baseTable = $ref->getProperty('baseTable');