1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Contracts\Tests\Cache;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Contracts\Cache\CacheTrait;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class CacheTraitTest extends TestCase
{
public function testSave()
{
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
$item->method('set')
->willReturn($item);
$item->method('isHit')
->willReturn(false);
$item->expects($this->once())
->method('set')
->with('computed data');
$cache = $this->getMockBuilder(TestPool::class)
->setMethods(['getItem', 'save'])
->getMock();
$cache->expects($this->once())
->method('getItem')
->with('key')
->willReturn($item);
$cache->expects($this->once())
->method('save');
$callback = function (CacheItemInterface $item) {
return 'computed data';
};
$cache->get('key', $callback);
}
public function testNoCallbackCallOnHit()
{
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
$item->method('isHit')
->willReturn(true);
$item->expects($this->never())
->method('set');
$cache = $this->getMockBuilder(TestPool::class)
->setMethods(['getItem', 'save'])
->getMock();
$cache->expects($this->once())
->method('getItem')
->with('key')
->willReturn($item);
$cache->expects($this->never())
->method('save');
$callback = function (CacheItemInterface $item) {
$this->assertTrue(false, 'This code should never be reached');
};
$cache->get('key', $callback);
}
public function testRecomputeOnBetaInf()
{
$item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
$item->method('set')
->willReturn($item);
$item->method('isHit')
// We want to recompute even if it is a hit
->willReturn(true);
$item->expects($this->once())
->method('set')
->with('computed data');
$cache = $this->getMockBuilder(TestPool::class)
->setMethods(['getItem', 'save'])
->getMock();
$cache->expects($this->once())
->method('getItem')
->with('key')
->willReturn($item);
$cache->expects($this->once())
->method('save');
$callback = function (CacheItemInterface $item) {
return 'computed data';
};
$cache->get('key', $callback, \INF);
}
public function testExceptionOnNegativeBeta()
{
$cache = $this->getMockBuilder(TestPool::class)
->setMethods(['getItem', 'save'])
->getMock();
$callback = function (CacheItemInterface $item) {
return 'computed data';
};
$this->expectException(\InvalidArgumentException::class);
$cache->get('key', $callback, -2);
}
}
class TestPool implements CacheItemPoolInterface
{
use CacheTrait;
public function hasItem($key): bool
{
}
public function deleteItem($key): bool
{
}
public function deleteItems(array $keys = []): bool
{
}
public function getItem($key): CacheItemInterface
{
}
public function getItems(array $key = []): iterable
{
}
public function saveDeferred(CacheItemInterface $item): bool
{
}
public function save(CacheItemInterface $item): bool
{
}
public function commit(): bool
{
}
public function clear(): bool
{
}
}
|