Skip to content

Commit

Permalink
Add tests for wrapped PSR-6 caches
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored and greg0ire committed May 6, 2021
1 parent e026b54 commit f4ddacc
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions tests/Functional/ResultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Tests\Functional;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\Result;
Expand All @@ -13,6 +14,7 @@
use function array_change_key_case;
use function array_shift;
use function array_values;
use function class_exists;
use function is_array;

use const CASE_LOWER;
Expand Down Expand Up @@ -233,6 +235,10 @@ public function testFetchingAllRowsSavesCache(callable $fetchAll): void
*/
public function testFetchingAllRowsSavesCacheLegacy(callable $fetchAll): void
{
if (! class_exists(ArrayCache::class)) {
self::markTestSkipped('This test requires the legacy ArrayCache class.');
}

$layerCache = new ArrayCache();

$result = $this->connection->executeQuery(
Expand All @@ -247,6 +253,26 @@ public function testFetchingAllRowsSavesCacheLegacy(callable $fetchAll): void
self::assertCount(1, $layerCache->fetch('testcachekey'));
}

/**
* @dataProvider fetchAllProvider
*/
public function testFetchingAllRowsSavesCacheLegacyWrapped(callable $fetchAll): void
{
$arrayAdapter = new ArrayAdapter();
$layerCache = DoctrineProvider::wrap($arrayAdapter);

$result = $this->connection->executeQuery(
'SELECT * FROM caching WHERE test_int > 500',
[],
[],
new QueryCacheProfile(0, 'testcachekey', $layerCache)
);

$fetchAll($result);

self::assertCount(1, $arrayAdapter->getItem('testcachekey')->get());
}

/**
* @return iterable<string,list<mixed>>
*/
Expand Down Expand Up @@ -374,6 +400,10 @@ public function testChangeCacheImpl(): void

public function testChangeCacheImplLegacy(): void
{
if (! class_exists(ArrayCache::class)) {
self::markTestSkipped('This test requires the legacy ArrayCache class.');
}

$stmt = $this->connection->executeQuery(
'SELECT * FROM caching WHERE test_int > 500',
[],
Expand Down Expand Up @@ -402,6 +432,37 @@ public function testChangeCacheImplLegacy(): void
self::assertCount(1, $secondCache->fetch('emptycachekey'));
}

public function testChangeCacheImplLegacyWrapped(): void
{
$stmt = $this->connection->executeQuery(
'SELECT * FROM caching WHERE test_int > 500',
[],
[],
new QueryCacheProfile(10, 'emptycachekey')
);

$this->hydrateViaIteration($stmt, static function (Result $result) {
return $result->fetchAssociative();
});

$arrayAdapter = new ArrayAdapter();
$secondCache = DoctrineProvider::wrap($arrayAdapter);

$stmt = $this->connection->executeQuery(
'SELECT * FROM caching WHERE test_int > 500',
[],
[],
new QueryCacheProfile(10, 'emptycachekey', $secondCache)
);

$this->hydrateViaIteration($stmt, static function (Result $result) {
return $result->fetchAssociative();
});

self::assertCount(2, $this->sqlLogger->queries, 'two hits');
self::assertCount(1, $arrayAdapter->getItem('emptycachekey')->get());
}

/**
* @return iterable<string,array<int,mixed>>
*/
Expand Down

0 comments on commit f4ddacc

Please sign in to comment.