From ba8ecfef2e1cfe015aab5483dddff7de59f29c10 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 27 Nov 2021 02:12:31 +0100 Subject: [PATCH] Provide abstract middleware classes --- .../AbstractConnectionMiddleware.php | 97 +++++++++++++++++++ .../Middleware/AbstractDriverMiddleware.php | 61 ++++++++++++ .../Middleware/AbstractResultMiddleware.php | 79 +++++++++++++++ .../AbstractStatementMiddleware.php | 42 ++++++++ src/Logging/Connection.php | 63 +++--------- src/Logging/Driver.php | 48 +-------- src/Logging/Statement.php | 19 ++-- src/Portability/Connection.php | 84 ++-------------- src/Portability/Driver.php | 39 ++------ src/Portability/Result.php | 36 ++----- src/Portability/Statement.php | 28 +----- .../AbstractConnectionMiddlewareTest.php | 77 +++++++++++++++ .../AbstractDriverMiddlewareTest.php | 54 +++++++++++ .../AbstractResultMiddlewareTest.php | 27 ++++++ .../AbstractStatementMiddlewareTest.php | 29 ++++++ 15 files changed, 520 insertions(+), 263 deletions(-) create mode 100644 src/Driver/Middleware/AbstractConnectionMiddleware.php create mode 100644 src/Driver/Middleware/AbstractDriverMiddleware.php create mode 100644 src/Driver/Middleware/AbstractResultMiddleware.php create mode 100644 src/Driver/Middleware/AbstractStatementMiddleware.php create mode 100644 tests/Driver/Middleware/AbstractConnectionMiddlewareTest.php create mode 100644 tests/Driver/Middleware/AbstractDriverMiddlewareTest.php create mode 100644 tests/Driver/Middleware/AbstractResultMiddlewareTest.php create mode 100644 tests/Driver/Middleware/AbstractStatementMiddlewareTest.php diff --git a/src/Driver/Middleware/AbstractConnectionMiddleware.php b/src/Driver/Middleware/AbstractConnectionMiddleware.php new file mode 100644 index 00000000000..951e5f53adf --- /dev/null +++ b/src/Driver/Middleware/AbstractConnectionMiddleware.php @@ -0,0 +1,97 @@ +wrappedConnection = $wrappedConnection; + } + + public function prepare(string $sql): Statement + { + return $this->wrappedConnection->prepare($sql); + } + + public function query(string $sql): Result + { + return $this->wrappedConnection->query($sql); + } + + /** + * {@inheritdoc} + */ + public function quote($value, $type = ParameterType::STRING) + { + return $this->wrappedConnection->quote($value, $type); + } + + public function exec(string $sql): int + { + return $this->wrappedConnection->exec($sql); + } + + /** + * {@inheritdoc} + */ + public function lastInsertId($name = null) + { + if ($name !== null) { + Deprecation::triggerIfCalledFromOutside( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/issues/4687', + 'The usage of Connection::lastInsertId() with a sequence name is deprecated.' + ); + } + + return $this->wrappedConnection->lastInsertId($name); + } + + /** + * {@inheritdoc} + */ + public function beginTransaction() + { + return $this->wrappedConnection->beginTransaction(); + } + + /** + * {@inheritdoc} + */ + public function commit() + { + return $this->wrappedConnection->commit(); + } + + /** + * {@inheritdoc} + */ + public function rollBack() + { + return $this->wrappedConnection->rollBack(); + } + + /** + * {@inheritdoc} + */ + public function getServerVersion() + { + if (! $this->wrappedConnection instanceof ServerInfoAwareConnection) { + throw new LogicException('The underlying connection is not a ServerInfoAwareConnection'); + } + + return $this->wrappedConnection->getServerVersion(); + } +} diff --git a/src/Driver/Middleware/AbstractDriverMiddleware.php b/src/Driver/Middleware/AbstractDriverMiddleware.php new file mode 100644 index 00000000000..ab1f508f7ee --- /dev/null +++ b/src/Driver/Middleware/AbstractDriverMiddleware.php @@ -0,0 +1,61 @@ +wrappedDriver = $wrappedDriver; + } + + /** + * {@inheritdoc} + */ + public function connect(array $params) + { + return $this->wrappedDriver->connect($params); + } + + /** + * {@inheritdoc} + */ + public function getDatabasePlatform() + { + return $this->wrappedDriver->getDatabasePlatform(); + } + + /** + * {@inheritdoc} + */ + public function getSchemaManager(Connection $conn, AbstractPlatform $platform) + { + return $this->wrappedDriver->getSchemaManager($conn, $platform); + } + + public function getExceptionConverter(): ExceptionConverter + { + return $this->wrappedDriver->getExceptionConverter(); + } + + /** + * {@inheritdoc} + */ + public function createDatabasePlatformForVersion($version) + { + if ($this->wrappedDriver instanceof VersionAwarePlatformDriver) { + return $this->wrappedDriver->createDatabasePlatformForVersion($version); + } + + return $this->wrappedDriver->getDatabasePlatform(); + } +} diff --git a/src/Driver/Middleware/AbstractResultMiddleware.php b/src/Driver/Middleware/AbstractResultMiddleware.php new file mode 100644 index 00000000000..ebc63c57078 --- /dev/null +++ b/src/Driver/Middleware/AbstractResultMiddleware.php @@ -0,0 +1,79 @@ +wrappedResult = $result; + } + + /** + * {@inheritdoc} + */ + public function fetchNumeric() + { + return $this->wrappedResult->fetchNumeric(); + } + + /** + * {@inheritdoc} + */ + public function fetchAssociative() + { + return $this->wrappedResult->fetchAssociative(); + } + + /** + * {@inheritdoc} + */ + public function fetchOne() + { + return $this->wrappedResult->fetchOne(); + } + + /** + * {@inheritdoc} + */ + public function fetchAllNumeric(): array + { + return $this->wrappedResult->fetchAllNumeric(); + } + + /** + * {@inheritdoc} + */ + public function fetchAllAssociative(): array + { + return $this->wrappedResult->fetchAllAssociative(); + } + + /** + * {@inheritdoc} + */ + public function fetchFirstColumn(): array + { + return $this->wrappedResult->fetchFirstColumn(); + } + + public function rowCount(): int + { + return $this->wrappedResult->rowCount(); + } + + public function columnCount(): int + { + return $this->wrappedResult->columnCount(); + } + + public function free(): void + { + $this->wrappedResult->free(); + } +} diff --git a/src/Driver/Middleware/AbstractStatementMiddleware.php b/src/Driver/Middleware/AbstractStatementMiddleware.php new file mode 100644 index 00000000000..a646cd30cb2 --- /dev/null +++ b/src/Driver/Middleware/AbstractStatementMiddleware.php @@ -0,0 +1,42 @@ +wrappedStatement = $wrappedStatement; + } + + /** + * {@inheritdoc} + */ + public function bindValue($param, $value, $type = ParameterType::STRING) + { + return $this->wrappedStatement->bindValue($param, $value, $type); + } + + /** + * {@inheritdoc} + */ + public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null) + { + return $this->wrappedStatement->bindParam($param, $variable, $type, $length); + } + + /** + * {@inheritdoc} + */ + public function execute($params = null): Result + { + return $this->wrappedStatement->execute($params); + } +} diff --git a/src/Logging/Connection.php b/src/Logging/Connection.php index 9bb11ac0593..2175dc467df 100644 --- a/src/Logging/Connection.php +++ b/src/Logging/Connection.php @@ -5,19 +5,13 @@ namespace Doctrine\DBAL\Logging; use Doctrine\DBAL\Driver\Connection as ConnectionInterface; +use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware; use Doctrine\DBAL\Driver\Result; -use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\Statement as DriverStatement; -use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; -use LogicException; use Psr\Log\LoggerInterface; -final class Connection implements ServerInfoAwareConnection +final class Connection extends AbstractConnectionMiddleware { - /** @var ConnectionInterface */ - private $connection; - /** @var LoggerInterface */ private $logger; @@ -26,8 +20,9 @@ final class Connection implements ServerInfoAwareConnection */ public function __construct(ConnectionInterface $connection, LoggerInterface $logger) { - $this->connection = $connection; - $this->logger = $logger; + parent::__construct($connection); + + $this->logger = $logger; } public function __destruct() @@ -38,7 +33,7 @@ public function __destruct() public function prepare(string $sql): DriverStatement { return new Statement( - $this->connection->prepare($sql), + parent::prepare($sql), $this->logger, $sql ); @@ -48,38 +43,14 @@ public function query(string $sql): Result { $this->logger->debug('Executing query: {sql}', ['sql' => $sql]); - return $this->connection->query($sql); - } - - /** - * {@inheritDoc} - */ - public function quote($value, $type = ParameterType::STRING) - { - return $this->connection->quote($value, $type); + return parent::query($sql); } public function exec(string $sql): int { $this->logger->debug('Executing statement: {sql}', ['sql' => $sql]); - return $this->connection->exec($sql); - } - - /** - * {@inheritDoc} - */ - public function lastInsertId($name = null) - { - if ($name !== null) { - Deprecation::triggerIfCalledFromOutside( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/issues/4687', - 'The usage of Connection::lastInsertId() with a sequence name is deprecated.' - ); - } - - return $this->connection->lastInsertId($name); + return parent::exec($sql); } /** @@ -89,7 +60,7 @@ public function beginTransaction() { $this->logger->debug('Beginning transaction'); - return $this->connection->beginTransaction(); + return parent::beginTransaction(); } /** @@ -99,7 +70,7 @@ public function commit() { $this->logger->debug('Committing transaction'); - return $this->connection->commit(); + return parent::commit(); } /** @@ -109,18 +80,6 @@ public function rollBack() { $this->logger->debug('Rolling back transaction'); - return $this->connection->rollBack(); - } - - /** - * {@inheritDoc} - */ - public function getServerVersion() - { - if (! $this->connection instanceof ServerInfoAwareConnection) { - throw new LogicException('The underlying connection is not a ServerInfoAwareConnection'); - } - - return $this->connection->getServerVersion(); + return parent::rollBack(); } } diff --git a/src/Logging/Driver.php b/src/Logging/Driver.php index 5ee12b4312a..12c8cffc6c5 100644 --- a/src/Logging/Driver.php +++ b/src/Logging/Driver.php @@ -4,18 +4,12 @@ namespace Doctrine\DBAL\Logging; -use Doctrine\DBAL\Connection as DBALConnection; use Doctrine\DBAL\Driver as DriverInterface; -use Doctrine\DBAL\Driver\API\ExceptionConverter; -use Doctrine\DBAL\Platforms\AbstractPlatform; -use Doctrine\DBAL\VersionAwarePlatformDriver; +use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware; use Psr\Log\LoggerInterface; -final class Driver implements VersionAwarePlatformDriver +final class Driver extends AbstractDriverMiddleware { - /** @var DriverInterface */ - private $driver; - /** @var LoggerInterface */ private $logger; @@ -24,7 +18,8 @@ final class Driver implements VersionAwarePlatformDriver */ public function __construct(DriverInterface $driver, LoggerInterface $logger) { - $this->driver = $driver; + parent::__construct($driver); + $this->logger = $logger; } @@ -36,44 +31,11 @@ public function connect(array $params) $this->logger->info('Connecting with parameters {params}', ['params' => $this->maskPassword($params)]); return new Connection( - $this->driver->connect($params), + parent::connect($params), $this->logger ); } - /** - * {@inheritDoc} - */ - public function getDatabasePlatform() - { - return $this->driver->getDatabasePlatform(); - } - - /** - * {@inheritDoc} - */ - public function getSchemaManager(DBALConnection $conn, AbstractPlatform $platform) - { - return $this->driver->getSchemaManager($conn, $platform); - } - - public function getExceptionConverter(): ExceptionConverter - { - return $this->driver->getExceptionConverter(); - } - - /** - * {@inheritDoc} - */ - public function createDatabasePlatformForVersion($version) - { - if ($this->driver instanceof VersionAwarePlatformDriver) { - return $this->driver->createDatabasePlatformForVersion($version); - } - - return $this->driver->getDatabasePlatform(); - } - /** * @param array $params Connection parameters * diff --git a/src/Logging/Statement.php b/src/Logging/Statement.php index 37ebf343da9..e993767aa08 100644 --- a/src/Logging/Statement.php +++ b/src/Logging/Statement.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Logging; +use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware; use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Statement as StatementInterface; use Doctrine\DBAL\ParameterType; @@ -12,11 +13,8 @@ use function array_slice; use function func_get_args; -final class Statement implements StatementInterface +final class Statement extends AbstractStatementMiddleware { - /** @var StatementInterface */ - private $statement; - /** @var LoggerInterface */ private $logger; @@ -34,9 +32,10 @@ final class Statement implements StatementInterface */ public function __construct(StatementInterface $statement, LoggerInterface $logger, string $sql) { - $this->statement = $statement; - $this->logger = $logger; - $this->sql = $sql; + parent::__construct($statement); + + $this->logger = $logger; + $this->sql = $sql; } /** @@ -47,7 +46,7 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le $this->params[$param] = &$variable; $this->types[$param] = $type; - return $this->statement->bindParam($param, $variable, $type, ...array_slice(func_get_args(), 3)); + return parent::bindParam($param, $variable, $type, ...array_slice(func_get_args(), 3)); } /** @@ -58,7 +57,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING) $this->params[$param] = $value; $this->types[$param] = $type; - return $this->statement->bindValue($param, $value, $type); + return parent::bindValue($param, $value, $type); } /** @@ -72,6 +71,6 @@ public function execute($params = null): ResultInterface 'types' => $this->types, ]); - return $this->statement->execute($params); + return parent::execute($params); } } diff --git a/src/Portability/Connection.php b/src/Portability/Connection.php index 9b9d8d09b6e..a22ab92ffc0 100644 --- a/src/Portability/Connection.php +++ b/src/Portability/Connection.php @@ -3,17 +3,14 @@ namespace Doctrine\DBAL\Portability; use Doctrine\DBAL\Driver\Connection as ConnectionInterface; +use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware; use Doctrine\DBAL\Driver\Result as DriverResult; -use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\Statement as DriverStatement; -use Doctrine\DBAL\ParameterType; -use Doctrine\Deprecations\Deprecation; -use LogicException; /** * Portability wrapper for a Connection. */ -final class Connection implements ServerInfoAwareConnection +final class Connection extends AbstractConnectionMiddleware { public const PORTABILITY_ALL = 255; public const PORTABILITY_NONE = 0; @@ -21,22 +18,20 @@ final class Connection implements ServerInfoAwareConnection public const PORTABILITY_EMPTY_TO_NULL = 4; public const PORTABILITY_FIX_CASE = 8; - /** @var ConnectionInterface */ - private $connection; - /** @var Converter */ private $converter; public function __construct(ConnectionInterface $connection, Converter $converter) { - $this->connection = $connection; - $this->converter = $converter; + parent::__construct($connection); + + $this->converter = $converter; } public function prepare(string $sql): DriverStatement { return new Statement( - $this->connection->prepare($sql), + parent::prepare($sql), $this->converter ); } @@ -44,73 +39,8 @@ public function prepare(string $sql): DriverStatement public function query(string $sql): DriverResult { return new Result( - $this->connection->query($sql), + parent::query($sql), $this->converter ); } - - /** - * {@inheritDoc} - */ - public function quote($value, $type = ParameterType::STRING) - { - return $this->connection->quote($value, $type); - } - - public function exec(string $sql): int - { - return $this->connection->exec($sql); - } - - /** - * {@inheritDoc} - */ - public function lastInsertId($name = null) - { - if ($name !== null) { - Deprecation::triggerIfCalledFromOutside( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/issues/4687', - 'The usage of Connection::lastInsertId() with a sequence name is deprecated.' - ); - } - - return $this->connection->lastInsertId($name); - } - - /** - * {@inheritDoc} - */ - public function beginTransaction() - { - return $this->connection->beginTransaction(); - } - - /** - * {@inheritDoc} - */ - public function commit() - { - return $this->connection->commit(); - } - - /** - * {@inheritDoc} - */ - public function rollBack() - { - return $this->connection->rollBack(); - } - - /** - * {@inheritDoc} - */ - public function getServerVersion() - { - if (! $this->connection instanceof ServerInfoAwareConnection) { - throw new LogicException('The underlying connection is not a ServerInfoAwareConnection'); - } - - return $this->connection->getServerVersion(); - } } diff --git a/src/Portability/Driver.php b/src/Portability/Driver.php index 5ae410cd005..7ea0a5aaed8 100644 --- a/src/Portability/Driver.php +++ b/src/Portability/Driver.php @@ -3,20 +3,15 @@ namespace Doctrine\DBAL\Portability; use Doctrine\DBAL\ColumnCase; -use Doctrine\DBAL\Connection as DBALConnection; use Doctrine\DBAL\Driver as DriverInterface; -use Doctrine\DBAL\Driver\API\ExceptionConverter; +use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware; use Doctrine\DBAL\Driver\PDO; -use Doctrine\DBAL\Platforms\AbstractPlatform; use const CASE_LOWER; use const CASE_UPPER; -final class Driver implements DriverInterface +final class Driver extends AbstractDriverMiddleware { - /** @var DriverInterface */ - private $driver; - /** @var int */ private $mode; @@ -25,9 +20,10 @@ final class Driver implements DriverInterface public function __construct(DriverInterface $driver, int $mode, int $case) { - $this->driver = $driver; - $this->mode = $mode; - $this->case = $case; + parent::__construct($driver); + + $this->mode = $mode; + $this->case = $case; } /** @@ -35,7 +31,7 @@ public function __construct(DriverInterface $driver, int $mode, int $case) */ public function connect(array $params) { - $connection = $this->driver->connect($params); + $connection = parent::connect($params); $portability = (new OptimizeFlags())( $this->getDatabasePlatform(), @@ -66,25 +62,4 @@ public function connect(array $params) new Converter($convertEmptyStringToNull, $rightTrimString, $case) ); } - - /** - * {@inheritDoc} - */ - public function getDatabasePlatform() - { - return $this->driver->getDatabasePlatform(); - } - - /** - * {@inheritDoc} - */ - public function getSchemaManager(DBALConnection $conn, AbstractPlatform $platform) - { - return $this->driver->getSchemaManager($conn, $platform); - } - - public function getExceptionConverter(): ExceptionConverter - { - return $this->driver->getExceptionConverter(); - } } diff --git a/src/Portability/Result.php b/src/Portability/Result.php index 1fa91ab4813..d8440b66318 100644 --- a/src/Portability/Result.php +++ b/src/Portability/Result.php @@ -4,13 +4,11 @@ namespace Doctrine\DBAL\Portability; +use Doctrine\DBAL\Driver\Middleware\AbstractResultMiddleware; use Doctrine\DBAL\Driver\Result as ResultInterface; -final class Result implements ResultInterface +final class Result extends AbstractResultMiddleware { - /** @var ResultInterface */ - private $result; - /** @var Converter */ private $converter; @@ -19,7 +17,8 @@ final class Result implements ResultInterface */ public function __construct(ResultInterface $result, Converter $converter) { - $this->result = $result; + parent::__construct($result); + $this->converter = $converter; } @@ -29,7 +28,7 @@ public function __construct(ResultInterface $result, Converter $converter) public function fetchNumeric() { return $this->converter->convertNumeric( - $this->result->fetchNumeric() + parent::fetchNumeric() ); } @@ -39,7 +38,7 @@ public function fetchNumeric() public function fetchAssociative() { return $this->converter->convertAssociative( - $this->result->fetchAssociative() + parent::fetchAssociative() ); } @@ -49,7 +48,7 @@ public function fetchAssociative() public function fetchOne() { return $this->converter->convertOne( - $this->result->fetchOne() + parent::fetchOne() ); } @@ -59,7 +58,7 @@ public function fetchOne() public function fetchAllNumeric(): array { return $this->converter->convertAllNumeric( - $this->result->fetchAllNumeric() + parent::fetchAllNumeric() ); } @@ -69,7 +68,7 @@ public function fetchAllNumeric(): array public function fetchAllAssociative(): array { return $this->converter->convertAllAssociative( - $this->result->fetchAllAssociative() + parent::fetchAllAssociative() ); } @@ -79,22 +78,7 @@ public function fetchAllAssociative(): array public function fetchFirstColumn(): array { return $this->converter->convertFirstColumn( - $this->result->fetchFirstColumn() + parent::fetchFirstColumn() ); } - - public function rowCount(): int - { - return $this->result->rowCount(); - } - - public function columnCount(): int - { - return $this->result->columnCount(); - } - - public function free(): void - { - $this->result->free(); - } } diff --git a/src/Portability/Statement.php b/src/Portability/Statement.php index f3f94e413e5..b104cf78af2 100644 --- a/src/Portability/Statement.php +++ b/src/Portability/Statement.php @@ -2,18 +2,15 @@ namespace Doctrine\DBAL\Portability; +use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware; use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Statement as DriverStatement; -use Doctrine\DBAL\ParameterType; /** * Portability wrapper for a Statement. */ -final class Statement implements DriverStatement +final class Statement extends AbstractStatementMiddleware { - /** @var DriverStatement */ - private $stmt; - /** @var Converter */ private $converter; @@ -22,24 +19,9 @@ final class Statement implements DriverStatement */ public function __construct(DriverStatement $stmt, Converter $converter) { - $this->stmt = $stmt; - $this->converter = $converter; - } - - /** - * {@inheritdoc} - */ - public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null) - { - return $this->stmt->bindParam($param, $variable, $type, $length); - } + parent::__construct($stmt); - /** - * {@inheritdoc} - */ - public function bindValue($param, $value, $type = ParameterType::STRING) - { - return $this->stmt->bindValue($param, $value, $type); + $this->converter = $converter; } /** @@ -48,7 +30,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING) public function execute($params = null): ResultInterface { return new Result( - $this->stmt->execute($params), + parent::execute($params), $this->converter ); } diff --git a/tests/Driver/Middleware/AbstractConnectionMiddlewareTest.php b/tests/Driver/Middleware/AbstractConnectionMiddlewareTest.php new file mode 100644 index 00000000000..5f3b924c2e2 --- /dev/null +++ b/tests/Driver/Middleware/AbstractConnectionMiddlewareTest.php @@ -0,0 +1,77 @@ +createMock(Statement::class); + $connection = $this->createMock(Connection::class); + $connection->expects(self::once()) + ->method('prepare') + ->with('SELECT 1') + ->willReturn($statement); + + self::assertSame($statement, $this->createMiddleware($connection)->prepare('SELECT 1')); + } + + public function testQuery(): void + { + $result = $this->createMock(Result::class); + $connection = $this->createMock(Connection::class); + $connection->expects(self::once()) + ->method('query') + ->with('SELECT 1') + ->willReturn($result); + + self::assertSame($result, $this->createMiddleware($connection)->query('SELECT 1')); + } + + public function testExec(): void + { + $connection = $this->createMock(Connection::class); + $connection->expects(self::once()) + ->method('exec') + ->with('UPDATE foo SET bar=\'baz\' WHERE some_field > 0') + ->willReturn(42); + + self::assertSame( + 42, + $this->createMiddleware($connection)->exec('UPDATE foo SET bar=\'baz\' WHERE some_field > 0') + ); + } + + public function testGetServerVersion(): void + { + $connection = $this->createMock(ServerInfoAwareConnection::class); + $connection->expects(self::once()) + ->method('getServerVersion') + ->willReturn('1.2.3'); + + self::assertSame('1.2.3', $this->createMiddleware($connection)->getServerVersion()); + } + + public function testGetServerVersionFailsOnLegacyConnections(): void + { + $connection = $this->createMock(Connection::class); + $middleware = $this->createMiddleware($connection); + + $this->expectException(LogicException::class); + $middleware->getServerVersion(); + } + + private function createMiddleware(Connection $connection): AbstractConnectionMiddleware + { + return new class ($connection) extends AbstractConnectionMiddleware { + }; + } +} diff --git a/tests/Driver/Middleware/AbstractDriverMiddlewareTest.php b/tests/Driver/Middleware/AbstractDriverMiddlewareTest.php new file mode 100644 index 00000000000..600cdfd0e83 --- /dev/null +++ b/tests/Driver/Middleware/AbstractDriverMiddlewareTest.php @@ -0,0 +1,54 @@ +createMock(Connection::class); + $driver = $this->createMock(Driver::class); + $driver->expects(self::once()) + ->method('connect') + ->with(['foo' => 'bar']) + ->willReturn($connection); + + self::assertSame($connection, $this->createMiddleware($driver)->connect(['foo' => 'bar'])); + } + + public function testCreateDatabasePlatformForVersion(): void + { + $platform = $this->createMock(AbstractPlatform::class); + $driver = $this->createMock(VersionAwarePlatformDriver::class); + $driver->expects(self::once()) + ->method('createDatabasePlatformForVersion') + ->with('1.2.3') + ->willReturn($platform); + + self::assertSame($platform, $this->createMiddleware($driver)->createDatabasePlatformForVersion('1.2.3')); + } + + public function testCreateDatabasePlatformForVersionWithLegacyDriver(): void + { + $platform = $this->createMock(AbstractPlatform::class); + $driver = $this->createMock(Driver::class); + $driver->expects(self::once()) + ->method('getDatabasePlatform') + ->willReturn($platform); + + self::assertSame($platform, $this->createMiddleware($driver)->createDatabasePlatformForVersion('1.2.3')); + } + + private function createMiddleware(Driver $driver): AbstractDriverMiddleware + { + return new class ($driver) extends AbstractDriverMiddleware { + }; + } +} diff --git a/tests/Driver/Middleware/AbstractResultMiddlewareTest.php b/tests/Driver/Middleware/AbstractResultMiddlewareTest.php new file mode 100644 index 00000000000..dfa10cc3f2a --- /dev/null +++ b/tests/Driver/Middleware/AbstractResultMiddlewareTest.php @@ -0,0 +1,27 @@ + 'value', 'another_field' => 42]; + $result = $this->createMock(Result::class); + $result->expects(self::once()) + ->method('fetchAssociative') + ->willReturn($row); + + self::assertSame($row, $this->createMiddleware($result)->fetchAssociative()); + } + + private function createMiddleware(Result $result): AbstractResultMiddleware + { + return new class ($result) extends AbstractResultMiddleware { + }; + } +} diff --git a/tests/Driver/Middleware/AbstractStatementMiddlewareTest.php b/tests/Driver/Middleware/AbstractStatementMiddlewareTest.php new file mode 100644 index 00000000000..2aeb15cbbd1 --- /dev/null +++ b/tests/Driver/Middleware/AbstractStatementMiddlewareTest.php @@ -0,0 +1,29 @@ +createMock(Result::class); + $statement = $this->createMock(Statement::class); + $statement->expects(self::once()) + ->method('execute') + ->with(['foo' => 'bar']) + ->willReturn($result); + + self::assertSame($result, $this->createMiddleware($statement)->execute(['foo' => 'bar'])); + } + + private function createMiddleware(Statement $statement): AbstractStatementMiddleware + { + return new class ($statement) extends AbstractStatementMiddleware { + }; + } +}