shlink/module/CLI/test/Command/Db/CreateDatabaseCommandTest.php

165 lines
6.8 KiB
PHP
Raw Normal View History

2019-08-06 19:22:49 +03:00
<?php
2019-10-05 18:26:10 +03:00
2019-08-06 19:22:49 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\Db;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
2022-03-05 12:32:05 +03:00
use Doctrine\DBAL\Platforms\SqlitePlatform;
2019-08-06 19:22:49 +03:00
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\Db\CreateDatabaseCommand;
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
2019-08-06 19:22:49 +03:00
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Lock\LockFactory;
2019-08-06 19:22:49 +03:00
use Symfony\Component\Lock\LockInterface;
use Symfony\Component\Process\PhpExecutableFinder;
2022-03-05 12:48:02 +03:00
use const Shlinkio\Shlink\MIGRATIONS_TABLE;
2019-08-06 19:22:49 +03:00
class CreateDatabaseCommandTest extends TestCase
{
use CliTestUtilsTrait;
2020-11-02 13:50:19 +03:00
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
private ObjectProphecy $processHelper;
private ObjectProphecy $regularConn;
private ObjectProphecy $schemaManager;
private ObjectProphecy $driver;
2019-08-06 19:22:49 +03:00
public function setUp(): void
{
$locker = $this->prophesize(LockFactory::class);
2019-08-06 19:22:49 +03:00
$lock = $this->prophesize(LockInterface::class);
$lock->acquire(Argument::any())->willReturn(true);
2020-01-01 22:48:31 +03:00
$lock->release()->will(function (): void {
2019-08-06 19:22:49 +03:00
});
$locker->createLock(Argument::cetera())->willReturn($lock->reveal());
$phpExecutableFinder = $this->prophesize(PhpExecutableFinder::class);
$phpExecutableFinder->find(false)->willReturn('/usr/local/bin/php');
$this->processHelper = $this->prophesize(ProcessRunnerInterface::class);
2019-08-06 19:22:49 +03:00
$this->schemaManager = $this->prophesize(AbstractSchemaManager::class);
$this->regularConn = $this->prophesize(Connection::class);
2021-10-23 16:44:56 +03:00
$this->regularConn->createSchemaManager()->willReturn($this->schemaManager->reveal());
$this->driver = $this->prophesize(Driver::class);
$this->regularConn->getDriver()->willReturn($this->driver->reveal());
$this->driver->getDatabasePlatform()->willReturn($this->prophesize(AbstractPlatform::class)->reveal());
2020-11-02 13:50:19 +03:00
$noDbNameConn = $this->prophesize(Connection::class);
2021-10-23 16:44:56 +03:00
$noDbNameConn->createSchemaManager()->willReturn($this->schemaManager->reveal());
2019-08-06 19:22:49 +03:00
$command = new CreateDatabaseCommand(
$locker->reveal(),
$this->processHelper->reveal(),
$phpExecutableFinder->reveal(),
$this->regularConn->reveal(),
2020-11-02 13:50:19 +03:00
$noDbNameConn->reveal(),
2019-08-06 19:22:49 +03:00
);
$this->commandTester = $this->testerForCommand($command);
2019-08-06 19:22:49 +03:00
}
/** @test */
public function successMessageIsPrintedIfDatabaseAlreadyExists(): void
{
$shlinkDatabase = 'shlink_database';
$getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]);
2019-08-06 19:22:49 +03:00
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']);
2020-01-01 22:48:31 +03:00
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
2019-08-06 19:22:49 +03:00
});
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table']);
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString('Database already exists. Run "db:migrate" command', $output);
2019-08-06 19:22:49 +03:00
$getDatabase->shouldHaveBeenCalledOnce();
$listDatabases->shouldHaveBeenCalledOnce();
$createDatabase->shouldNotHaveBeenCalled();
$listTables->shouldHaveBeenCalledOnce();
}
/** @test */
public function databaseIsCreatedIfItDoesNotExist(): void
{
$shlinkDatabase = 'shlink_database';
$getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]);
2019-08-06 19:22:49 +03:00
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', 'bar']);
2020-01-01 22:48:31 +03:00
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
2019-08-06 19:22:49 +03:00
});
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table', MIGRATIONS_TABLE]);
2019-08-06 19:22:49 +03:00
$this->commandTester->execute([]);
$getDatabase->shouldHaveBeenCalledOnce();
$listDatabases->shouldHaveBeenCalledOnce();
$createDatabase->shouldHaveBeenCalledOnce();
$listTables->shouldHaveBeenCalledOnce();
}
/**
* @test
* @dataProvider provideEmptyDatabase
*/
public function tablesAreCreatedIfDatabaseIsEmpty(array $tables): void
2019-08-06 19:22:49 +03:00
{
$shlinkDatabase = 'shlink_database';
$getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]);
2019-08-06 19:22:49 +03:00
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', $shlinkDatabase, 'bar']);
2020-01-01 22:48:31 +03:00
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
2019-08-06 19:22:49 +03:00
});
$listTables = $this->schemaManager->listTableNames()->willReturn($tables);
$runCommand = $this->processHelper->run(Argument::type(OutputInterface::class), [
2019-08-06 19:22:49 +03:00
'/usr/local/bin/php',
CreateDatabaseCommand::DOCTRINE_SCRIPT,
CreateDatabaseCommand::DOCTRINE_CREATE_SCHEMA_COMMAND,
'--no-interaction',
]);
2019-08-06 19:22:49 +03:00
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString('Creating database tables...', $output);
self::assertStringContainsString('Database properly created!', $output);
2019-08-06 19:22:49 +03:00
$getDatabase->shouldHaveBeenCalledOnce();
$listDatabases->shouldHaveBeenCalledOnce();
$createDatabase->shouldNotHaveBeenCalled();
$listTables->shouldHaveBeenCalledOnce();
$runCommand->shouldHaveBeenCalledOnce();
}
public function provideEmptyDatabase(): iterable
{
yield 'no tables' => [[]];
yield 'migrations table' => [[MIGRATIONS_TABLE]];
}
/** @test */
public function databaseCheckIsSkippedForSqlite(): void
{
$this->driver->getDatabasePlatform()->willReturn($this->prophesize(SqlitePlatform::class)->reveal());
$shlinkDatabase = 'shlink_database';
$getDatabase = $this->regularConn->getParams()->willReturn(['dbname' => $shlinkDatabase]);
$listDatabases = $this->schemaManager->listDatabases()->willReturn(['foo', 'bar']);
2020-01-01 22:48:31 +03:00
$createDatabase = $this->schemaManager->createDatabase($shlinkDatabase)->will(function (): void {
});
$listTables = $this->schemaManager->listTableNames()->willReturn(['foo_table', 'bar_table']);
$this->commandTester->execute([]);
$getDatabase->shouldNotHaveBeenCalled();
$listDatabases->shouldNotHaveBeenCalled();
$createDatabase->shouldNotHaveBeenCalled();
$listTables->shouldHaveBeenCalledOnce();
}
2019-08-06 19:22:49 +03:00
}