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
|
<?php
namespace Illuminate\Tests\Cache;
use DateInterval;
use DateTime;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\RedisTaggedCache;
use Illuminate\Cache\TagSet;
use Illuminate\Contracts\Cache\Store;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;
class CacheTaggedCacheTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
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 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'));
}
public function testRedisCacheTagsPushForeverKeysCorrectly()
{
$store = m::mock(Store::class);
$tagSet = m::mock(TagSet::class, [$store, ['foo', 'bar']]);
$tagSet->shouldReceive('getNamespace')->andReturn('foo|bar');
$tagSet->shouldReceive('getNames')->andReturn(['foo', 'bar']);
$redis = new RedisTaggedCache($store, $tagSet);
$store->shouldReceive('getPrefix')->andReturn('prefix:');
$store->shouldReceive('connection')->andReturn($conn = m::mock(stdClass::class));
$conn->shouldReceive('sadd')->once()->with('prefix:foo:forever_ref', 'prefix:'.sha1('foo|bar').':key1');
$conn->shouldReceive('sadd')->once()->with('prefix:bar:forever_ref', 'prefix:'.sha1('foo|bar').':key1');
$store->shouldReceive('forever')->with(sha1('foo|bar').':key1', 'key1:value');
$redis->forever('key1', 'key1:value');
}
public function testRedisCacheTagsPushStandardKeysCorrectly()
{
$store = m::mock(Store::class);
$tagSet = m::mock(TagSet::class, [$store, ['foo', 'bar']]);
$tagSet->shouldReceive('getNamespace')->andReturn('foo|bar');
$tagSet->shouldReceive('getNames')->andReturn(['foo', 'bar']);
$redis = new RedisTaggedCache($store, $tagSet);
$store->shouldReceive('getPrefix')->andReturn('prefix:');
$store->shouldReceive('connection')->andReturn($conn = m::mock(stdClass::class));
$conn->shouldReceive('sadd')->once()->with('prefix:foo:standard_ref', 'prefix:'.sha1('foo|bar').':key1');
$conn->shouldReceive('sadd')->once()->with('prefix:bar:standard_ref', 'prefix:'.sha1('foo|bar').':key1');
$store->shouldReceive('push')->with(sha1('foo|bar').':key1', 'key1:value');
$store->shouldReceive('put')->andReturn(true);
$redis->put('key1', 'key1:value', 60);
}
public function testRedisCacheTagsPushForeverKeysCorrectlyWithNullTTL()
{
$store = m::mock(Store::class);
$tagSet = m::mock(TagSet::class, [$store, ['foo', 'bar']]);
$tagSet->shouldReceive('getNamespace')->andReturn('foo|bar');
$tagSet->shouldReceive('getNames')->andReturn(['foo', 'bar']);
$redis = new RedisTaggedCache($store, $tagSet);
$store->shouldReceive('getPrefix')->andReturn('prefix:');
$store->shouldReceive('connection')->andReturn($conn = m::mock(stdClass::class));
$conn->shouldReceive('sadd')->once()->with('prefix:foo:forever_ref', 'prefix:'.sha1('foo|bar').':key1');
$conn->shouldReceive('sadd')->once()->with('prefix:bar:forever_ref', 'prefix:'.sha1('foo|bar').':key1');
$store->shouldReceive('forever')->with(sha1('foo|bar').':key1', 'key1:value');
$redis->put('key1', 'key1:value');
}
public function testRedisCacheTagsCanBeFlushed()
{
$store = m::mock(Store::class);
$tagSet = m::mock(TagSet::class, [$store, ['foo', 'bar']]);
$tagSet->shouldReceive('getNamespace')->andReturn('foo|bar');
$redis = new RedisTaggedCache($store, $tagSet);
$store->shouldReceive('getPrefix')->andReturn('prefix:');
$store->shouldReceive('connection')->andReturn($conn = m::mock(stdClass::class));
// Forever tag keys
$conn->shouldReceive('smembers')->once()->with('prefix:foo:forever_ref')->andReturn(['key1', 'key2']);
$conn->shouldReceive('smembers')->once()->with('prefix:bar:forever_ref')->andReturn(['key3']);
$conn->shouldReceive('del')->once()->with('key1', 'key2');
$conn->shouldReceive('del')->once()->with('key3');
$conn->shouldReceive('del')->once()->with('prefix:foo:forever_ref');
$conn->shouldReceive('del')->once()->with('prefix:bar:forever_ref');
// Standard tag keys
$conn->shouldReceive('smembers')->once()->with('prefix:foo:standard_ref')->andReturn(['key4', 'key5']);
$conn->shouldReceive('smembers')->once()->with('prefix:bar:standard_ref')->andReturn(['key6']);
$conn->shouldReceive('del')->once()->with('key4', 'key5');
$conn->shouldReceive('del')->once()->with('key6');
$conn->shouldReceive('del')->once()->with('prefix:foo:standard_ref');
$conn->shouldReceive('del')->once()->with('prefix:bar:standard_ref');
$tagSet->shouldReceive('reset')->once();
$redis->flush();
}
}
|