diff --git a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactory.php b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactory.php index e9b7a59..6c95e24 100644 --- a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactory.php +++ b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactory.php @@ -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 { @@ -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. diff --git a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php index 874b2a1..d09ba12 100644 --- a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php +++ b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php @@ -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; } /** @@ -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) { diff --git a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriverTest.php b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriverTest.php index 87fd577..ed6df35 100644 --- a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriverTest.php +++ b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriverTest.php @@ -9,10 +9,9 @@ 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'); @@ -20,7 +19,7 @@ public function testConnect() $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');