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;
|
2022-04-10 20:48:32 +03:00
|
|
|
use Doctrine\DBAL\Driver;
|
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;
|
2023-02-15 20:55:25 +03:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
|
2023-06-01 20:27:04 +03:00
|
|
|
use Exception;
|
2023-02-09 22:42:18 +03:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-22 14:05:36 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-08-06 19:22:49 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\CLI\Command\Db\CreateDatabaseCommand;
|
2021-02-13 00:59:40 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
|
2023-06-18 11:51:59 +03:00
|
|
|
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
|
2019-08-06 19:22:49 +03:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2019-11-30 19:59:04 +03:00
|
|
|
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
|
|
|
|
2019-08-06 19:22:49 +03:00
|
|
|
class CreateDatabaseCommandTest extends TestCase
|
|
|
|
{
|
2019-12-30 00:27:00 +03:00
|
|
|
private CommandTester $commandTester;
|
2022-10-24 20:53:13 +03:00
|
|
|
private MockObject & ProcessRunnerInterface $processHelper;
|
|
|
|
private MockObject & Connection $regularConn;
|
2023-02-15 20:55:25 +03:00
|
|
|
private MockObject & ClassMetadataFactory $metadataFactory;
|
2022-10-24 20:53:13 +03:00
|
|
|
private MockObject & AbstractSchemaManager $schemaManager;
|
|
|
|
private MockObject & Driver $driver;
|
2019-08-06 19:22:49 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2019-08-06 19:22:49 +03:00
|
|
|
{
|
2022-10-22 14:05:36 +03:00
|
|
|
$locker = $this->createMock(LockFactory::class);
|
|
|
|
$lock = $this->createMock(LockInterface::class);
|
|
|
|
$lock->method('acquire')->withAnyParameters()->willReturn(true);
|
|
|
|
$locker->method('createLock')->withAnyParameters()->willReturn($lock);
|
|
|
|
|
|
|
|
$phpExecutableFinder = $this->createMock(PhpExecutableFinder::class);
|
|
|
|
$phpExecutableFinder->method('find')->with($this->isFalse())->willReturn('/usr/local/bin/php');
|
|
|
|
|
|
|
|
$this->processHelper = $this->createMock(ProcessRunnerInterface::class);
|
|
|
|
$this->schemaManager = $this->createMock(AbstractSchemaManager::class);
|
|
|
|
|
|
|
|
$this->regularConn = $this->createMock(Connection::class);
|
|
|
|
$this->regularConn->method('createSchemaManager')->willReturn($this->schemaManager);
|
|
|
|
$this->driver = $this->createMock(Driver::class);
|
|
|
|
$this->regularConn->method('getDriver')->willReturn($this->driver);
|
2023-02-15 20:55:25 +03:00
|
|
|
|
|
|
|
$this->metadataFactory = $this->createMock(ClassMetadataFactory::class);
|
|
|
|
$em = $this->createMock(EntityManagerInterface::class);
|
|
|
|
$em->method('getConnection')->willReturn($this->regularConn);
|
|
|
|
$em->method('getMetadataFactory')->willReturn($this->metadataFactory);
|
|
|
|
|
2022-10-22 14:05:36 +03:00
|
|
|
$noDbNameConn = $this->createMock(Connection::class);
|
|
|
|
$noDbNameConn->method('createSchemaManager')->withAnyParameters()->willReturn($this->schemaManager);
|
2019-08-06 19:22:49 +03:00
|
|
|
|
2023-02-15 20:55:25 +03:00
|
|
|
$command = new CreateDatabaseCommand($locker, $this->processHelper, $phpExecutableFinder, $em, $noDbNameConn);
|
2023-06-18 11:51:59 +03:00
|
|
|
$this->commandTester = CliTestUtils::testerForCommand($command);
|
2019-08-06 19:22:49 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-08-06 19:22:49 +03:00
|
|
|
public function successMessageIsPrintedIfDatabaseAlreadyExists(): void
|
|
|
|
{
|
2023-06-01 20:27:04 +03:00
|
|
|
$this->regularConn->expects($this->never())->method('getParams');
|
|
|
|
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
|
|
|
|
|
2023-02-15 20:55:25 +03:00
|
|
|
$metadataMock = $this->createMock(ClassMetadata::class);
|
|
|
|
$metadataMock->expects($this->once())->method('getTableName')->willReturn('foo_table');
|
|
|
|
$this->metadataFactory->method('getAllMetadata')->willReturn([$metadataMock]);
|
2022-10-22 14:05:36 +03:00
|
|
|
$this->schemaManager->expects($this->never())->method('createDatabase');
|
|
|
|
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn(['foo_table', 'bar_table']);
|
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('Database already exists. Run "db:migrate" command', $output);
|
2019-08-06 19:22:49 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-08-06 19:22:49 +03:00
|
|
|
public function databaseIsCreatedIfItDoesNotExist(): void
|
|
|
|
{
|
2023-06-01 20:27:04 +03:00
|
|
|
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
|
|
|
|
|
2019-08-06 19:22:49 +03:00
|
|
|
$shlinkDatabase = 'shlink_database';
|
2022-10-22 14:05:36 +03:00
|
|
|
$this->regularConn->expects($this->once())->method('getParams')->willReturn(['dbname' => $shlinkDatabase]);
|
2023-02-15 20:55:25 +03:00
|
|
|
$this->metadataFactory->method('getAllMetadata')->willReturn([]);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->schemaManager->expects($this->once())->method('createDatabase')->with($shlinkDatabase);
|
2023-06-01 20:27:04 +03:00
|
|
|
$this->schemaManager->expects($this->once())->method('listTableNames')->willThrowException(new Exception(''));
|
2019-08-06 19:22:49 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([]);
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test, DataProvider('provideEmptyDatabase')]
|
2022-03-05 12:41:13 +03:00
|
|
|
public function tablesAreCreatedIfDatabaseIsEmpty(array $tables): void
|
2019-08-06 19:22:49 +03:00
|
|
|
{
|
2023-06-01 20:27:04 +03:00
|
|
|
$this->regularConn->expects($this->never())->method('getParams');
|
|
|
|
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
|
|
|
|
|
2023-02-15 20:55:25 +03:00
|
|
|
$metadata = $this->createMock(ClassMetadata::class);
|
|
|
|
$metadata->method('getTableName')->willReturn('shlink_table');
|
|
|
|
$this->metadataFactory->method('getAllMetadata')->willReturn([$metadata]);
|
2022-10-22 14:05:36 +03:00
|
|
|
$this->schemaManager->expects($this->never())->method('createDatabase');
|
|
|
|
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn($tables);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->processHelper->expects($this->once())->method('run')->with($this->isInstanceOf(OutputInterface::class), [
|
|
|
|
'/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
|
|
|
}
|
2019-08-06 21:16:16 +03:00
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideEmptyDatabase(): iterable
|
2022-03-05 12:41:13 +03:00
|
|
|
{
|
|
|
|
yield 'no tables' => [[]];
|
2023-02-15 20:55:25 +03:00
|
|
|
yield 'migrations table' => [['non_shlink_table']];
|
2022-03-05 12:41:13 +03:00
|
|
|
}
|
2019-08-06 19:22:49 +03:00
|
|
|
}
|