Refactored ResultCacheTest for better coverage

parent 96f8cabf
...@@ -102,40 +102,40 @@ class ResultCacheTest extends FunctionalTestCase ...@@ -102,40 +102,40 @@ class ResultCacheTest extends FunctionalTestCase
$numExpectedResult[] = array_values($v); $numExpectedResult[] = array_values($v);
} }
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = $this->hydrateStmt($stmt, static function (ResultStatement $stmt) { $data = $this->hydrateViaFetchAll($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAssociative(); return $stmt->fetchAllAssociative();
}); });
self::assertEquals($this->expectedResult, $data); self::assertEquals($this->expectedResult, $data);
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = $this->hydrateStmt($stmt, static function (ResultStatement $stmt) { $data = $this->hydrateViaFetchAll($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchNumeric(); return $stmt->fetchAllNumeric();
}); });
self::assertEquals($numExpectedResult, $data); self::assertEquals($numExpectedResult, $data);
} }
/** /**
* @dataProvider fetchModeProvider * @dataProvider fetchProvider
*/ */
public function testIteratorFetch(callable $fetchMode) : void public function testFetchViaIteration(callable $fetch, callable $fetchAll) : void
{ {
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = $this->hydrateStmt($stmt, $fetchMode); $data = $this->hydrateViaFetchAll($stmt, $fetchAll);
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$dataIterator = $this->hydrateStmtIterator($stmt, $fetchMode); $dataIterator = $this->hydrateViaIteration($stmt, $fetch);
self::assertEquals($data, $dataIterator); self::assertEquals($data, $dataIterator);
} }
public function testDontCloseNoCache() : void public function testDontCloseNoCache() : void
{ {
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = []; $data = [];
...@@ -143,7 +143,7 @@ class ResultCacheTest extends FunctionalTestCase ...@@ -143,7 +143,7 @@ class ResultCacheTest extends FunctionalTestCase
$data[] = $row; $data[] = $row;
} }
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$data = []; $data = [];
...@@ -156,14 +156,14 @@ class ResultCacheTest extends FunctionalTestCase ...@@ -156,14 +156,14 @@ class ResultCacheTest extends FunctionalTestCase
public function testDontFinishNoCache() : void public function testDontFinishNoCache() : void
{ {
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$stmt->fetchAssociative(); $stmt->fetchAssociative();
$stmt->closeCursor(); $stmt->closeCursor();
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
$this->hydrateStmt($stmt, static function (ResultStatement $stmt) { $this->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchNumeric(); return $stmt->fetchNumeric();
}); });
...@@ -173,14 +173,14 @@ class ResultCacheTest extends FunctionalTestCase ...@@ -173,14 +173,14 @@ class ResultCacheTest extends FunctionalTestCase
public function testFetchAllAndFinishSavesCache() : void public function testFetchAllAndFinishSavesCache() : void
{ {
$layerCache = new ArrayCache(); $layerCache = new ArrayCache();
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'testcachekey', $layerCache)); $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'testcachekey', $layerCache));
$stmt->fetchAllAssociative(); $stmt->fetchAllAssociative();
$stmt->closeCursor(); $stmt->closeCursor();
self::assertCount(1, $layerCache->fetch('testcachekey')); self::assertCount(1, $layerCache->fetch('testcachekey'));
} }
public function testfetchColumn() : void public function testFetchColumn() : void
{ {
$query = $this->connection->getDatabasePlatform() $query = $this->connection->getDatabasePlatform()
->getDummySelectSQL('1'); ->getDummySelectSQL('1');
...@@ -201,29 +201,29 @@ class ResultCacheTest extends FunctionalTestCase ...@@ -201,29 +201,29 @@ class ResultCacheTest extends FunctionalTestCase
*/ */
private function assertCacheNonCacheSelectSameFetchModeAreEqual(array $expectedResult, callable $fetchMode) : void private function assertCacheNonCacheSelectSameFetchModeAreEqual(array $expectedResult, callable $fetchMode) : void
{ {
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
self::assertEquals(2, $stmt->columnCount()); self::assertEquals(2, $stmt->columnCount());
$data = $this->hydrateStmt($stmt, $fetchMode); $data = $this->hydrateViaIteration($stmt, $fetchMode);
self::assertEquals($expectedResult, $data); self::assertEquals($expectedResult, $data);
$stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey'));
self::assertEquals(2, $stmt->columnCount()); self::assertEquals(2, $stmt->columnCount());
$data = $this->hydrateStmt($stmt, $fetchMode); $data = $this->hydrateViaIteration($stmt, $fetchMode);
self::assertEquals($expectedResult, $data); self::assertEquals($expectedResult, $data);
self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit'); self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit');
} }
public function testEmptyResultCache() : void public function testEmptyResultCache() : void
{ {
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey'));
$data = $this->hydrateStmt($stmt, static function (ResultStatement $stmt) { $this->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAssociative(); return $stmt->fetchAssociative();
}); });
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey'));
$data = $this->hydrateStmt($stmt, static function (ResultStatement $stmt) { $this->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAssociative(); return $stmt->fetchAssociative();
}); });
...@@ -232,14 +232,15 @@ class ResultCacheTest extends FunctionalTestCase ...@@ -232,14 +232,15 @@ class ResultCacheTest extends FunctionalTestCase
public function testChangeCacheImpl() : void public function testChangeCacheImpl() : void
{ {
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey')); $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey'));
$data = $this->hydrateStmt($stmt, static function (ResultStatement $stmt) { $this->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAssociative(); return $stmt->fetchAssociative();
}); });
$secondCache = new ArrayCache(); $secondCache = new ArrayCache();
$stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey', $secondCache));
$data = $this->hydrateStmt($stmt, static function (ResultStatement $stmt) { $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey', $secondCache));
$this->hydrateViaIteration($stmt, static function (ResultStatement $stmt) {
return $stmt->fetchAssociative(); return $stmt->fetchAssociative();
}); });
...@@ -250,28 +251,44 @@ class ResultCacheTest extends FunctionalTestCase ...@@ -250,28 +251,44 @@ class ResultCacheTest extends FunctionalTestCase
/** /**
* @return iterable<string,array<int,mixed>> * @return iterable<string,array<int,mixed>>
*/ */
public static function fetchModeProvider() : iterable public static function fetchProvider() : iterable
{ {
yield 'associative' => [ yield 'associative' => [
static function (ResultStatement $stmt) { static function (ResultStatement $stmt) {
return $stmt->fetchAssociative(); return $stmt->fetchAssociative();
}, },
static function (ResultStatement $stmt) {
return $stmt->fetchAllAssociative();
},
]; ];
yield 'numeric' => [ yield 'numeric' => [
static function (ResultStatement $stmt) { static function (ResultStatement $stmt) {
return $stmt->fetchNumeric(); return $stmt->fetchNumeric();
}, },
static function (ResultStatement $stmt) {
return $stmt->fetchAllNumeric();
},
];
yield 'column' => [
static function (ResultStatement $stmt) {
return $stmt->fetchOne();
},
static function (ResultStatement $stmt) {
return $stmt->fetchColumn();
},
]; ];
} }
/** /**
* @return array<int, mixed> * @return array<int, mixed>
*/ */
private function hydrateStmt(ResultStatement $stmt, callable $fetchMode) : array private function hydrateViaFetchAll(ResultStatement $stmt, callable $fetchAll) : array
{ {
$data = []; $data = [];
while (($row = $fetchMode($stmt)) !== false) {
foreach ($fetchAll($stmt) as $row) {
$data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row; $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
} }
...@@ -283,11 +300,11 @@ class ResultCacheTest extends FunctionalTestCase ...@@ -283,11 +300,11 @@ class ResultCacheTest extends FunctionalTestCase
/** /**
* @return array<int, mixed> * @return array<int, mixed>
*/ */
private function hydrateStmtIterator(ResultStatement $stmt, callable $fetchMode) : array private function hydrateViaIteration(ResultStatement $stmt, callable $fetch) : array
{ {
$data = []; $data = [];
while (($row = $fetchMode($stmt)) !== false) { while (($row = $fetch($stmt)) !== false) {
$data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row; $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment