Skip to content

Commit

Permalink
bug #29794 Always pass $key to NullAdapter->createCacheItem (TysonAndre)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.2 branch.

Discussion
----------

Always pass $key to NullAdapter->createCacheItem

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes (build failure seems unrelated)
| Fixed tickets |
| License       | MIT
| Doc PR        |

Previously, if this were called, it would throw an ArgumentCountError.
I'm assuming existing code always checks hasItem, so this bug hasn't impacted many people.
This was noticed via static analysis.

The get() method was added to NullAdapter in symfony 4.2

Commits
-------

1976d29 Always pass $key to NullAdapter->createCacheItem
  • Loading branch information
nicolas-grekas committed Jan 25, 2019
2 parents f7b9bb2 + 1976d29 commit 968e736
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/NullAdapter.php
Expand Up @@ -42,7 +42,7 @@ function ($key) {
*/
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
{
return $callback(($this->createCacheItem)());
return $callback(($this->createCacheItem)($key));
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/NullAdapterTest.php
Expand Up @@ -34,6 +34,19 @@ public function testGetItem()
$this->assertNull($item->get(), "Item's value must be null when isHit is false.");
}

public function testGet()
{
$adapter = $this->createCachePool();

$fetched = [];
$item = $adapter->get('myKey', function ($item) use (&$fetched) { $fetched[] = $item; });
$this->assertCount(1, $fetched);
$item = $fetched[0];
$this->assertFalse($item->isHit());
$this->assertNull($item->get(), "Item's value must be null when isHit is false.");
$this->assertSame('myKey', $item->getKey());
}

public function testHasItem()
{
$this->assertFalse($this->createCachePool()->hasItem('key'));
Expand Down

0 comments on commit 968e736

Please sign in to comment.