Skip to content

Commit

Permalink
beginTransaction fix + VersionAwarePlatformDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Mar 22, 2016
1 parent 1758a8f commit 5b5687e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
55 changes: 38 additions & 17 deletions src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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')]),
]
)
;
Expand Down
2 changes: 1 addition & 1 deletion tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 5b5687e

Please sign in to comment.