Migrated CliTestUtilsTrait to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-22 12:46:16 +02:00
parent 983e3c9eaa
commit 58db902084
3 changed files with 25 additions and 20 deletions

View File

@ -4,9 +4,9 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI;
use Prophecy\Argument;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;
@ -14,21 +14,19 @@ use Symfony\Component\Console\Tester\CommandTester;
trait CliTestUtilsTrait
{
use ProphecyTrait;
use ProphecyTrait; // TODO Remove
/**
* @return ObjectProphecy|Command
* @return MockObject & Command
*/
private function createCommandMock(string $name): ObjectProphecy
private function createCommandMock(string $name): MockObject
{
$command = $this->prophesize(Command::class);
$command->getName()->willReturn($name);
$command->getDefinition()->willReturn($name);
$command->isEnabled()->willReturn(true);
$command->getAliases()->willReturn([]);
$command->getDefinition()->willReturn(new InputDefinition());
$command->setApplication(Argument::type(Application::class))->willReturn(function (): void {
});
$command = $this->createMock(Command::class);
$command->method('getName')->willReturn($name);
$command->method('isEnabled')->willReturn(true);
$command->method('getAliases')->willReturn([]);
$command->method('getDefinition')->willReturn(new InputDefinition());
$command->method('setApplication')->with(Assert::isInstanceOf(Application::class));
return $command;
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
@ -38,7 +39,7 @@ class LocateVisitsCommandTest extends TestCase
private ObjectProphecy $visitService;
private ObjectProphecy $visitToLocation;
private ObjectProphecy $lock;
private ObjectProphecy $downloadDbCommand;
private MockObject $downloadDbCommand;
protected function setUp(): void
{
@ -59,9 +60,7 @@ class LocateVisitsCommandTest extends TestCase
);
$this->downloadDbCommand = $this->createCommandMock(DownloadGeoLiteDbCommand::NAME);
$this->downloadDbCommand->run(Argument::cetera())->willReturn(ExitCodes::EXIT_SUCCESS);
$this->commandTester = $this->testerForCommand($command, $this->downloadDbCommand->reveal());
$this->commandTester = $this->testerForCommand($command, $this->downloadDbCommand);
}
/**
@ -87,6 +86,7 @@ class LocateVisitsCommandTest extends TestCase
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any())->willReturn(
Location::emptyInstance(),
);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->commandTester->setInputs(['y']);
$this->commandTester->execute($args);
@ -126,6 +126,7 @@ class LocateVisitsCommandTest extends TestCase
$this->invokeHelperMethods($visit, $location),
);
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any())->willThrow($e);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
@ -154,6 +155,7 @@ class LocateVisitsCommandTest extends TestCase
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any())->willThrow(
IpCannotBeLocatedException::forError(WrongIpException::fromIpAddress('1.2.3.4')),
);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
@ -183,6 +185,7 @@ class LocateVisitsCommandTest extends TestCase
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(function (): void {
});
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any());
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$output = $this->commandTester->getDisplay();
@ -198,7 +201,7 @@ class LocateVisitsCommandTest extends TestCase
/** @test */
public function showsProperMessageWhenGeoLiteUpdateFails(): void
{
$this->downloadDbCommand->run(Argument::cetera())->willReturn(ExitCodes::EXIT_FAILURE);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_FAILURE);
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
@ -210,6 +213,8 @@ class LocateVisitsCommandTest extends TestCase
/** @test */
public function providingAllFlagOnItsOwnDisplaysNotice(): void
{
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->commandTester->execute(['--all' => true]);
$output = $this->commandTester->getDisplay();
@ -222,6 +227,8 @@ class LocateVisitsCommandTest extends TestCase
*/
public function processingAllCancelsCommandIfUserDoesNotActivelyAgreeToConfirmation(array $inputs): void
{
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Execution aborted');

View File

@ -31,8 +31,8 @@ class ApplicationFactoryTest extends TestCase
'baz' => 'baz',
],
]);
$sm->setService('foo', $this->createCommandMock('foo')->reveal());
$sm->setService('bar', $this->createCommandMock('bar')->reveal());
$sm->setService('foo', $this->createCommandMock('foo'));
$sm->setService('bar', $this->createCommandMock('bar'));
$instance = ($this->factory)($sm);