From ac7a9871f6ec532475c326ed136949127e694942 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 2 May 2022 15:22:05 +0200 Subject: [PATCH] Allow the installation of Doctrine DBAL 3 --- .gitignore | 1 + composer.json | 2 +- src/Activator/DatabaseActivator.php | 24 +++++++++++++++++++---- tests/Activator/DatabaseActivatorTest.php | 4 ++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 1251376..61a1e4e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ vendor composer.lock build/logs/* !build/logs/.gitkeep +.phpunit.result.cache diff --git a/composer.json b/composer.json index ad0e70e..17a1695 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "php": "^7.3||^8.0", "flagception/flagception": "^1.7", - "doctrine/dbal": "^2.12", + "doctrine/dbal": "^2.13.1 || ^3.3", "symfony/options-resolver": ">=2.7" }, "require-dev": { diff --git a/src/Activator/DatabaseActivator.php b/src/Activator/DatabaseActivator.php index eb94be0..f45af83 100644 --- a/src/Activator/DatabaseActivator.php +++ b/src/Activator/DatabaseActivator.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Driver\Exception as DBALDriverException; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Exception as DBALException; +use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Schema; use Flagception\Activator\FeatureActivatorInterface; use Flagception\Model\Context; @@ -89,7 +90,7 @@ public function isActive($name, Context $context): bool $this->setup(); // $result contains the response from state (true / false) or false if no feature found - $result = $this->getConnection()->executeQuery( + $result = $this->getConnection()->fetchOne( sprintf( 'SELECT %s FROM %s WHERE %s = :feature_name', $this->options['db_column_state'], @@ -97,7 +98,7 @@ public function isActive($name, Context $context): bool $this->options['db_column_feature'], ), ['feature_name' => $name] - )->fetchOne(); + ); return is_bool($result) ? $result : filter_var($result, FILTER_VALIDATE_BOOLEAN); } @@ -111,7 +112,7 @@ public function isActive($name, Context $context): bool */ private function setup(): void { - $manager = $this->getConnection()->getSchemaManager(); + $manager = $this->createSchemaManager(); if ($this->tablesExist === true || $manager->tablesExist([$this->options['db_table']]) === true) { $this->tablesExist = true; @@ -130,7 +131,7 @@ private function setup(): void $queries = $schema->toSql($platform); foreach ($queries as $query) { - $this->getConnection()->executeQuery($query); + $this->getConnection()->executeStatement($query); } $this->tablesExist = true; @@ -152,4 +153,19 @@ public function getConnection(): Connection return $this->connection; } + + /** + * Fetches the schema manager from the DBAL connection. + * + * @throws DBALException + */ + private function createSchemaManager(): ?AbstractSchemaManager + { + $connection = $this->getConnection(); + + // BC for DBAL 2 + return method_exists($connection, 'createSchemaManager') + ? $connection->createSchemaManager() + : $connection->getSchemaManager(); + } } diff --git a/tests/Activator/DatabaseActivatorTest.php b/tests/Activator/DatabaseActivatorTest.php index b61092b..50488d0 100644 --- a/tests/Activator/DatabaseActivatorTest.php +++ b/tests/Activator/DatabaseActivatorTest.php @@ -88,11 +88,11 @@ public function testSetConnectByUri() public function testSetConnectByPdo() { $activator = new DatabaseActivator([ + 'driver' => 'pdo_mysql', 'pdo' => $pdo = $this->createMock(PDO::class) ]); $pdo - ->expects(static::once()) ->method('getAttribute') ->with(PDO::ATTR_DRIVER_NAME) ->willReturn('mysql'); @@ -173,7 +173,7 @@ private function runIntegration(Connection $connection, $tableName, $featureColu $stateColumn => false ]); - $result = $connection->executeQuery("SELECT $featureColumn, $stateColumn FROM $tableName")->fetchAllAssociative(); + $result = $connection->fetchAllAssociative("SELECT $featureColumn, $stateColumn FROM $tableName"); static::assertEquals([ [