Created NoDbNameConnectionFactoryTest

This commit is contained in:
Alejandro Celaya 2019-08-06 17:30:28 +02:00
parent a575f2eced
commit e79c41d753
2 changed files with 59 additions and 1 deletions

View file

@ -68,9 +68,11 @@ class CreateDatabaseCommand extends AbstractDatabaseCommand
private function checkDbExists(): void
{
$shlinkDatabase = $this->conn->getDatabase();
// In order to create the new database, we have to use a connection where the dbname was not set.
// Otherwise, it will fail to connect and will not be able to create the new database
$schemaManager = $this->noDbNameConn->getSchemaManager();
$databases = $schemaManager->listDatabases();
$shlinkDatabase = $this->conn->getDatabase();
if (! contains($databases, $shlinkDatabase)) {
$schemaManager->createDatabase($shlinkDatabase);

View file

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Doctrine;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\Common\Doctrine\NoDbNameConnectionFactory;
class NoDbNameConnectionFactoryTest extends TestCase
{
/** @var NoDbNameConnectionFactory */
private $factory;
/** @var ObjectProphecy */
private $container;
/** @var ObjectProphecy */
private $originalConn;
public function setUp(): void
{
$this->container = $this->prophesize(ContainerInterface::class);
$this->originalConn = $this->prophesize(Connection::class);
$this->container->get(Connection::class)->willReturn($this->originalConn->reveal());
$this->factory = new NoDbNameConnectionFactory();
}
/** @test */
public function createsNewConnectionRemovingDbNameFromOriginalConnectionParams(): void
{
$params = [
'username' => 'foo',
'password' => 'bar',
'dbname' => 'something',
];
$getOriginalParams = $this->originalConn->getParams()->willReturn($params);
$getOriginalDriver = $this->originalConn->getDriver()->willReturn($this->prophesize(Driver::class)->reveal());
$getOriginalConfig = $this->originalConn->getConfiguration()->willReturn(null);
$getOriginalEvents = $this->originalConn->getEventManager()->willReturn(null);
$conn = ($this->factory)($this->container->reveal());
$this->assertEquals([
'username' => 'foo',
'password' => 'bar',
], $conn->getParams());
$getOriginalParams->shouldHaveBeenCalledOnce();
$getOriginalDriver->shouldHaveBeenCalledOnce();
$getOriginalConfig->shouldHaveBeenCalledOnce();
$getOriginalEvents->shouldHaveBeenCalledOnce();
$this->container->get(Connection::class)->shouldHaveBeenCalledOnce();
}
}