Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for wrapped PSR-6 caches #4639

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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