mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Migrated CreateDatabaseCommandTest to use PHPUnit mocks
This commit is contained in:
parent
4cdcad29df
commit
13431ff8cf
@ -9,9 +9,8 @@ use Doctrine\DBAL\Driver;
|
|||||||
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
||||||
use Doctrine\DBAL\Platforms\SqlitePlatform;
|
use Doctrine\DBAL\Platforms\SqlitePlatform;
|
||||||
use Doctrine\DBAL\Schema\AbstractSchemaManager;
|
use Doctrine\DBAL\Schema\AbstractSchemaManager;
|
||||||
|
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\CreateDatabaseCommand;
|
use Shlinkio\Shlink\CLI\Command\Db\CreateDatabaseCommand;
|
||||||
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
|
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
|
||||||
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
||||||
@ -28,40 +27,37 @@ class CreateDatabaseCommandTest extends TestCase
|
|||||||
use CliTestUtilsTrait;
|
use CliTestUtilsTrait;
|
||||||
|
|
||||||
private CommandTester $commandTester;
|
private CommandTester $commandTester;
|
||||||
private ObjectProphecy $processHelper;
|
private MockObject $processHelper;
|
||||||
private ObjectProphecy $regularConn;
|
private MockObject $regularConn;
|
||||||
private ObjectProphecy $schemaManager;
|
private MockObject $schemaManager;
|
||||||
private ObjectProphecy $driver;
|
private MockObject $driver;
|
||||||
|
|
||||||
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);
|
||||||
$this->schemaManager = $this->prophesize(AbstractSchemaManager::class);
|
$this->schemaManager = $this->createMock(AbstractSchemaManager::class);
|
||||||
|
|
||||||
$this->regularConn = $this->prophesize(Connection::class);
|
$this->regularConn = $this->createMock(Connection::class);
|
||||||
$this->regularConn->createSchemaManager()->willReturn($this->schemaManager->reveal());
|
$this->regularConn->method('createSchemaManager')->willReturn($this->schemaManager);
|
||||||
$this->driver = $this->prophesize(Driver::class);
|
$this->driver = $this->createMock(Driver::class);
|
||||||
$this->regularConn->getDriver()->willReturn($this->driver->reveal());
|
$this->regularConn->method('getDriver')->willReturn($this->driver);
|
||||||
$this->driver->getDatabasePlatform()->willReturn($this->prophesize(AbstractPlatform::class)->reveal());
|
$noDbNameConn = $this->createMock(Connection::class);
|
||||||
$noDbNameConn = $this->prophesize(Connection::class);
|
$noDbNameConn->method('createSchemaManager')->withAnyParameters()->willReturn($this->schemaManager);
|
||||||
$noDbNameConn->createSchemaManager()->willReturn($this->schemaManager->reveal());
|
|
||||||
|
|
||||||
$command = new CreateDatabaseCommand(
|
$command = new CreateDatabaseCommand(
|
||||||
$locker->reveal(),
|
$locker,
|
||||||
$this->processHelper->reveal(),
|
$this->processHelper,
|
||||||
$phpExecutableFinder->reveal(),
|
$phpExecutableFinder,
|
||||||
$this->regularConn->reveal(),
|
$this->regularConn,
|
||||||
$noDbNameConn->reveal(),
|
$noDbNameConn,
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->commandTester = $this->testerForCommand($command);
|
$this->commandTester = $this->testerForCommand($command);
|
||||||
@ -71,38 +67,33 @@ class CreateDatabaseCommandTest extends TestCase
|
|||||||
public function successMessageIsPrintedIfDatabaseAlreadyExists(): void
|
public function successMessageIsPrintedIfDatabaseAlreadyExists(): void
|
||||||
{
|
{
|
||||||
$shlinkDatabase = 'shlink_database';
|
$shlinkDatabase = 'shlink_database';
|
||||||
$getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]);
|
$this->regularConn->expects($this->once())->method('getParams')->willReturn(['dbname' => $shlinkDatabase]);
|
||||||
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']);
|
$this->schemaManager->expects($this->once())->method('listDatabases')->willReturn(
|
||||||
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
|
['foo', $shlinkDatabase, 'bar'],
|
||||||
});
|
);
|
||||||
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table']);
|
$this->schemaManager->expects($this->never())->method('createDatabase');
|
||||||
|
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn(['foo_table', 'bar_table']);
|
||||||
|
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
|
||||||
|
|
||||||
$this->commandTester->execute([]);
|
$this->commandTester->execute([]);
|
||||||
$output = $this->commandTester->getDisplay();
|
$output = $this->commandTester->getDisplay();
|
||||||
|
|
||||||
self::assertStringContainsString('Database already exists. Run "db:migrate" command', $output);
|
self::assertStringContainsString('Database already exists. Run "db:migrate" command', $output);
|
||||||
$getDatabase->shouldHaveBeenCalledOnce();
|
|
||||||
$listDatabases->shouldHaveBeenCalledOnce();
|
|
||||||
$createDatabase->shouldNotHaveBeenCalled();
|
|
||||||
$listTables->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function databaseIsCreatedIfItDoesNotExist(): void
|
public function databaseIsCreatedIfItDoesNotExist(): void
|
||||||
{
|
{
|
||||||
$shlinkDatabase = 'shlink_database';
|
$shlinkDatabase = 'shlink_database';
|
||||||
$getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]);
|
$this->regularConn->expects($this->once())->method('getParams')->willReturn(['dbname' => $shlinkDatabase]);
|
||||||
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', 'bar']);
|
$this->schemaManager->expects($this->once())->method('listDatabases')->willReturn(['foo', 'bar']);
|
||||||
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
|
$this->schemaManager->expects($this->once())->method('createDatabase')->with($this->equalTo($shlinkDatabase));
|
||||||
});
|
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn(
|
||||||
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table', MIGRATIONS_TABLE]);
|
['foo_table', 'bar_table', MIGRATIONS_TABLE],
|
||||||
|
);
|
||||||
|
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
|
||||||
|
|
||||||
$this->commandTester->execute([]);
|
$this->commandTester->execute([]);
|
||||||
|
|
||||||
$getDatabase->shouldHaveBeenCalledOnce();
|
|
||||||
$listDatabases->shouldHaveBeenCalledOnce();
|
|
||||||
$createDatabase->shouldHaveBeenCalledOnce();
|
|
||||||
$listTables->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -112,28 +103,28 @@ class CreateDatabaseCommandTest extends TestCase
|
|||||||
public function tablesAreCreatedIfDatabaseIsEmpty(array $tables): void
|
public function tablesAreCreatedIfDatabaseIsEmpty(array $tables): void
|
||||||
{
|
{
|
||||||
$shlinkDatabase = 'shlink_database';
|
$shlinkDatabase = 'shlink_database';
|
||||||
$getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]);
|
$this->regularConn->expects($this->once())->method('getParams')->willReturn(['dbname' => $shlinkDatabase]);
|
||||||
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']);
|
$this->schemaManager->expects($this->once())->method('listDatabases')->willReturn(
|
||||||
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
|
['foo', $shlinkDatabase, 'bar'],
|
||||||
});
|
);
|
||||||
$listTables = $this->schemaManager->listTableNames()->willReturn($tables);
|
$this->schemaManager->expects($this->never())->method('createDatabase');
|
||||||
$runCommand = $this->processHelper->run(Argument::type(OutputInterface::class), [
|
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn($tables);
|
||||||
'/usr/local/bin/php',
|
$this->processHelper->expects($this->once())->method('run')->with(
|
||||||
CreateDatabaseCommand::DOCTRINE_SCRIPT,
|
$this->isInstanceOf(OutputInterface::class),
|
||||||
CreateDatabaseCommand::DOCTRINE_CREATE_SCHEMA_COMMAND,
|
$this->equalTo([
|
||||||
'--no-interaction',
|
'/usr/local/bin/php',
|
||||||
]);
|
CreateDatabaseCommand::DOCTRINE_SCRIPT,
|
||||||
|
CreateDatabaseCommand::DOCTRINE_CREATE_SCHEMA_COMMAND,
|
||||||
|
'--no-interaction',
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
|
||||||
|
|
||||||
$this->commandTester->execute([]);
|
$this->commandTester->execute([]);
|
||||||
$output = $this->commandTester->getDisplay();
|
$output = $this->commandTester->getDisplay();
|
||||||
|
|
||||||
self::assertStringContainsString('Creating database tables...', $output);
|
self::assertStringContainsString('Creating database tables...', $output);
|
||||||
self::assertStringContainsString('Database properly created!', $output);
|
self::assertStringContainsString('Database properly created!', $output);
|
||||||
$getDatabase->shouldHaveBeenCalledOnce();
|
|
||||||
$listDatabases->shouldHaveBeenCalledOnce();
|
|
||||||
$createDatabase->shouldNotHaveBeenCalled();
|
|
||||||
$listTables->shouldHaveBeenCalledOnce();
|
|
||||||
$runCommand->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideEmptyDatabase(): iterable
|
public function provideEmptyDatabase(): iterable
|
||||||
@ -145,20 +136,13 @@ class CreateDatabaseCommandTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
public function databaseCheckIsSkippedForSqlite(): void
|
public function databaseCheckIsSkippedForSqlite(): void
|
||||||
{
|
{
|
||||||
$this->driver->getDatabasePlatform()->willReturn($this->prophesize(SqlitePlatform::class)->reveal());
|
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(SqlitePlatform::class));
|
||||||
|
|
||||||
$shlinkDatabase = 'shlink_database';
|
$this->regularConn->expects($this->never())->method('getParams');
|
||||||
$getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]);
|
$this->schemaManager->expects($this->never())->method('listDatabases');
|
||||||
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', 'bar']);
|
$this->schemaManager->expects($this->never())->method('createDatabase');
|
||||||
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
|
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn(['foo_table', 'bar_table']);
|
||||||
});
|
|
||||||
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table']);
|
|
||||||
|
|
||||||
$this->commandTester->execute([]);
|
$this->commandTester->execute([]);
|
||||||
|
|
||||||
$getDatabase->shouldNotHaveBeenCalled();
|
|
||||||
$listDatabases->shouldNotHaveBeenCalled();
|
|
||||||
$createDatabase->shouldNotHaveBeenCalled();
|
|
||||||
$listTables->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user