diff --git a/module/CLI/src/Command/Db/CreateDatabaseCommand.php b/module/CLI/src/Command/Db/CreateDatabaseCommand.php index c507a601..1ac933fa 100644 --- a/module/CLI/src/Command/Db/CreateDatabaseCommand.php +++ b/module/CLI/src/Command/Db/CreateDatabaseCommand.php @@ -18,11 +18,11 @@ use function Functional\contains; class CreateDatabaseCommand extends AbstractDatabaseCommand { public const NAME = 'db:create'; - private const DOCTRINE_HELPER_SCRIPT = 'vendor/doctrine/orm/bin/doctrine.php'; - private const DOCTRINE_HELPER_COMMAND = 'orm:schema-tool:create'; + public const DOCTRINE_HELPER_SCRIPT = 'vendor/doctrine/orm/bin/doctrine.php'; + public const DOCTRINE_HELPER_COMMAND = 'orm:schema-tool:create'; /** @var Connection */ - private $conn; + private $regularConn; /** @var Connection */ private $noDbNameConn; @@ -34,7 +34,7 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand Connection $noDbNameConn ) { parent::__construct($locker, $processHelper, $phpFinder); - $this->conn = $conn; + $this->regularConn = $conn; $this->noDbNameConn = $noDbNameConn; } @@ -72,7 +72,7 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand // Otherwise, it will fail to connect and will not be able to create the new database $schemaManager = $this->noDbNameConn->getSchemaManager(); $databases = $schemaManager->listDatabases(); - $shlinkDatabase = $this->conn->getDatabase(); + $shlinkDatabase = $this->regularConn->getDatabase(); if (! contains($databases, $shlinkDatabase)) { $schemaManager->createDatabase($shlinkDatabase); @@ -83,7 +83,7 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand { // If at least one of the shlink tables exist, we will consider the database exists somehow. // Any inconsistency will be taken care by the migrations - $schemaManager = $this->conn->getSchemaManager(); + $schemaManager = $this->regularConn->getSchemaManager(); return ! empty($schemaManager->listTableNames()); } diff --git a/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php b/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php new file mode 100644 index 00000000..33e9d94c --- /dev/null +++ b/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php @@ -0,0 +1,130 @@ +prophesize(Locker::class); + $lock = $this->prophesize(LockInterface::class); + $lock->acquire(Argument::any())->willReturn(true); + $lock->release()->will(function () { + }); + $locker->createLock(Argument::cetera())->willReturn($lock->reveal()); + + $phpExecutableFinder = $this->prophesize(PhpExecutableFinder::class); + $phpExecutableFinder->find(false)->willReturn('/usr/local/bin/php'); + + $this->processHelper = $this->prophesize(ProcessHelper::class); + $this->schemaManager = $this->prophesize(AbstractSchemaManager::class); + + $this->regularConn = $this->prophesize(Connection::class); + $this->regularConn->getSchemaManager()->willReturn($this->schemaManager->reveal()); + $this->noDbNameConn = $this->prophesize(Connection::class); + $this->noDbNameConn->getSchemaManager()->willReturn($this->schemaManager->reveal()); + + $command = new CreateDatabaseCommand( + $locker->reveal(), + $this->processHelper->reveal(), + $phpExecutableFinder->reveal(), + $this->regularConn->reveal(), + $this->noDbNameConn->reveal() + ); + $app = new Application(); + $app->add($command); + + $this->commandTester = new CommandTester($command); + } + + /** @test */ + public function successMessageIsPrintedIfDatabaseAlreadyExists(): void + { + $shlinkDatabase = 'shlink_database'; + $getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); + $listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']); + $createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function () { + }); + $listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table']); + + $this->commandTester->execute([]); + $output = $this->commandTester->getDisplay(); + + $this->assertStringContainsString('Database already exists. Run "db:migrate" command', $output); + $getDatabase->shouldHaveBeenCalledOnce(); + $listDatabases->shouldHaveBeenCalledOnce(); + $createDatabase->shouldNotHaveBeenCalled(); + $listTables->shouldHaveBeenCalledOnce(); + } + + /** @test */ + public function databaseIsCreatedIfItDoesNotExist(): void + { + $shlinkDatabase = 'shlink_database'; + $getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); + $listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', 'bar']); + $createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function () { + }); + $listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table']); + + $this->commandTester->execute([]); + + $getDatabase->shouldHaveBeenCalledOnce(); + $listDatabases->shouldHaveBeenCalledOnce(); + $createDatabase->shouldHaveBeenCalledOnce(); + $listTables->shouldHaveBeenCalledOnce(); + } + + /** @test */ + public function tablesAreCreatedIfDatabaseIsEMpty(): void + { + $shlinkDatabase = 'shlink_database'; + $getDatabase = $this->regularConn->getDatabase()->willReturn($shlinkDatabase); + $listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']); + $createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function () { + }); + $listTables = $this->schemaManager->listTableNames()->willReturn([]); + $runCommand = $this->processHelper->run(Argument::type(OutputInterface::class), [ + '/usr/local/bin/php', + CreateDatabaseCommand::DOCTRINE_HELPER_SCRIPT, + CreateDatabaseCommand::DOCTRINE_HELPER_COMMAND, + ]); + + $this->commandTester->execute([]); + $output = $this->commandTester->getDisplay(); + + $this->assertStringContainsString('Creating database tables...', $output); + $this->assertStringContainsString('Database properly created!', $output); + $getDatabase->shouldHaveBeenCalledOnce(); + $listDatabases->shouldHaveBeenCalledOnce(); + $createDatabase->shouldNotHaveBeenCalled(); + $listTables->shouldHaveBeenCalledOnce(); + $runCommand->shouldHaveBeenCalledOnce(); + } +}