Skip to content

Commit

Permalink
Add Connection Registry
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Apr 5, 2020
1 parent 7811e4e commit f0a3f7f
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 7 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.3",
"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
62 changes: 56 additions & 6 deletions composer.lock

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

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

declare(strict_types=1);

namespace Doctrine\DBAL\Registry;

use Doctrine\DBAL\Connection;
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;
}
71 changes: 71 additions & 0 deletions lib/Doctrine/DBAL/Registry/Psr11ConnectionRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Registry;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Registry\ConnectionRegistry;
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;
}
}
83 changes: 83 additions & 0 deletions tests/Doctrine/Tests/DBAL/Registry/Psr11ConnectionRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\DBAL\Registry;

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

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

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

protected function setUp() : void
{
/** @var Connection&Stub $fooConnection */
$fooConnection = $this->createStub(Connection::class);
/** @var Connection&Stub $barConnection */
$barConnection = $this->createStub(Connection::class);

$this->connections = [
'foo' => $fooConnection,
'bar' => $barConnection,
];

/** @var ContainerInterface&Stub $container */
$container = $this->createStub(ContainerInterface::class);
$container->method('has')
->willReturnCallback(function (string $name) : bool {
return isset($this->connections[$name]);
});

$container->method('get')
->willReturnCallback(function (string $name) : Connection {
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 f0a3f7f

Please sign in to comment.