Skip to content

Commit

Permalink
✅ fix usage of method_exist for phpspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- committed Oct 14, 2022
1 parent 7810b3e commit 6834362
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
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->fetchAllNumeric()->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
19 changes: 13 additions & 6 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->fetchAllNumeric();
}

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

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

return (boolean) $this->connection->$method(
Expand Down Expand Up @@ -159,8 +166,8 @@ public function isDirectory($key)
private function getColumnValue($key, $column)
{
$method = 'fetchOne'; // dbal 3.x
if (!method_exists($this->connection, $method)) {
$method = 'fetchColumn'; // dbal 2.x
if (!method_exists(Connection::class, $method)) {
$method = 'fetchColumn'; // BC layer for dbal 2.x
}

$value = $this->connection->$method(
Expand All @@ -184,8 +191,8 @@ public function listKeys($prefix = '')
$prefix = trim($prefix);

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

$keys = $this->connection->$method(
Expand Down

0 comments on commit 6834362

Please sign in to comment.