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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
<?php
namespace Illuminate\Tests\Cache;
use DateInterval;
use DateTime;
use Illuminate\Cache\ArrayStore;
use PHPUnit\Framework\TestCase;
class CacheTaggedCacheTest extends TestCase
{
public function testCacheCanBeSavedWithMultipleTags()
{
$store = new ArrayStore;
$tags = ['bop', 'zap'];
$store->tags($tags)->put('foo', 'bar', 10);
$this->assertSame('bar', $store->tags($tags)->get('foo'));
}
public function testCacheCanBeSetWithDatetimeArgument()
{
$store = new ArrayStore;
$tags = ['bop', 'zap'];
$duration = new DateTime;
$duration->add(new DateInterval('PT10M'));
$store->tags($tags)->put('foo', 'bar', $duration);
$this->assertSame('bar', $store->tags($tags)->get('foo'));
}
public function testCacheSavedWithMultipleTagsCanBeFlushed()
{
$store = new ArrayStore;
$tags1 = ['bop', 'zap'];
$store->tags($tags1)->put('foo', 'bar', 10);
$tags2 = ['bam', 'pow'];
$store->tags($tags2)->put('foo', 'bar', 10);
$store->tags('zap')->flush();
$this->assertNull($store->tags($tags1)->get('foo'));
$this->assertSame('bar', $store->tags($tags2)->get('foo'));
}
public function testTagsWithStringArgument()
{
$store = new ArrayStore;
$store->tags('bop')->put('foo', 'bar', 10);
$this->assertSame('bar', $store->tags('bop')->get('foo'));
}
public function testWithIncrement()
{
$store = new ArrayStore;
$taggableStore = $store->tags('bop');
$taggableStore->put('foo', 5, 10);
$value = $taggableStore->increment('foo');
$this->assertSame(6, $value);
$value = $taggableStore->increment('foo');
$this->assertSame(7, $value);
$value = $taggableStore->increment('foo', 3);
$this->assertSame(10, $value);
$value = $taggableStore->increment('foo', -2);
$this->assertSame(8, $value);
$value = $taggableStore->increment('x');
$this->assertSame(1, $value);
$value = $taggableStore->increment('y', 10);
$this->assertSame(10, $value);
}
public function testWithDecrement()
{
$store = new ArrayStore;
$taggableStore = $store->tags('bop');
$taggableStore->put('foo', 50, 10);
$value = $taggableStore->decrement('foo');
$this->assertSame(49, $value);
$value = $taggableStore->decrement('foo');
$this->assertSame(48, $value);
$value = $taggableStore->decrement('foo', 3);
$this->assertSame(45, $value);
$value = $taggableStore->decrement('foo', -2);
$this->assertSame(47, $value);
$value = $taggableStore->decrement('x');
$this->assertSame(-1, $value);
$value = $taggableStore->decrement('y', 10);
$this->assertSame(-10, $value);
}
public function testMany()
{
$store = $this->getTestCacheStoreWithTagValues();
$values = $store->tags(['fruit'])->many(['a', 'e', 'b', 'd', 'c']);
$this->assertSame([
'a' => 'apple',
'e' => null,
'b' => 'banana',
'd' => null,
'c' => 'orange',
], $values);
}
public function testManyWithDefaultValues()
{
$store = $this->getTestCacheStoreWithTagValues();
$values = $store->tags(['fruit'])->many([
'a' => 147,
'e' => 547,
'b' => 'hello world!',
'x' => 'hello world!',
'd',
'c',
]);
$this->assertSame([
'a' => 'apple',
'e' => 547,
'b' => 'banana',
'x' => 'hello world!',
'd' => null,
'c' => 'orange',
], $values);
}
public function testGetMultiple()
{
$store = $this->getTestCacheStoreWithTagValues();
$values = $store->tags(['fruit'])->getMultiple(['a', 'e', 'b', 'd', 'c']);
$this->assertSame([
'a' => 'apple',
'e' => null,
'b' => 'banana',
'd' => null,
'c' => 'orange',
], $values);
$values = $store->tags(['fruit', 'color'])->getMultiple(['a', 'e', 'b', 'd', 'c']);
$this->assertSame([
'a' => 'red',
'e' => 'blue',
'b' => null,
'd' => 'yellow',
'c' => null,
], $values);
}
public function testGetMultipleWithDefaultValue()
{
$store = $this->getTestCacheStoreWithTagValues();
$values = $store->tags(['fruit', 'color'])->getMultiple(['a', 'e', 'b', 'd', 'c'], 547);
$this->assertSame([
'a' => 'red',
'e' => 'blue',
'b' => 547,
'd' => 'yellow',
'c' => 547,
], $values);
}
public function testTagsWithIncrementCanBeFlushed()
{
$store = new ArrayStore;
$store->tags('bop')->increment('foo', 5);
$this->assertEquals(5, $store->tags('bop')->get('foo'));
$store->tags('bop')->flush();
$this->assertNull($store->tags('bop')->get('foo'));
}
public function testTagsWithDecrementCanBeFlushed()
{
$store = new ArrayStore;
$store->tags('bop')->decrement('foo', 5);
$this->assertEquals(-5, $store->tags('bop')->get('foo'));
$store->tags('bop')->flush();
$this->assertNull($store->tags('bop')->get('foo'));
}
public function testTagsCacheForever()
{
$store = new ArrayStore;
$tags = ['bop', 'zap'];
$store->tags($tags)->forever('foo', 'bar');
$this->assertSame('bar', $store->tags($tags)->get('foo'));
}
private function getTestCacheStoreWithTagValues(): ArrayStore
{
$store = new ArrayStore;
$tags = ['fruit'];
$store->tags($tags)->put('a', 'apple', 10);
$store->tags($tags)->put('b', 'banana', 10);
$store->tags($tags)->put('c', 'orange', 10);
$tags = ['fruit', 'color'];
$store->tags($tags)->putMany([
'a' => 'red',
'd' => 'yellow',
'e' => 'blue',
], 10);
$tags = ['sizes', 'shirt'];
$store->tags($tags)->putMany([
'a' => 'small',
'b' => 'medium',
'c' => 'large',
], 10);
return $store;
}
}
|