-
-
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
245 additions
and
8 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,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; | ||
} |
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,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; | ||
} | ||
} |
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,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()); | ||
} | ||
} |