8000 [Cache][Contracts] We must save the item or the trait does not have a… · symfony/symfony@06cd8dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 06cd8dc

Browse files
Nyholmnicolas-grekas
authored andcommitted
[Cache][Contracts] We must save the item or the trait does not have any effect
1 parent 8ab7077 commit 06cd8dc

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

src/Symfony/Contracts/Cache/GetForCacheItemPoolTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function get(string $key, callable $callback, float $beta = null)
3636
if (INF === $beta || !$item->isHit()) {
3737
$value = $callback($item);
3838
$item->set($value);
39+
$this->save($item);
3940
}
4041

4142
return $item->get();
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Contracts\Tests\Cache;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Cache\CacheItemInterface;
16+
use Symfony\Contracts\Cache\GetForCacheItemPoolTrait;
17+
18+
/**
19+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
*/
21+
class GetForCacheItemPoolTraitTest extends TestCase
22+
{
23+
public function testSave()
24+
{
25+
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
26+
$item->method('isHit')
27+
->willReturn(false);
28+
29+
$item->expects($this->once())
30+
->method('set')
31+
->with('computed data');
32+
33+
$cache = $this->getMockBuilder(ClassUsingTrait::class)
34+
->setMethods(array('getItem', 'save'))
35+
->getMock();
36+
$cache->expects($this->once())
37+
->method('getItem')
38+
->with('key')
39+
->willReturn($item);
40+
$cache->expects($this->once())
41+
->method('save');
42+
43+
$callback = function (CacheItemInterface $item) {
44+
return 'computed data';
45+
};
46+
47+
$cache->get('key', $callback);
48+
}
49+
50+
public function testNoCallbackCallOnHit()
51+
{
52+
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
53+
$item->method('isHit')
54+
->willReturn(true);
55+
56+
$item->expects($this->never())
57+
->method('set');
58+
59+
$cache = $this->getMockBuilder(ClassUsingTrait::class)
60+
->setMethods(array('getItem', 'save'))
61+
->getMock();
62+
63+
$cache->expects($this->once())
64+
->method('getItem')
65+
->with('key')
66+
->willReturn($item);
67+
$cache->expects($this->never())
68+
->method('save');
69+
70+
$callback = function (CacheItemInterface $item) {
71+
$this->assertTrue(false, 'This code should never be reached');
72+
};
73+
74+
$cache->get('key', $callback);
75+
}
76+
77+
public function testRecomputeOnBetaInf()
78+
{
79+
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
80+
$item->method('isHit')
81+
// We want to recompute even if it is a hit
82+
->willReturn(true);
83+
84+
$item->expects($this->once())
85+
->method('set')
86+
->with('computed data');
87+
88+
$cache = $this->getMockBuilder(ClassUsingTrait::class)
89+
->setMethods(array('getItem', 'save'))
90+
->getMock();
91+
92+
$cache->expects($this->once())
93+
->method('getItem')
94+
->with('key')
95+
->willReturn($item);
96+
$cache->expects($this->once())
97+
->method('save');
98+
99+
$callback = function (CacheItemInterface $item) {
100+
return 'computed data';
101+
};
102+
103+
$cache->get('key', $callback, INF);
104+
}
105+
106+
public function testExceptionOnNegativeBeta()
107+
{
108+
$cache = $this->getMockBuilder(ClassUsingTrait::class)
109+
->setMethods(array('getItem', 'save'))
110+
->getMock();
111+
112+
$callback = function (CacheItemInterface $item) {
113+
return 'computed data';
114+
};
115+
116+
$this->expectException(\InvalidArgumentException::class);
117+
$cache->get('key', $callback, -2);
118+
}
119+
}
120+
121+
class ClassUsingTrait
122+
{
123+
use GetForCacheItemPoolTrait;
124+
125+
public function getItem($key)
126+
{
127+
}
128+
129+
public function save(CacheItemInterface $item)
130+
{
131+
}
132+
}

src/Symfony/Contracts/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"php": "^7.1.3"
2020
},
2121
"require-dev": {
22+
"psr/cache": "^1.0",
2223
"psr/container": "^1.0"
2324
},
2425
"suggest": {

0 commit comments

Comments
 (0)
0