Skip to content

Commit

Permalink
fix StaticDriver usage (#23)
Browse files Browse the repository at this point in the history
* fix StaticDriver usage

In some scenarios where the doctrine dbal connection was configured using 'url: ...' the StaticDriver was not used because Doctrine ignores the 'driverClass' param in that case :(

See https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/DriverManager.php#L403

* keep test suite compatible with phpunit 4.x
  • Loading branch information
dmaicher committed May 3, 2017
1 parent bf69afd commit 638865b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;

class StaticConnectionFactory extends ConnectionFactory
{
Expand All @@ -18,18 +19,19 @@ class StaticConnectionFactory extends ConnectionFactory
*/
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = array())
{
if (isset($params['driverClass'])) {
$underlyingDriverClass = $params['driverClass'];
} else {
//there seems to be no other way to access the originally configured Driver class :(
$connectionOriginalDriver = parent::createConnection($params, $config, $eventManager, $mappingTypes);
$underlyingDriverClass = get_class($connectionOriginalDriver->getDriver());
}
// create the original connection to get the used wrapper class + driver
$connectionOriginalDriver = parent::createConnection($params, $config, $eventManager, $mappingTypes);

StaticDriver::setUnderlyingDriverClass($underlyingDriverClass);
// wrapper class can be overridden/customized in params (see Doctrine\DBAL\DriverManager)
$connectionWrapperClass = get_class($connectionOriginalDriver);

$params['driverClass'] = StaticDriver::class;
$connection = parent::createConnection($params, $config, $eventManager, $mappingTypes);
/** @var Connection $connection */
$connection = new $connectionWrapperClass(
$connectionOriginalDriver->getParams(),
new StaticDriver($connectionOriginalDriver->getDriver()),
$connectionOriginalDriver->getConfiguration(),
$connectionOriginalDriver->getEventManager()
);

if (StaticDriver::isKeepStaticConnections()) {
// The underlying connection already has a transaction started.
Expand Down
21 changes: 2 additions & 19 deletions src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,14 @@ class StaticDriver implements Driver, ExceptionConverterDriver, VersionAwarePlat
*/
private static $keepStaticConnections = false;

/**
* @var string
*/
private static $underlyingDriverClass;

/**
* @var Driver
*/
private $underlyingDriver;

public function __construct()
public function __construct(Driver $underlyingDriver)
{
if (self::$underlyingDriverClass === null) {
throw new \Exception('Cannot instantiate without setting underlying Driver class');
}

$this->underlyingDriver = new self::$underlyingDriverClass();
$this->underlyingDriver = $underlyingDriver;
}

/**
Expand Down Expand Up @@ -130,14 +121,6 @@ public static function isKeepStaticConnections()
return self::$keepStaticConnections;
}

/**
* @param string $underlyingDriverClass
*/
public static function setUnderlyingDriverClass($underlyingDriverClass)
{
self::$underlyingDriverClass = $underlyingDriverClass;
}

public static function beginTransaction()
{
foreach (self::$connections as $con) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ class StaticDriverTest extends \PHPUnit_Framework_TestCase
{
public function testConnect()
{
$driver = new StaticDriver();
$driver = new StaticDriver(new MockDriver());

$driver::setKeepStaticConnections(true);
$driver::setUnderlyingDriverClass(MockDriver::class);

$connection1 = $driver->connect(['database_name' => 1], 'user1', 'pw1');
$connection2 = $driver->connect(['database_name' => 2], 'user1', 'pw2');

$this->assertInstanceOf(StaticConnection::class, $connection1);
$this->assertNotSame($connection1->getWrappedConnection(), $connection2->getWrappedConnection());

$driver = new StaticDriver();
$driver = new StaticDriver(new MockDriver());

$connectionNew1 = $driver->connect(['database_name' => 1], 'user1', 'pw1');
$connectionNew2 = $driver->connect(['database_name' => 2], 'user1', 'pw2');
Expand Down

0 comments on commit 638865b

Please sign in to comment.