From 9afc42d79cb623d65b2ee9dfd560ec482ff3c129 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Wed, 27 May 2020 09:24:49 -0700 Subject: [PATCH] Refactored ResultCacheTest for better coverage --- tests/Functional/ResultCacheTest.php | 89 +++++++++++++++++----------- 1 file changed, 53 insertions(+), 36 deletions(-) diff --git a/tests/Functional/ResultCacheTest.php b/tests/Functional/ResultCacheTest.php index bf558f57c61..c1cf584832b 100644 --- a/tests/Functional/ResultCacheTest.php +++ b/tests/Functional/ResultCacheTest.php @@ -102,40 +102,40 @@ public function testMixingFetch() : void $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) { - return $stmt->fetchAssociative(); + $data = $this->hydrateViaFetchAll($stmt, static function (ResultStatement $stmt) { + return $stmt->fetchAllAssociative(); }); 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) { - return $stmt->fetchNumeric(); + $data = $this->hydrateViaFetchAll($stmt, static function (ResultStatement $stmt) { + return $stmt->fetchAllNumeric(); }); 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')); - $data = $this->hydrateStmt($stmt, $fetchMode); + $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey')); + $data = $this->hydrateViaFetchAll($stmt, $fetchAll); - $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(10, 'testcachekey')); - $dataIterator = $this->hydrateStmtIterator($stmt, $fetchMode); + $stmt = $this->connection->executeQuery('SELECT * FROM caching ORDER BY test_int ASC', [], [], new QueryCacheProfile(0, 'testcachekey')); + $dataIterator = $this->hydrateViaIteration($stmt, $fetch); self::assertEquals($data, $dataIterator); } 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 = []; @@ -143,7 +143,7 @@ public function testDontCloseNoCache() : void $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 = []; @@ -156,14 +156,14 @@ public function testDontCloseNoCache() : 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->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(); }); @@ -173,14 +173,14 @@ public function testDontFinishNoCache() : void public function testFetchAllAndFinishSavesCache() : void { $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->closeCursor(); self::assertCount(1, $layerCache->fetch('testcachekey')); } - public function testfetchColumn() : void + public function testFetchColumn() : void { $query = $this->connection->getDatabasePlatform() ->getDummySelectSQL('1'); @@ -201,29 +201,29 @@ public function testfetchColumn() : 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()); - $data = $this->hydrateStmt($stmt, $fetchMode); + $data = $this->hydrateViaIteration($stmt, $fetchMode); 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()); - $data = $this->hydrateStmt($stmt, $fetchMode); + $data = $this->hydrateViaIteration($stmt, $fetchMode); self::assertEquals($expectedResult, $data); self::assertCount(1, $this->sqlLogger->queries, 'just one dbal hit'); } public function testEmptyResultCache() : void { - $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey')); - $data = $this->hydrateStmt($stmt, static function (ResultStatement $stmt) { + $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey')); + $this->hydrateViaIteration($stmt, static function (ResultStatement $stmt) { return $stmt->fetchAssociative(); }); - $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey')); - $data = $this->hydrateStmt($stmt, static function (ResultStatement $stmt) { + $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey')); + $this->hydrateViaIteration($stmt, static function (ResultStatement $stmt) { return $stmt->fetchAssociative(); }); @@ -232,14 +232,15 @@ public function testEmptyResultCache() : void public function testChangeCacheImpl() : void { - $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(10, 'emptycachekey')); - $data = $this->hydrateStmt($stmt, static function (ResultStatement $stmt) { + $stmt = $this->connection->executeQuery('SELECT * FROM caching WHERE test_int > 500', [], [], new QueryCacheProfile(0, 'emptycachekey')); + $this->hydrateViaIteration($stmt, static function (ResultStatement $stmt) { return $stmt->fetchAssociative(); }); $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(); }); @@ -250,28 +251,44 @@ public function testChangeCacheImpl() : void /** * @return iterable> */ - public static function fetchModeProvider() : iterable + public static function fetchProvider() : iterable { yield 'associative' => [ static function (ResultStatement $stmt) { return $stmt->fetchAssociative(); }, + static function (ResultStatement $stmt) { + return $stmt->fetchAllAssociative(); + }, ]; yield 'numeric' => [ static function (ResultStatement $stmt) { 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 */ - private function hydrateStmt(ResultStatement $stmt, callable $fetchMode) : array + private function hydrateViaFetchAll(ResultStatement $stmt, callable $fetchAll) : array { $data = []; - while (($row = $fetchMode($stmt)) !== false) { + + foreach ($fetchAll($stmt) as $row) { $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row; } @@ -283,11 +300,11 @@ private function hydrateStmt(ResultStatement $stmt, callable $fetchMode) : array /** * @return array */ - private function hydrateStmtIterator(ResultStatement $stmt, callable $fetchMode) : array + private function hydrateViaIteration(ResultStatement $stmt, callable $fetch) : array { $data = []; - while (($row = $fetchMode($stmt)) !== false) { + while (($row = $fetch($stmt)) !== false) { $data[] = is_array($row) ? array_change_key_case($row, CASE_LOWER) : $row; }