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

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