1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace think\tests; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Mockery as m; |
7
|
|
|
use Mockery\MockInterface; |
8
|
|
|
use org\bovigo\vfs\vfsStream; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use think\App; |
11
|
|
|
use think\Cache; |
12
|
|
|
use think\Config; |
13
|
|
|
use think\Container; |
14
|
|
|
|
15
|
|
|
class CacheTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var App|MockInterface */ |
|
|
|
|
18
|
|
|
protected $app; |
19
|
|
|
|
20
|
|
|
/** @var Cache|MockInterface */ |
|
|
|
|
21
|
|
|
protected $cache; |
22
|
|
|
|
23
|
|
|
/** @var Config|MockInterface */ |
|
|
|
|
24
|
|
|
protected $config; |
25
|
|
|
|
26
|
|
|
protected function tearDown(): void |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
m::close(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function setUp() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->app = m::mock(App::class)->makePartial(); |
34
|
|
|
|
35
|
|
|
Container::setInstance($this->app); |
36
|
|
|
$this->app->shouldReceive('make')->with(App::class)->andReturn($this->app); |
37
|
|
|
$this->config = m::mock(Config::class)->makePartial(); |
38
|
|
|
$this->app->shouldReceive('get')->with('config')->andReturn($this->config); |
39
|
|
|
|
40
|
|
|
$this->cache = new Cache($this->app); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetConfig() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$config = [ |
46
|
|
|
'default' => 'file', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
$this->config->shouldReceive('get')->with('cache')->andReturn($config); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$this->assertEquals($config, $this->cache->getConfig()); |
52
|
|
|
|
53
|
|
|
$this->expectException(InvalidArgumentException::class); |
54
|
|
|
$this->cache->getStoreConfig('foo'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testCacheManagerInstances() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->config->shouldReceive('get')->with("cache.stores.single", null)->andReturn(['type' => 'file']); |
60
|
|
|
|
61
|
|
|
$channel1 = $this->cache->store('single'); |
62
|
|
|
$channel2 = $this->cache->store('single'); |
63
|
|
|
|
64
|
|
|
$this->assertSame($channel1, $channel2); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testFileCache() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$root = vfsStream::setup(); |
70
|
|
|
|
71
|
|
|
$this->config->shouldReceive('get')->with("cache.default", null)->andReturn('file'); |
72
|
|
|
|
73
|
|
|
$this->config->shouldReceive('get')->with("cache.stores.file", null)->andReturn(['type' => 'file', 'path' => $root->url()]); |
74
|
|
|
|
75
|
|
|
$this->cache->set('foo', 5); |
76
|
|
|
$this->cache->inc('foo'); |
|
|
|
|
77
|
|
|
$this->assertEquals(6, $this->cache->get('foo')); |
78
|
|
|
$this->cache->dec('foo', 2); |
|
|
|
|
79
|
|
|
$this->assertEquals(4, $this->cache->get('foo')); |
80
|
|
|
|
81
|
|
|
$this->cache->set('bar', true); |
82
|
|
|
$this->assertTrue($this->cache->get('bar')); |
83
|
|
|
|
84
|
|
|
$this->cache->set('baz', null); |
85
|
|
|
$this->assertNull($this->cache->get('baz')); |
86
|
|
|
|
87
|
|
|
$this->assertTrue($this->cache->has('baz')); |
88
|
|
|
$this->cache->delete('baz'); |
89
|
|
|
$this->assertFalse($this->cache->has('baz')); |
90
|
|
|
$this->assertNull($this->cache->get('baz')); |
91
|
|
|
$this->assertFalse($this->cache->get('baz', false)); |
92
|
|
|
|
93
|
|
|
$this->assertTrue($root->hasChildren()); |
94
|
|
|
$this->cache->clear(); |
95
|
|
|
$this->assertFalse($root->hasChildren()); |
96
|
|
|
|
97
|
|
|
//tags |
98
|
|
|
$this->cache->tag('foo')->set('bar', 'foobar'); |
|
|
|
|
99
|
|
|
$this->assertEquals('foobar', $this->cache->get('bar')); |
100
|
|
|
$this->cache->tag('foo')->clear(); |
101
|
|
|
$this->assertFalse($this->cache->has('bar')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testRedisCache() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->config->shouldReceive('get')->with("cache.default", null)->andReturn('redis'); |
107
|
|
|
$this->config->shouldReceive('get')->with("cache.stores.redis", null)->andReturn(['type' => 'redis']); |
108
|
|
|
|
109
|
|
|
$redis = m::mock('overload:\Predis\Client'); |
110
|
|
|
|
111
|
|
|
$redis->shouldReceive("set")->once()->with('foo', 5)->andReturnTrue(); |
|
|
|
|
112
|
|
|
$redis->shouldReceive("incrby")->once()->with('foo', 1)->andReturnTrue(); |
113
|
|
|
$redis->shouldReceive("decrby")->once()->with('foo', 2)->andReturnTrue(); |
114
|
|
|
$redis->shouldReceive("get")->once()->with('foo')->andReturn(6); |
115
|
|
|
$redis->shouldReceive("get")->once()->with('foo')->andReturn(4); |
116
|
|
|
$redis->shouldReceive("set")->once()->with('bar', \Opis\Closure\serialize(true))->andReturnTrue(); |
117
|
|
|
$redis->shouldReceive("set")->once()->with('baz', \Opis\Closure\serialize(null))->andReturnTrue(); |
118
|
|
|
$redis->shouldReceive("del")->once()->with('baz')->andReturnTrue(); |
119
|
|
|
$redis->shouldReceive("flushDB")->once()->andReturnTrue(); |
120
|
|
|
$redis->shouldReceive("set")->once()->with('bar', \Opis\Closure\serialize('foobar'))->andReturnTrue(); |
121
|
|
|
$redis->shouldReceive("sAdd")->once()->with('tag:' . md5('foo'), 'bar')->andReturnTrue(); |
122
|
|
|
$redis->shouldReceive("sMembers")->once()->with('tag:' . md5('foo'))->andReturn(['bar']); |
123
|
|
|
$redis->shouldReceive("del")->once()->with(['bar'])->andReturnTrue(); |
124
|
|
|
$redis->shouldReceive("del")->once()->with('tag:' . md5('foo'))->andReturnTrue(); |
125
|
|
|
|
126
|
|
|
$this->cache->set('foo', 5); |
127
|
|
|
$this->cache->inc('foo'); |
128
|
|
|
$this->assertEquals(6, $this->cache->get('foo')); |
129
|
|
|
$this->cache->dec('foo', 2); |
130
|
|
|
$this->assertEquals(4, $this->cache->get('foo')); |
131
|
|
|
|
132
|
|
|
$this->cache->set('bar', true); |
133
|
|
|
$this->cache->set('baz', null); |
134
|
|
|
$this->cache->delete('baz'); |
135
|
|
|
$this->cache->clear(); |
136
|
|
|
|
137
|
|
|
//tags |
138
|
|
|
$this->cache->tag('foo')->set('bar', 'foobar'); |
139
|
|
|
$this->cache->tag('foo')->clear(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|