Completed
Push — 6.0 ( a4ef3c...64a49e )
by yun
04:55
created

CacheTest::testCacheManagerInstances()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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 */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
18
    protected $app;
19
20
    /** @var Cache|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
21
    protected $cache;
22
23
    /** @var Config|MockInterface */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
24
    protected $config;
25
26
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
27
    {
28
        m::close();
29
    }
30
31
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
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);
0 ignored issues
show
Bug introduced by
$this->app of type Mockery\Mock is incompatible with the type think\App expected by parameter $app of think\Cache::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $this->cache = new Cache(/** @scrutinizer ignore-type */ $this->app);
Loading history...
41
    }
42
43
    public function testGetConfig()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testGetConfig()
Loading history...
44
    {
45
        $config = [
46
            'default' => 'file',
47
        ];
48
49
        $this->config->shouldReceive('get')->with('cache')->andReturn($config);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on think\Config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $this->config->/** @scrutinizer ignore-call */ 
50
                       shouldReceive('get')->with('cache')->andReturn($config);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testCacheManagerInstances()
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testFileCache()
Loading history...
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');
0 ignored issues
show
Bug introduced by
The method inc() does not exist on think\Cache. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $this->cache->/** @scrutinizer ignore-call */ 
77
                      inc('foo');
Loading history...
77
        $this->assertEquals(6, $this->cache->get('foo'));
78
        $this->cache->dec('foo', 2);
0 ignored issues
show
Bug introduced by
The method dec() does not exist on think\Cache. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        $this->cache->/** @scrutinizer ignore-call */ 
79
                      dec('foo', 2);
Loading history...
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');
0 ignored issues
show
Bug introduced by
The method tag() does not exist on think\Cache. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        $this->cache->/** @scrutinizer ignore-call */ 
99
                      tag('foo')->set('bar', 'foobar');
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testRedisCache()
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method andReturnTrue() does not exist on Mockery\ExpectationInterface. Did you maybe mean andReturn()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        $redis->shouldReceive("set")->once()->with('foo', 5)->/** @scrutinizer ignore-call */ andReturnTrue();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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