Skip to content

Commit

Permalink
Add DBAL Connection Registry
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Mar 8, 2020
1 parent 5ffc845 commit d866f1d
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 8 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"php": "^7.2",
"doctrine/cache": "^1.0",
"doctrine/event-manager": "^1.0",
"ocramius/package-versions": "^1.4"
"ocramius/package-versions": "^1.4",
"psr/container": "^1.0"
},
"require-dev": {
"doctrine/coding-standard": "^7.0",
Expand Down
63 changes: 56 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions lib/Doctrine/DBAL/ConnectionRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL;

use InvalidArgumentException;

interface ConnectionRegistry
{
/**
* Gets the default connection name.
*
* @return string The default connection name.
*/
public function getDefaultConnectionName() : string;

/**
* Gets the named connection.
*
* @param string $name The connection name (null for the default one).
*
* @throws InvalidArgumentException in case the connection for the given name does not exist.
*/
public function getConnection(?string $name = null) : Connection;

/**
* Gets an array of all registered connections.
*
* @return array<string, Connection> An array of Connection instances.
*/
public function getConnections() : array;

/**
* Gets all connection names.
*
* @return array<string> An array of connection names.
*/
public function getConnectionNames() : array;
}
69 changes: 69 additions & 0 deletions lib/Doctrine/DBAL/Psr11ConnectionRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL;

use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use function sprintf;

class Psr11ConnectionRegistry implements ConnectionRegistry
{
/** @var ContainerInterface */
private $container;

/** @var string */
private $defaultConnectionName;

/** @var string[] */
private $connectionNames;

/**
* @param string[] $connectionNames
*/
public function __construct(ContainerInterface $container, string $defaultConnectionName, array $connectionNames)
{
$this->container = $container;
$this->defaultConnectionName = $defaultConnectionName;
$this->connectionNames = $connectionNames;
}

public function getDefaultConnectionName() : string
{
return $this->defaultConnectionName;
}

public function getConnection(?string $name = null) : Connection
{
$name = $name ?? $this->defaultConnectionName;

if (! $this->container->has($name)) {
throw new InvalidArgumentException(sprintf('Connection with name "%s" does not exist.', $name));
}

return $this->container->get($name);
}

/**
* @inheritDoc
*/
public function getConnections() : array
{
$connections = [];

foreach ($this->connectionNames as $connectionName) {
$connections[$connectionName] = $this->container->get($connectionName);
}

return $connections;
}

/**
* @inheritDoc
*/
public function getConnectionNames() : array
{
return $this->connectionNames;
}
}
78 changes: 78 additions & 0 deletions tests/Doctrine/Tests/DBAL/Psr11ConnectionRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\DBAL;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionRegistry;
use Doctrine\DBAL\Psr11ConnectionRegistry;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use function array_keys;

class Psr11ConnectionRegistryTest extends TestCase
{
/** @var ConnectionRegistry */
private $registry;

/** @var array<string, Connection> */
private $connections;

protected function setUp() : void
{
$this->connections = [
'foo' => $this->createMock(Connection::class),
'bar' => $this->createMock(Connection::class),
];

$container = $this->createMock(ContainerInterface::class);
$container->expects($this->any())
->method('has')
->willReturnCallback(function (string $name) {
return isset($this->connections[$name]);
});

$container->expects($this->any())
->method('get')
->willReturnCallback(function (string $name) {
return $this->connections[$name];
});

$this->registry = new Psr11ConnectionRegistry($container, 'bar', array_keys($this->connections));
}

public function testGetDefaultConnection() : void
{
$this->assertSame($this->connections['bar'], $this->registry->getConnection());
}

public function testGetConnectionByName() : void
{
$this->assertSame($this->connections['foo'], $this->registry->getConnection('foo'));
$this->assertSame($this->connections['bar'], $this->registry->getConnection('bar'));
}

public function testGetNotExistentConnection() : void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Connection with name "something" does not exist.');
$this->registry->getConnection('something');
}

public function testGetDefaultConnectionName() : void
{
$this->assertSame('bar', $this->registry->getDefaultConnectionName());
}

public function getGetConnections() : void
{
$this->assertSame($this->connections, $this->registry->getConnections());
}

public function testGetConnectionNames() : void
{
$this->assertSame(array_keys($this->connections), $this->registry->getConnectionNames());
}
}

0 comments on commit d866f1d

Please sign in to comment.