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

⬆️ Compatibility with doctrine/dbal 3.x #681

Merged
merged 3 commits into from
Oct 14, 2022
Merged
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
- { php: '8.1', packages: 'aws/aws-sdk-php:^3.158', phpspec: 'spec/Gaufrette/Adapter/AwsS3Spec.php' }
- { php: '8.1', packages: 'google/apiclient:^2.12', phpspec: 'spec/Gaufrette/Adapter/GoogleCloudStorageSpec.php' }
- { php: '8.1', packages: 'doctrine/dbal:^2.3', phpspec: 'spec/Gaufrette/Adapter/DoctrineDbalSpec.php' }
- { php: '8.1', packages: 'doctrine/dbal:^3.4', phpspec: 'spec/Gaufrette/Adapter/DoctrineDbalSpec.php' }
- { php: '8.1', packages: 'league/flysystem:^1.0', phpspec: 'spec/Gaufrette/Adapter/FlysystemSpec.php' }
- { php: '8.1', packages: 'microsoft/azure-storage-blob:^1.0', phpspec: 'spec/Gaufrette/Adapter/AzureBlobStore' }
- { php: '8.1', packages: 'mongodb/mongodb:^1.1', phpspec: 'spec/Gaufrette/Adapter/GridFSSpec.php' }
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ docker.all-deps: docker.deps ## Install dependencies
docker/run-task php${PHP_VERSION} composer require --no-update \
aws/aws-sdk-php:^3.158 \
google/apiclient:^2.12 \
doctrine/dbal:^2.3 \
doctrine/dbal:^3.4 \
league/flysystem:^1.0 \
microsoft/azure-storage-blob:^1.0 \
phpseclib/phpseclib:^2.0 \
Expand Down Expand Up @@ -67,7 +67,7 @@ require-all-legacy: # kept for compatibility with the old CI config, to be remov
composer require --no-update \
aws/aws-sdk-php:^3.158 \
google/apiclient:^2.12 \
doctrine/dbal:^2.3 \
doctrine/dbal:^3.4 \
league/flysystem:^1.0 \
microsoft/azure-storage-blob:^1.0 \
phpseclib/phpseclib:^2.0 \
Expand Down
63 changes: 51 additions & 12 deletions spec/Gaufrette/Adapter/DoctrineDbalSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require_once 'functions.php';

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Result;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

Expand Down Expand Up @@ -46,13 +46,18 @@ function it_checks_if_file_exists(Connection $connection)
return sprintf('"%s"', $argument[0]);
});

$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}

$connection
->fetchColumn('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->$method('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(12);
$this->exists('filename')->shouldReturn(true);

$connection
->fetchColumn('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->$method('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(0);
$this->exists('filename')->shouldReturn(false);
}
Expand All @@ -67,8 +72,14 @@ function it_writes_to_new_file(Connection $connection)
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});

$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}

