Migrated ProcessRunnerTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-22 14:37:13 +02:00
parent a484455b0b
commit 4cb44be9a0

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 Shlinkio\Shlink\CLI\Util\ProcessRunner; use Shlinkio\Shlink\CLI\Util\ProcessRunner;
use Symfony\Component\Console\Helper\DebugFormatterHelper; use Symfony\Component\Console\Helper\DebugFormatterHelper;
use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\HelperSet;
@ -17,90 +15,73 @@ use Symfony\Component\Process\Process;
class ProcessRunnerTest extends TestCase class ProcessRunnerTest extends TestCase
{ {
use ProphecyTrait;
private ProcessRunner $runner; private ProcessRunner $runner;
private ObjectProphecy $helper; private MockObject $helper;
private ObjectProphecy $formatter; private MockObject $formatter;
private ObjectProphecy $process; private MockObject $process;
private ObjectProphecy $output; private MockObject $output;
protected function setUp(): void protected function setUp(): void
{ {
$this->helper = $this->prophesize(ProcessHelper::class); $this->helper = $this->createMock(ProcessHelper::class);
$this->formatter = $this->prophesize(DebugFormatterHelper::class); $this->formatter = $this->createMock(DebugFormatterHelper::class);
$helperSet = $this->prophesize(HelperSet::class); $helperSet = $this->createMock(HelperSet::class);
$helperSet->get('debug_formatter')->willReturn($this->formatter->reveal()); $helperSet->method('get')->with($this->equalTo('debug_formatter'))->willReturn($this->formatter);
$this->helper->getHelperSet()->willReturn($helperSet->reveal()); $this->helper->method('getHelperSet')->with()->willReturn($helperSet);
$this->process = $this->prophesize(Process::class); $this->process = $this->createMock(Process::class);
$this->output = $this->createMock(OutputInterface::class);
$this->runner = new ProcessRunner($this->helper->reveal(), fn () => $this->process->reveal()); $this->runner = new ProcessRunner($this->helper, fn () => $this->process);
$this->output = $this->prophesize(OutputInterface::class);
} }
/** @test */ /** @test */
public function noMessagesAreWrittenWhenOutputIsNotVerbose(): void public function noMessagesAreWrittenWhenOutputIsNotVerbose(): void
{ {
$isVeryVerbose = $this->output->isVeryVerbose()->willReturn(false); $this->output->expects($this->exactly(2))->method('isVeryVerbose')->with()->willReturn(false);
$isDebug = $this->output->isDebug()->willReturn(false); $this->output->expects($this->once())->method('isDebug')->with()->willReturn(false);
$mustRun = $this->process->mustRun(Argument::cetera())->willReturn($this->process->reveal()); $this->output->expects($this->never())->method('write');
$this->process->expects($this->once())->method('mustRun')->withAnyParameters()->willReturn($this->process);
$this->process->expects($this->never())->method('isSuccessful');
$this->process->expects($this->never())->method('getCommandLine');
$this->helper->expects($this->never())->method('wrapCallback');
$this->formatter->expects($this->never())->method('start');
$this->formatter->expects($this->never())->method('stop');
$this->runner->run($this->output->reveal(), []); $this->runner->run($this->output, []);
$isVeryVerbose->shouldHaveBeenCalledTimes(2);
$isDebug->shouldHaveBeenCalledOnce();
$mustRun->shouldHaveBeenCalledOnce();
$this->process->isSuccessful()->shouldNotHaveBeenCalled();
$this->process->getCommandLine()->shouldNotHaveBeenCalled();
$this->output->write(Argument::cetera())->shouldNotHaveBeenCalled();
$this->helper->wrapCallback(Argument::cetera())->shouldNotHaveBeenCalled();
$this->formatter->start(Argument::cetera())->shouldNotHaveBeenCalled();
$this->formatter->stop(Argument::cetera())->shouldNotHaveBeenCalled();
} }
/** @test */ /** @test */
public function someMessagesAreWrittenWhenOutputIsVerbose(): void public function someMessagesAreWrittenWhenOutputIsVerbose(): void
{ {
$isVeryVerbose = $this->output->isVeryVerbose()->willReturn(true); $this->output->expects($this->exactly(2))->method('isVeryVerbose')->with()->willReturn(true);
$isDebug = $this->output->isDebug()->willReturn(false); $this->output->expects($this->once())->method('isDebug')->with()->willReturn(false);
$mustRun = $this->process->mustRun(Argument::cetera())->willReturn($this->process->reveal()); $this->output->expects($this->exactly(2))->method('write')->withAnyParameters();
$isSuccessful = $this->process->isSuccessful()->willReturn(true); $this->process->expects($this->once())->method('mustRun')->withAnyParameters()->willReturn($this->process);
$getCommandLine = $this->process->getCommandLine()->willReturn('true'); $this->process->expects($this->exactly(2))->method('isSuccessful')->with()->willReturn(true);
$start = $this->formatter->start(Argument::cetera())->willReturn(''); $this->process->expects($this->once())->method('getCommandLine')->with()->willReturn('true');
$stop = $this->formatter->stop(Argument::cetera())->willReturn(''); $this->formatter->expects($this->once())->method('start')->withAnyParameters()->willReturn('');
$this->formatter->expects($this->once())->method('stop')->withAnyParameters()->willReturn('');
$this->helper->expects($this->never())->method('wrapCallback');
$this->runner->run($this->output->reveal(), []); $this->runner->run($this->output, []);
$isVeryVerbose->shouldHaveBeenCalledTimes(2);
$isDebug->shouldHaveBeenCalledOnce();
$mustRun->shouldHaveBeenCalledOnce();
$this->output->write(Argument::cetera())->shouldHaveBeenCalledTimes(2);
$this->helper->wrapCallback(Argument::cetera())->shouldNotHaveBeenCalled();
$isSuccessful->shouldHaveBeenCalledTimes(2);
$getCommandLine->shouldHaveBeenCalledOnce();
$start->shouldHaveBeenCalledOnce();
$stop->shouldHaveBeenCalledOnce();
} }
/** @test */ /** @test */
public function wrapsCallbackWhenOutputIsDebug(): void public function wrapsCallbackWhenOutputIsDebug(): void
{ {
$isVeryVerbose = $this->output->isVeryVerbose()->willReturn(false); $this->output->expects($this->exactly(2))->method('isVeryVerbose')->with()->willReturn(false);
$isDebug = $this->output->isDebug()->willReturn(true); $this->output->expects($this->once())->method('isDebug')->with()->willReturn(true);
$mustRun = $this->process->mustRun(Argument::cetera())->willReturn($this->process->reveal()); $this->output->expects($this->never())->method('write');
$wrapCallback = $this->helper->wrapCallback(Argument::cetera())->willReturn(function (): void { $this->process->expects($this->once())->method('mustRun')->withAnyParameters()->willReturn($this->process);
}); $this->process->expects($this->never())->method('isSuccessful');
$this->process->expects($this->never())->method('getCommandLine');
$this->helper->expects($this->once())->method('wrapCallback')->withAnyParameters()->willReturn(
function (): void {
},
);
$this->formatter->expects($this->never())->method('start');
$this->formatter->expects($this->never())->method('stop');
$this->runner->run($this->output->reveal(), []); $this->runner->run($this->output, []);
$isVeryVerbose->shouldHaveBeenCalledTimes(2);
$isDebug->shouldHaveBeenCalledOnce();
$mustRun->shouldHaveBeenCalledOnce();
$wrapCallback->shouldHaveBeenCalledOnce();
$this->process->isSuccessful()->shouldNotHaveBeenCalled();
$this->process->getCommandLine()->shouldNotHaveBeenCalled();
$this->output->write(Argument::cetera())->shouldNotHaveBeenCalled();
$this->formatter->start(Argument::cetera())->shouldNotHaveBeenCalled();
$this->formatter->stop(Argument::cetera())->shouldNotHaveBeenCalled();
} }
} }