Skip to content

Commit

Permalink
add BC layer for changed return types
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Oct 30, 2023
1 parent 001c4bd commit 575fa19
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 98 deletions.
86 changes: 34 additions & 52 deletions src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace DAMA\DoctrineTestBundle\Doctrine\DBAL;

use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
* @internal
*/
trait StaticConnectionTrait
{
/**
* @var Connection
*/
private $connection;

/**
* @var AbstractPlatform
*/
private $platform;

/**
* @var bool
*/
private $nested = false;

public function __construct(Connection $connection, AbstractPlatform $platform)
{
parent::__construct($connection);
$this->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;
}
}
62 changes: 16 additions & 46 deletions tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 28 in tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php

View workflow job for this annotation

GitHub Actions / php-stan

Parameter #1 $versionProvider of method Tests\DAMA\DoctrineTestBundle\Doctrine\DBAL\MockDriver::getDatabasePlatform() is not optional.

Check failure on line 28 in tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriver.php

View workflow job for this annotation

GitHub Actions / php-stan

Parameter $versionProvider of method Tests\DAMA\DoctrineTestBundle\Doctrine\DBAL\MockDriver::getDatabasePlatform() has invalid type Doctrine\DBAL\ServerVersionProvider.
{
return new MySQL80Platform();
}
}
}
56 changes: 56 additions & 0 deletions tests/DAMA/DoctrineTestBundle/Doctrine/DBAL/MockDriverTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\DAMA\DoctrineTestBundle\Doctrine\DBAL;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;

trait MockDriverTrait
{
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 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;
}
}

0 comments on commit 575fa19

Please sign in to comment.