$connection
->fetchColumn('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->$method('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(false);
$connection
->insert(
Expand All @@ -90,13 +101,18 @@ function it_writes_to_new_file(Connection $connection)
*/
function it_write_file(Connection $connection)
{
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}

$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->fetchColumn('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->$method('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(true);
$connection
->update(
Expand All @@ -120,13 +136,18 @@ function it_write_file(Connection $connection)
*/
function it_reads_file(Connection $connection)
{
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}

$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->fetchColumn('SELECT "content" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->$method('SELECT "content" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn('some content');

$this->read('filename')->shouldReturn('some content');
Expand All @@ -137,13 +158,18 @@ function it_reads_file(Connection $connection)
*/
function it_calculates_checksum(Connection $connection)
{
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}

$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->fetchColumn('SELECT "checksum" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->$method('SELECT "checksum" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(1234);

$this->checksum('filename')->shouldReturn(1234);
Expand All @@ -154,13 +180,18 @@ function it_calculates_checksum(Connection $connection)
*/
function it_gets_mtime(Connection $connection)
{
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}

$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->fetchColumn('SELECT "mtime" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->$method('SELECT "mtime" FROM "someTableName" WHERE "key" = :key', ['key' => 'filename'])
->willReturn(1234);

$this->mtime('filename')->shouldReturn(1234);
Expand Down Expand Up @@ -194,19 +225,27 @@ function it_renames_file(Connection $connection)

/**
* @param \Doctrine\DBAL\Connection $connection
* @param \Doctrine\DBAL\Statement $stmt
*/
function it_get_keys(Connection $connection, Statement $stmt)
function it_get_keys(Connection $connection, $result)
{
$stmt->fetchAll(\PDO::FETCH_COLUMN)->willReturn(['filename', 'filename1', 'filename2']);
if (class_exists(Result::class)) {
// dbal 3.x
$result->beADoubleOf(Result::class);
$result->fetchFirstColumn()->willReturn(['filename', 'filename1', 'filename2']);
} else {
// BC layer for dbal 2.x
$result->beADoubleOf(\Doctrine\DBAL\Statement::class);
$result->fetchAll(\PDO::FETCH_COLUMN)->willReturn(['filename', 'filename1', 'filename2']);
}

$connection
->quoteIdentifier(Argument::any())
->will(function ($argument) {
return sprintf('"%s"', $argument[0]);
});
$connection
->executeQuery('SELECT "key" FROM "someTableName"')
->willReturn($stmt);
->willReturn($result);

$this->keys()->shouldReturn(['filename', 'filename1', 'filename2']);
}
Expand Down
28 changes: 25 additions & 3 deletions src/Gaufrette/Adapter/DoctrineDbal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Gaufrette\Adapter;

use Doctrine\DBAL\Result;
use Gaufrette\Adapter;
use Gaufrette\Util;
use Doctrine\DBAL\Connection;
Expand Down Expand Up @@ -52,6 +53,12 @@ public function keys()
$this->getQuotedTable()
));

if (class_exists(Result::class)) {
// dbal 3.x
return $stmt->fetchFirstColumn();
}

// BC layer for dbal 2.x
return $stmt->fetchAll(\PDO::FETCH_COLUMN);
}

Expand Down Expand Up @@ -88,7 +95,12 @@ public function checksum($key)
*/
public function exists($key)
{
return (boolean) $this->connection->fetchColumn(
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, $method)) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}

return (boolean) $this->connection->$method(
sprintf(
'SELECT COUNT(%s) FROM %s WHERE %s = :key',
$this->getQuotedColumn('key'),
Expand Down Expand Up @@ -153,7 +165,12 @@ public function isDirectory($key)

private function getColumnValue($key, $column)
{
$value = $this->connection->fetchColumn(
$method = 'fetchOne'; // dbal 3.x
if (!method_exists(Connection::class, $method)) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}

$value = $this->connection->$method(
sprintf(
'SELECT %s FROM %s WHERE %s = :key',
$this->getQuotedColumn($column),
Expand All @@ -173,7 +190,12 @@ public function listKeys($prefix = '')
{
$prefix = trim($prefix);

$keys = $this->connection->fetchAll(
$method = 'fetchAllAssociative'; // dbal 3.x
if (!method_exists(Connection::class, 'fetchAllAssociative')) {
$method = 'fetchAll'; // BC layer for dbal 2.x
}

$keys = $this->connection->$method(
sprintf(
'SELECT %s AS _key FROM %s WHERE %s LIKE :pattern',
$this->getQuotedColumn('key'),
Expand Down
9 changes: 8 additions & 1 deletion tests/Gaufrette/Functional/Adapter/DoctrineDbalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ protected function setUp(): void
$schema = $this->connection->getSchemaManager()->createSchema();

$table = $schema->createTable('gaufrette');
$table->addColumn('key', 'string', ['unique' => true]);
$column = $table->addColumn('key', 'string');
if (method_exists($column, 'setPlatformOption')) {
// dbal 3.4+
$column->setPlatformOption('unique', true);
} else {
// BC layer dbal 2.x
$column->setUnique(true);
}
$table->addColumn('content', 'blob');
$table->addColumn('mtime', 'integer');
$table->addColumn('checksum', 'string', ['length' => 32]);
Expand Down