Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the installation of Doctrine DBAL 3 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor
composer.lock
build/logs/*
!build/logs/.gitkeep
.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
24 changes: 20 additions & 4 deletions src/Activator/DatabaseActivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,15 +90,15 @@ 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'],
$this->options['db_table'],
$this->options['db_column_feature'],
),
['feature_name' => $name]
)->fetchOne();
);

return is_bool($result) ? $result : filter_var($result, FILTER_VALIDATE_BOOLEAN);
}
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions tests/Activator/DatabaseActivatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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([
[
Expand Down