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

130 lines
5.9 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;
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;
use Exception;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
2019-08-06 19:22:49 +03:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\Db\CreateDatabaseCommand;
use Shlinkio\Shlink\CLI\Util\ProcessRunnerInterface;
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;
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
protected function setUp(): void
2019-08-06 19:22:49 +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);
$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);
$this->commandTester = CliTestUtils::testerForCommand($command);
2019-08-06 19:22:49 +03:00
}
#[Test]
2019-08-06 19:22:49 +03:00
public function successMessageIsPrintedIfDatabaseAlreadyExists(): void
{
$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]);
$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
}
#[Test]
2019-08-06 19:22:49 +03:00
public function databaseIsCreatedIfItDoesNotExist(): void
{
$this->driver->method('getDatabasePlatform')->willReturn($this->createMock(AbstractPlatform::class));
2019-08-06 19:22:49 +03:00
$shlinkDatabase = 'shlink_database';
$this->regularConn->expects($this->once())->method('getParams')->willReturn(['dbname' => $shlinkDatabase]);
2023-02-15 20:55:25 +03:00
$this->metadataFactory->method('getAllMetadata')->willReturn([]);
$this->schemaManager->expects($this->once())->method('createDatabase')->with($shlinkDatabase);
$this->schemaManager->expects($this->once())->method('listTableNames')->willThrowException(new Exception(''));
2019-08-06 19:22:49 +03:00
$this->commandTester->execute([]);
}
#[Test, DataProvider('provideEmptyDatabase')]
public function tablesAreCreatedIfDatabaseIsEmpty(array $tables): void
2019-08-06 19:22:49 +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]);
$this->schemaManager->expects($this->never())->method('createDatabase');
$this->schemaManager->expects($this->once())->method('listTableNames')->willReturn($tables);
$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
}
2023-02-09 11:32:38 +03:00
public static function provideEmptyDatabase(): iterable
{
yield 'no tables' => [[]];
2023-02-15 20:55:25 +03:00
yield 'migrations table' => [['non_shlink_table']];
}
2019-08-06 19:22:49 +03:00
}