Skip to content

Commit

Permalink
Merge pull request doctrine#4639 from derrabus/improvement/add-test-f…
Browse files Browse the repository at this point in the history
…or-wrapped-cache

Add tests for wrapped PSR-6 caches
  • Loading branch information
greg0ire authored May 14, 2021
2 parents e026b54 + 3cd1007 commit 7d22dd9
Showing 1 changed file with 67 additions and 6 deletions.
73 changes: 67 additions & 6 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 @@ -181,7 +183,7 @@ public function testFetchAndFinishSavesCache(): void
while (($row = $result->fetchNumeric()) !== false) {
}

self::assertCount(1, $this->sqlLogger->queries);
self::assertCount(1, $this->sqlLogger->queries, 'Only one query has hit the database.');
}

public function testDontFinishNoCache(): void
Expand All @@ -206,7 +208,7 @@ public function testDontFinishNoCache(): void
return $result->fetchNumeric();
});

self::assertCount(2, $this->sqlLogger->queries);
self::assertCount(2, $this->sqlLogger->queries, 'Two queries have hit the database.');
}

/**
Expand All @@ -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 @@ -312,7 +338,7 @@ private function assertCacheNonCacheSelectSameFetchModeAreEqual(array $expectedR
self::assertEquals(2, $stmt->columnCount());
$data = $this->hydrateViaIteration($stmt, $fetchMode);
self::assertEquals($expectedResult, $data);
self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit');
self::assertCount(1, $this->sqlLogger->queries, 'Only one query has hit the database.');
}

public function testEmptyResultCache(): void
Expand All @@ -339,7 +365,7 @@ public function testEmptyResultCache(): void
return $result->fetchAssociative();
});

self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit');
self::assertCount(1, $this->sqlLogger->queries, 'Only one query has hit the database.');
}

public function testChangeCacheImpl(): void
Expand Down Expand Up @@ -368,12 +394,16 @@ public function testChangeCacheImpl(): void
return $result->fetchAssociative();
});

self::assertCount(2, $this->sqlLogger->queries, 'two hits');
self::assertCount(2, $this->sqlLogger->queries, 'Two queries have hit the database.');
self::assertCount(1, $secondCache->getItem('emptycachekey')->get());
}

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 All @@ -398,10 +428,41 @@ public function testChangeCacheImplLegacy(): void
return $result->fetchAssociative();
});

self::assertCount(2, $this->sqlLogger->queries, 'two hits');
self::assertCount(2, $this->sqlLogger->queries, 'Two queries have hit the database.');
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 queries have hit the database.');
self::assertCount(1, $arrayAdapter->getItem('emptycachekey')->get());
}

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

0 comments on commit 7d22dd9

Please sign in to comment.