-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
253 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
83
tests/Doctrine/Tests/DBAL/Registry/Psr11ConnectionRegistryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |