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

ResultCacheStatement::fetchAllAssociative does not store results in cache #4414

Merged
merged 1 commit into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
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
16 changes: 6 additions & 10 deletions lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ public function fetchAllNumeric(): array
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}

$this->store($data);
$this->data = $data;

$this->saveToCache();

return array_map('array_values', $data);
}
Expand All @@ -258,7 +260,9 @@ public function fetchAllAssociative(): array
$data = $this->statement->fetchAll(FetchMode::ASSOCIATIVE);
}

$this->store($data);
$this->data = $data;

$this->saveToCache();

return $data;
}
Expand Down Expand Up @@ -322,14 +326,6 @@ private function doFetch()
return false;
}

/**
* @param array<int,array<string,mixed>> $data
*/
private function store(array $data): void
{
$this->data = $data;
}

private function saveToCache(): void
{
if ($this->data === null) {
Expand Down
39 changes: 37 additions & 2 deletions tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Cache\ResultCacheStatement;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Logging\DebugStack;
Expand Down Expand Up @@ -212,7 +213,10 @@ public function testDontFinishNoCache(): void
self::assertCount(2, $this->sqlLogger->queries);
}

public function testFetchAllSavesCache(): void
/**
* @dataProvider fetchAllProvider
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
*/
public function testFetchingAllRowsSavesCache(callable $fetchAll): void
{
$layerCache = new ArrayCache();

Expand All @@ -222,11 +226,42 @@ public function testFetchAllSavesCache(): void
[],
new QueryCacheProfile(0, 'testcachekey', $layerCache)
);
$stmt->fetchAll();

$fetchAll($stmt);

self::assertCount(1, $layerCache->fetch('testcachekey'));
}

/**
* @return iterable<string,list<mixed>>
*/
public static function fetchAllProvider(): iterable
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
{
yield 'fetchAll' => [
static function (ResultCacheStatement $statement): void {
$statement->fetchAll();
},
];

yield 'fetchAllAssociative' => [
static function (ResultCacheStatement $statement): void {
$statement->fetchAllAssociative();
},
];

yield 'fetchAllNumeric' => [
static function (ResultCacheStatement $statement): void {
$statement->fetchAllNumeric();
},
];

yield 'fetchFirstColumn' => [
static function (ResultCacheStatement $result): void {
$result->fetchFirstColumn();
},
];
}

public function testFetchAllColumn(): void
{
$query = $this->connection->getDatabasePlatform()
Expand Down