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;
|
2019-08-06 21:16:16 +03:00
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
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 Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Helper\ProcessHelper;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
use Symfony\Component\Lock\Factory as Locker;
|
|
|
|
use Symfony\Component\Lock\LockInterface;
|
|
|
|
use Symfony\Component\Process\PhpExecutableFinder;
|
|
|
|
|
|
|
|
class CreateDatabaseCommandTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var CommandTester */
|
|
|
|
private $commandTester;
|
|
|
|
/** @var ObjectProphecy */
|
|
|
|
private $processHelper;
|
|
|
|
/** @var ObjectProphecy */
|
|
|
|
private $regularConn;
|
|
|
|
/** @var ObjectProphecy */
|
|
|
|
private $noDbNameConn;
|
|
|
|
/** @var ObjectProphecy */
|
|
|
|
private $schemaManager;
|
2019-08-06 21:16:16 +03:00
|
|
|
/** @var ObjectProphecy */
|
|
|
|
private $databasePlatform;
|
2019-08-06 19:22:49 +03:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$locker = $this->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);
|
2019-08-06 21:16:16 +03:00
|
|
|
$this->databasePlatform = $this->prophesize(AbstractPlatform::class);
|
2019-08-06 19:22:49 +03:00
|
|
|
|
|
|
|
$this->regularConn = $this->prophesize(Connection::class);
|
|
|
|
$this->regularConn->getSchemaManager()->willReturn($this->schemaManager->reveal());
|
2019-08-06 21:16:16 +03:00
|
|
|
$this->regularConn->getDatabasePlatform()->willReturn($this->databasePlatform->reveal());
|
2019-08-06 19:22:49 +03:00
|
|
|
$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 */
|
2019-08-06 21:48:48 +03:00
|
|
|
public function tablesAreCreatedIfDatabaseIsEmpty(): void
|
2019-08-06 19:22:49 +03:00
|
|
|
{
|
|
|
|
$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([]);
|
2019-11-17 11:52:45 +03:00
|
|
|
$runCommand = $this->processHelper->mustRun(Argument::type(OutputInterface::class), [
|
2019-08-06 19:22:49 +03:00
|
|
|
'/usr/local/bin/php',
|
2019-11-17 11:52:45 +03:00
|
|
|
CreateDatabaseCommand::DOCTRINE_SCRIPT,
|
|
|
|
CreateDatabaseCommand::DOCTRINE_CREATE_SCHEMA_COMMAND,
|
2019-08-06 21:48:48 +03:00
|
|
|
], Argument::cetera());
|
2019-08-06 19:22:49 +03:00
|
|
|
|
|
|
|
$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();
|
|
|
|
}
|
2019-08-06 21:16:16 +03:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function databaseCheckIsSkippedForSqlite(): void
|
|
|
|
{
|
|
|
|
$this->databasePlatform->getName()->willReturn('sqlite');
|
|
|
|
|
|
|
|
$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->shouldNotHaveBeenCalled();
|
|
|
|
$listDatabases->shouldNotHaveBeenCalled();
|
|
|
|
$createDatabase->shouldNotHaveBeenCalled();
|
|
|
|
$listTables->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2019-08-06 19:22:49 +03:00
|
|
|
}
|