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 committed May 14, 2021
1 parent e026b54 commit 385f7f6
Showing 1 changed file with 66 additions and 6 deletions.
72 changes: 66 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 Down Expand Up @@ -181,7 +182,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 +207,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 +234,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 +252,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 +337,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 +364,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 +393,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 +427,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 385f7f6

Please sign in to comment.