diff --git a/tests/Functional/ResultCacheTest.php b/tests/Functional/ResultCacheTest.php index 36d7ad668d1..e987a574301 100644 --- a/tests/Functional/ResultCacheTest.php +++ b/tests/Functional/ResultCacheTest.php @@ -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; @@ -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; @@ -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 @@ -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.'); } /** @@ -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( @@ -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> */ @@ -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 @@ -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 @@ -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', [], @@ -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> */