Migrated MigrateDatabaseCommandTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-22 13:08:05 +02:00
parent 13431ff8cf
commit 101b4daff4

View file

@ -4,9 +4,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\Db; namespace ShlinkioTest\Shlink\CLI\Command\Db;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\Db\MigrateDatabaseCommand; use Shlinkio\Shlink\CLI\Command\Db\MigrateDatabaseCommand;
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface; use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait; use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
@ -21,45 +20,41 @@ class MigrateDatabaseCommandTest extends TestCase
use CliTestUtilsTrait; use CliTestUtilsTrait;
private CommandTester $commandTester; private CommandTester $commandTester;
private ObjectProphecy $processHelper; private MockObject $processHelper;
protected function setUp(): void protected function setUp(): void
{ {
$locker = $this->prophesize(LockFactory::class); $locker = $this->createMock(LockFactory::class);
$lock = $this->prophesize(LockInterface::class); $lock = $this->createMock(LockInterface::class);
$lock->acquire(Argument::any())->willReturn(true); $lock->method('acquire')->withAnyParameters()->willReturn(true);
$lock->release()->will(function (): void { $locker->method('createLock')->withAnyParameters()->willReturn($lock);
});
$locker->createLock(Argument::cetera())->willReturn($lock->reveal());
$phpExecutableFinder = $this->prophesize(PhpExecutableFinder::class); $phpExecutableFinder = $this->createMock(PhpExecutableFinder::class);
$phpExecutableFinder->find(false)->willReturn('/usr/local/bin/php'); $phpExecutableFinder->method('find')->with($this->isFalse())->willReturn('/usr/local/bin/php');
$this->processHelper = $this->prophesize(ProcessRunnerInterface::class); $this->processHelper = $this->createMock(ProcessRunnerInterface::class);
$command = new MigrateDatabaseCommand( $command = new MigrateDatabaseCommand($locker, $this->processHelper, $phpExecutableFinder);
$locker->reveal(),
$this->processHelper->reveal(),
$phpExecutableFinder->reveal(),
);
$this->commandTester = $this->testerForCommand($command); $this->commandTester = $this->testerForCommand($command);
} }
/** @test */ /** @test */
public function migrationsCommandIsRunWithProperVerbosity(): void public function migrationsCommandIsRunWithProperVerbosity(): void
{ {
$runCommand = $this->processHelper->run(Argument::type(OutputInterface::class), [ $this->processHelper->expects($this->once())->method('run')->with(
'/usr/local/bin/php', $this->isInstanceOf(OutputInterface::class),
MigrateDatabaseCommand::DOCTRINE_MIGRATIONS_SCRIPT, $this->equalTo([
MigrateDatabaseCommand::DOCTRINE_MIGRATE_COMMAND, '/usr/local/bin/php',
'--no-interaction', MigrateDatabaseCommand::DOCTRINE_MIGRATIONS_SCRIPT,
]); MigrateDatabaseCommand::DOCTRINE_MIGRATE_COMMAND,
'--no-interaction',
]),
);
$this->commandTester->execute([]); $this->commandTester->execute([]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
self::assertStringContainsString('Migrating database...', $output); self::assertStringContainsString('Migrating database...', $output);
self::assertStringContainsString('Database properly migrated!', $output); self::assertStringContainsString('Database properly migrated!', $output);
$runCommand->shouldHaveBeenCalledOnce();
} }
} }