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->databasePlatform = $this->prophesize(AbstractPlatform::class); $this->regularConn = $this->prophesize(Connection::class); $this->regularConn->getSchemaManager()->willReturn($this->schemaManager->reveal()); $this->regularConn->getDatabasePlatform()->willReturn($this->databasePlatform->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->mustRun(Argument::type(OutputInterface::class), [ '/usr/local/bin/php', CreateDatabaseCommand::DOCTRINE_SCRIPT, CreateDatabaseCommand::DOCTRINE_CREATE_SCHEMA_COMMAND, ], Argument::cetera()); $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(); } /** @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(); } }