Completed
Branch 6.0 (d30585)
by yun
04:17
created

CacheTest::testRedisCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 30
nc 2
nop 0
dl 0
loc 39
rs 9.44
c 3
b 0
f 0
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
        //multiple
104
        $this->cache->setMultiple(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']]);
105
        $this->assertEquals(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']], $this->cache->getMultiple(['foo', 'foobar']));
106
        $this->assertTrue($this->cache->deleteMultiple(['foo', 'foobar']));
107
    }
108
109
    public function testRedisCache()
110
    {
111
        if (extension_loaded('redis')) {
112
            return;
113
        }
114
        $this->config->shouldReceive('get')->with("cache.default", null)->andReturn('redis');
115
        $this->config->shouldReceive('get')->with("cache.stores.redis", null)->andReturn(['type' => 'redis']);
116
117
        $redis = m::mock('overload:\Predis\Client');
118
119
        $redis->shouldReceive("set")->once()->with('foo', 5)->andReturnTrue();
120
        $redis->shouldReceive("incrby")->once()->with('foo', 1)->andReturnTrue();
121
        $redis->shouldReceive("decrby")->once()->with('foo', 2)->andReturnTrue();
122
        $redis->shouldReceive("get")->once()->with('foo')->andReturn('6');
123
        $redis->shouldReceive("get")->once()->with('foo')->andReturn('4');
124
        $redis->shouldReceive("set")->once()->with('bar', serialize(true))->andReturnTrue();
125
        $redis->shouldReceive("set")->once()->with('baz', serialize(null))->andReturnTrue();
126
        $redis->shouldReceive("del")->once()->with('baz')->andReturnTrue();
127
        $redis->shouldReceive("flushDB")->once()->andReturnTrue();
128
        $redis->shouldReceive("set")->once()->with('bar', serialize('foobar'))->andReturnTrue();
129
        $redis->shouldReceive("sAdd")->once()->with('tag:' . md5('foo'), 'bar')->andReturnTrue();
130
        $redis->shouldReceive("sMembers")->once()->with('tag:' . md5('foo'))->andReturn(['bar']);
131
        $redis->shouldReceive("del")->once()->with(['bar'])->andReturnTrue();
132
        $redis->shouldReceive("del")->once()->with('tag:' . md5('foo'))->andReturnTrue();
133
134
        $this->cache->set('foo', 5);
135
        $this->cache->inc('foo');
136
        $this->assertEquals(6, $this->cache->get('foo'));
137
        $this->cache->dec('foo', 2);
138
        $this->assertEquals(4, $this->cache->get('foo'));
139
140
        $this->cache->set('bar', true);
141
        $this->cache->set('baz', null);
142
        $this->cache->delete('baz');
143
        $this->cache->clear();
144
145
        //tags
146
        $this->cache->tag('foo')->set('bar', 'foobar');
147
        $this->cache->tag('foo')->clear();
148
    }
149
}
150