From 5b5687e8623503fa6caa6bc9a2c6b3c82a5d5535 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Tue, 22 Mar 2016 20:01:05 +0100 Subject: [PATCH] beginTransaction fix + VersionAwarePlatformDriver --- .../Doctrine/DBAL/StaticConnectionFactory.php | 8 ++- .../Doctrine/DBAL/StaticDriver.php | 55 +++++++++++++------ .../DoctrineTestCompilerPassTest.php | 12 ++-- .../Doctrine/DBAL/MockDriver.php | 2 +- .../DBAL/StaticConnectionFactoryTest.php | 20 ++++++- .../Doctrine/DBAL/StaticDriverTest.php | 2 + 6 files changed, 70 insertions(+), 29 deletions(-) diff --git a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactory.php b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactory.php index 108505e..e9b7a59 100644 --- a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactory.php +++ b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactory.php @@ -31,9 +31,11 @@ public function createConnection(array $params, Configuration $config = null, Ev $params['driverClass'] = StaticDriver::class; $connection = parent::createConnection($params, $config, $eventManager, $mappingTypes); - // The underlying connection already has a transaction started. - // So we start it as well on this connection so the internal state ($_transactionNestingLevel) is in sync with the underlying connection. - $connection->beginTransaction(); + if (StaticDriver::isKeepStaticConnections()) { + // The underlying connection already has a transaction started. + // So we start it as well on this connection so the internal state ($_transactionNestingLevel) is in sync with the underlying connection. + $connection->beginTransaction(); + } return $connection; } diff --git a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php index 64ce9b0..874b2a1 100644 --- a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php +++ b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php @@ -6,8 +6,9 @@ use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\Driver\DriverException; use Doctrine\DBAL\Driver\ExceptionConverterDriver; +use Doctrine\DBAL\VersionAwarePlatformDriver; -class StaticDriver implements Driver, ExceptionConverterDriver +class StaticDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver { /** * @var Connection[] @@ -101,6 +102,42 @@ public function convertException($message, DriverException $exception) return $exception; } + /** + * {@inheritdoc} + */ + public function createDatabasePlatformForVersion($version) + { + if ($this->underlyingDriver instanceof VersionAwarePlatformDriver) { + return $this->underlyingDriver->createDatabasePlatformForVersion($version); + } + + return $this->getDatabasePlatform(); + } + + /** + * @param $keepStaticConnections bool + */ + public static function setKeepStaticConnections($keepStaticConnections) + { + self::$keepStaticConnections = $keepStaticConnections; + } + + /** + * @return bool + */ + 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) { @@ -125,20 +162,4 @@ public static function commit() $con->commit(); } } - - /** - * @param $keepStaticConnections bool - */ - public static function setKeepStaticConnections($keepStaticConnections) - { - self::$keepStaticConnections = $keepStaticConnections; - } - - /** - * @param string $underlyingDriverClass - */ - public static function setUnderlyingDriverClass($underlyingDriverClass) - { - self::$underlyingDriverClass = $underlyingDriverClass; - } } diff --git a/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php b/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php index 394b0b5..b653db7 100644 --- a/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php +++ b/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php @@ -91,27 +91,27 @@ public function testProcess(array $processedConfig) ->withConsecutive( [ 'doctrine.orm.a_metadata_cache', - (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.a_metadata_cache')]) + (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.a_metadata_cache')]), ], [ 'doctrine.orm.b_metadata_cache', - (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.b_metadata_cache')]) + (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.b_metadata_cache')]), ], [ 'doctrine.orm.c_metadata_cache', - (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.c_metadata_cache')]) + (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.c_metadata_cache')]), ], [ 'doctrine.orm.a_query_cache', - (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.a_query_cache')]) + (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.a_query_cache')]), ], [ 'doctrine.orm.b_query_cache', - (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.b_query_cache')]) + (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.b_query_cache')]), ], [ 'doctrine.orm.c_query_cache', - (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.c_query_cache')]) + (new Definition(StaticArrayCache::class))->addMethodCall('setNamespace', [sha1('doctrine.orm.c_query_cache')]), ] ) ; diff --git a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php index bca6acb..061fb03 100644 --- a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php +++ b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php @@ -30,7 +30,7 @@ private function getMock($class) */ public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) { - return $this->getMock(Connection::class); + return $this->getMock(Driver\Connection::class); } /** diff --git a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactoryTest.php b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactoryTest.php index 7a0fdd6..2ce7c69 100644 --- a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactoryTest.php +++ b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionFactoryTest.php @@ -7,15 +7,31 @@ class StaticConnectionFactoryTest extends \PHPUnit_Framework_TestCase { - public function testCreateConnection() + /** + * @dataProvider createConnectionDataProvider + * + * @param bool $keepStaticConnections + * @param int $expectedNestingLevel + */ + public function testCreateConnection($keepStaticConnections, $expectedNestingLevel) { $factory = new StaticConnectionFactory([]); + StaticDriver::setKeepStaticConnections($keepStaticConnections); + $connection = $factory->createConnection([ 'driverClass' => MockDriver::class, ]); $this->assertInstanceOf(StaticDriver::class, $connection->getDriver()); - $this->assertSame(1, $connection->getTransactionNestingLevel()); + $this->assertSame($expectedNestingLevel, $connection->getTransactionNestingLevel()); + } + + public function createConnectionDataProvider() + { + return [ + [false, 0], + [true, 1], + ]; } } diff --git a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriverTest.php b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriverTest.php index 2376393..87fd577 100644 --- a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriverTest.php +++ b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriverTest.php @@ -2,6 +2,7 @@ namespace Tests\DAMA\DoctrineTestBundle\Doctrine\DBAL; +use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticConnection; use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver; class StaticDriverTest extends \PHPUnit_Framework_TestCase @@ -16,6 +17,7 @@ public function testConnect() $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();