diff --git a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnection.php b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnection.php index e889a8d..e8999a7 100644 --- a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnection.php +++ b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnection.php @@ -2,76 +2,58 @@ namespace DAMA\DoctrineTestBundle\Doctrine\DBAL; -use Doctrine\DBAL\Driver\Connection; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware; -use Doctrine\DBAL\Platforms\AbstractPlatform; /** * Wraps a real connection and makes sure the initial nested transaction is using a savepoint. */ -class StaticConnection extends AbstractConnectionMiddleware -{ - private const SAVEPOINT_NAME = 'DAMA_TEST'; - - /** - * @var Connection - */ - private $connection; +if (method_exists(Connection::class, 'getEventManager')) { + // DBAL < 4 + class StaticConnection extends AbstractConnectionMiddleware + { + use StaticConnectionTrait; - /** - * @var AbstractPlatform - */ - private $platform; + public function beginTransaction(): bool + { + $this->doBeginTransaction(); - /** - * @var bool - */ - private $nested = false; + return true; + } - public function __construct(Connection $connection, AbstractPlatform $platform) - { - parent::__construct($connection); - $this->connection = $connection; - $this->platform = $platform; - } + public function commit(): bool + { + $this->doCommit(); - public function beginTransaction(): void - { - if ($this->nested) { - throw new \BadMethodCallException(sprintf('Bad call to "%s". A savepoint is already in use for a nested transaction.', __METHOD__)); + return true; } - $this->exec($this->platform->createSavePoint(self::SAVEPOINT_NAME)); + public function rollBack(): bool + { + $this->doRollBack(); - $this->nested = true; + return true; + } } - - public function commit(): void +} else { + // DBAL >= 4 + class StaticConnection extends AbstractConnectionMiddleware { - if (!$this->nested) { - throw new \BadMethodCallException(sprintf('Bad call to "%s". There is no savepoint for a nested transaction.', __METHOD__)); - } + use StaticConnectionTrait; - if ($this->platform->supportsReleaseSavepoints()) { - $this->exec($this->platform->releaseSavePoint(self::SAVEPOINT_NAME)); + public function beginTransaction(): void + { + $this->doBeginTransaction(); } - $this->nested = false; - } - - public function rollBack(): void - { - if (!$this->nested) { - throw new \BadMethodCallException(sprintf('Bad call to "%s". There is no savepoint for a nested transaction.', __METHOD__)); + public function commit(): void + { + $this->doCommit(); } - $this->exec($this->platform->rollbackSavePoint(self::SAVEPOINT_NAME)); - - $this->nested = false; - } - - public function getWrappedConnection(): Connection - { - return $this->connection; + public function rollBack(): void + { + $this->doRollBack(); + } } } diff --git a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionTrait.php b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionTrait.php new file mode 100644 index 0000000..a6fc1f2 --- /dev/null +++ b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnectionTrait.php @@ -0,0 +1,74 @@ +connection = $connection; + $this->platform = $platform; + } + + private function doBeginTransaction(): void + { + if ($this->nested) { + throw new \BadMethodCallException(sprintf('Bad call to "%s". A savepoint is already in use for a nested transaction.', __METHOD__)); + } + + $this->exec($this->platform->createSavePoint('DAMA_TEST')); + + $this->nested = true; + } + + private function doCommit(): void + { + if (!$this->nested) { + throw new \BadMethodCallException(sprintf('Bad call to "%s". There is no savepoint for a nested transaction.', __METHOD__)); + } + + if ($this->platform->supportsReleaseSavepoints()) { + $this->exec($this->platform->releaseSavePoint('DAMA_TEST')); + } + + $this->nested = false; + } + + private function doRollBack(): void + { + if (!$this->nested) { + throw new \BadMethodCallException(sprintf('Bad call to "%s". There is no savepoint for a nested transaction.', __METHOD__)); + } + + $this->exec($this->platform->rollbackSavePoint('DAMA_TEST')); + + $this->nested = false; + } + + public function getWrappedConnection(): Connection + { + return $this->connection; + } +} diff --git a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php index 8fe4ba4..7415f38 100644 --- a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php +++ b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php @@ -4,60 +4,30 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; -use Doctrine\DBAL\Driver\API\ExceptionConverter; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MySQL80Platform; -use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\ServerVersionProvider; -class MockDriver implements Driver -{ - private $connection; - private $schemaManager; - private $exceptionConverter; - - /** - * @param Driver\Connection $connection - * @param AbstractSchemaManager $schemaManager - * @param ExceptionConverter $exceptionConverter - */ - public function __construct( - $connection, - $schemaManager, - $exceptionConverter - ) { - $this->connection = $connection; - $this->schemaManager = $schemaManager; - $this->exceptionConverter = $exceptionConverter; - } - - public function connect(array $params): Driver\Connection - { - return clone $this->connection; - } - - public function getDatabasePlatform(ServerVersionProvider $versionProvider): AbstractPlatform - { - return new MySQL80Platform(); - } - - public function getSchemaManager(Connection $conn, AbstractPlatform $platform): AbstractSchemaManager +if (method_exists(Connection::class, 'getEventManager')) { + // DBAL < 4 + class MockDriver implements Driver { - return $this->schemaManager; - } + use MockDriverTrait; - public function getName(): string - { - return 'mock'; + public function getDatabasePlatform(): AbstractPlatform + { + return new MySQL80Platform(); + } } - - public function getDatabase(Connection $conn): string +} else { + // DBAL >= 4 + class MockDriver implements Driver { - return 'mock'; - } + use MockDriverTrait; - public function getExceptionConverter(): ExceptionConverter - { - return $this->exceptionConverter; + public function getDatabasePlatform(ServerVersionProvider $versionProvider): AbstractPlatform + { + return new MySQL80Platform(); + } } } diff --git a/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriverTrait.php b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriverTrait.php new file mode 100644 index 0000000..c1c03a8 --- /dev/null +++ b/tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriverTrait.php @@ -0,0 +1,56 @@ +connection = $connection; + $this->schemaManager = $schemaManager; + $this->exceptionConverter = $exceptionConverter; + } + + public function connect(array $params): Driver\Connection + { + return clone $this->connection; + } + + public function getSchemaManager(Connection $conn, AbstractPlatform $platform): AbstractSchemaManager + { + return $this->schemaManager; + } + + public function getName(): string + { + return 'mock'; + } + + public function getDatabase(Connection $conn): string + { + return 'mock'; + } + + public function getExceptionConverter(): ExceptionConverter + { + return $this->exceptionConverter; + } +}