Skip to content

Commit

Permalink
Merge pull request #4623 from derrabus/doctrine-cache-2
Browse files Browse the repository at this point in the history
Support Doctrine Cache 2
  • Loading branch information
greg0ire committed May 1, 2021
2 parents 55da37b + cf25812 commit 35a4071
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -34,7 +34,7 @@
"require": {
"php": "^7.1 || ^8",
"ext-pdo": "*",
"doctrine/cache": "^1.0",
"doctrine/cache": "^1.0|^2.0",
"doctrine/deprecations": "^0.5.3",
"doctrine/event-manager": "^1.0"
},
Expand All @@ -44,6 +44,7 @@
"phpstan/phpstan": "0.12.81",
"phpunit/phpunit": "^7.5.20|^8.5|9.5.0",
"squizlabs/php_codesniffer": "3.6.0",
"symfony/cache": "^4.4",
"symfony/console": "^2.0.5|^3.0|^4.0|^5.0",
"vimeo/psalm": "4.6.4"
},
Expand Down
21 changes: 19 additions & 2 deletions tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Expand Up @@ -3,6 +3,8 @@
namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException;
Expand All @@ -19,8 +21,10 @@
use Exception;
use PDO;
use RuntimeException;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Throwable;

use function class_exists;
use function file_exists;
use function in_array;
use function unlink;
Expand Down Expand Up @@ -389,7 +393,7 @@ public function testResultCompatibilityWhenExecutingQueryWithQueryCacheParam():
$this->connection->getDatabasePlatform()->getDummySelectSQL(),
[],
[],
new QueryCacheProfile(1, 'cacheKey', new ArrayCache())
new QueryCacheProfile(1, 'cacheKey', $this->getArrayCache())
);

self::assertInstanceOf(Result::class, $result);
Expand All @@ -402,10 +406,23 @@ public function testResultCompatibilityWhenExecutingCacheQuery(): void
$this->connection->getDatabasePlatform()->getDummySelectSQL(),
[],
[],
new QueryCacheProfile(1, 'cacheKey', new ArrayCache())
new QueryCacheProfile(1, 'cacheKey', $this->getArrayCache())
);

self::assertInstanceOf(Result::class, $result);
self::assertInstanceOf(Driver\ResultStatement::class, $result);
}

private function getArrayCache(): Cache
{
if (class_exists(DoctrineProvider::class)) {
return DoctrineProvider::wrap(new ArrayAdapter());
}

if (class_exists(ArrayCache::class)) {
return new ArrayCache();
}

self::fail('Cannot instantiate cache backend.');
}
}
25 changes: 21 additions & 4 deletions tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php
Expand Up @@ -3,17 +3,21 @@
namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\DbalFunctionalTestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

use function array_change_key_case;
use function array_merge;
use function array_shift;
use function array_values;
use function class_exists;
use function is_array;

use const CASE_LOWER;
Expand Down Expand Up @@ -49,7 +53,7 @@ protected function setUp(): void
$config = $this->connection->getConfiguration();
$config->setSQLLogger($this->sqlLogger = new DebugStack());

$cache = new ArrayCache();
$cache = $this->getArrayCache();
$config->setResultCacheImpl($cache);
}

Expand Down Expand Up @@ -217,7 +221,7 @@ public function testDontFinishNoCache(): void
*/
public function testFetchingAllRowsSavesCache(callable $fetchAll): void
{
$layerCache = new ArrayCache();
$layerCache = $this->getArrayCache();

$stmt = $this->connection->executeQuery(
'SELECT * FROM caching WHERE test_int > 500',
Expand Down Expand Up @@ -266,7 +270,7 @@ public function testFetchAllColumn(): void
$query = $this->connection->getDatabasePlatform()
->getDummySelectSQL('1');

$qcp = new QueryCacheProfile(0, null, new ArrayCache());
$qcp = new QueryCacheProfile(0, null, $this->getArrayCache());

$stmt = $this->connection->executeCacheQuery($query, [], [], $qcp);
$stmt->fetchAll(FetchMode::COLUMN);
Expand Down Expand Up @@ -339,7 +343,7 @@ public function testChangeCacheImpl(): void

$this->hydrateStmt($stmt);

$secondCache = new ArrayCache();
$secondCache = $this->getArrayCache();

$stmt = $this->connection->executeQuery(
'SELECT * FROM caching WHERE test_int > 500',
Expand Down Expand Up @@ -381,4 +385,17 @@ private function hydrateStmtIterator(Driver\ResultStatement $stmt, int $fetchMod

return $data;
}

private function getArrayCache(): Cache
{
if (class_exists(DoctrineProvider::class)) {
return DoctrineProvider::wrap(new ArrayAdapter());
}

if (class_exists(ArrayCache::class)) {
return new ArrayCache();
}

self::fail('Cannot instantiate cache backend.');
}
}

0 comments on commit 35a4071

Please sign in to comment.