-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathCacheRedisStoreTest.php
executable file
·155 lines (135 loc) · 5.99 KB
/
CacheRedisStoreTest.php
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
<?php
namespace Illuminate\Tests\Cache;
use Illuminate\Cache\RedisStore;
use Illuminate\Contracts\Redis\Factory;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class CacheRedisStoreTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testGetReturnsNullWhenNotFound()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('get')->once()->with('prefix:foo')->andReturn(null);
$this->assertNull($redis->get('foo'));
}
public function testRedisValueIsReturned()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('get')->once()->with('prefix:foo')->andReturn(serialize('foo'));
$this->assertSame('foo', $redis->get('foo'));
}
public function testRedisMultipleValuesAreReturned()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('mget')->once()->with(['prefix:foo', 'prefix:fizz', 'prefix:norf', 'prefix:null'])
->andReturn([
serialize('bar'),
serialize('buzz'),
serialize('quz'),
null,
]);
$results = $redis->many(['foo', 'fizz', 'norf', 'null']);
$this->assertSame('bar', $results['foo']);
$this->assertSame('buzz', $results['fizz']);
$this->assertSame('quz', $results['norf']);
$this->assertNull($results['null']);
}
public function testRedisValueIsReturnedForNumerics()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('get')->once()->with('prefix:foo')->andReturn(1);
$this->assertEquals(1, $redis->get('foo'));
}
public function testSetMethodProperlyCallsRedis()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:foo', 60, serialize('foo'))->andReturn('OK');
$result = $redis->put('foo', 'foo', 60);
$this->assertTrue($result);
}
public function testSetMultipleMethodProperlyCallsRedis()
{
$redis = $this->getRedis();
/** @var m\MockInterface $connection */
$connection = $redis->getRedis();
$connection->shouldReceive('connection')->with('default')->andReturn($redis->getRedis());
$connection->shouldReceive('multi')->once();
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:foo', 60, serialize('bar'))->andReturn('OK');
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:baz', 60, serialize('qux'))->andReturn('OK');
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:bar', 60, serialize('norf'))->andReturn('OK');
$connection->shouldReceive('exec')->once();
$result = $redis->putMany([
'foo' => 'bar',
'baz' => 'qux',
'bar' => 'norf',
], 60);
$this->assertTrue($result);
}
public function testSetMethodProperlyCallsRedisForNumerics()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('setex')->once()->with('prefix:foo', 60, 1);
$result = $redis->put('foo', 1, 60);
$this->assertFalse($result);
}
public function testIncrementMethodProperlyCallsRedis()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('incrby')->once()->with('prefix:foo', 5);
$redis->increment('foo', 5);
}
public function testDecrementMethodProperlyCallsRedis()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('decrby')->once()->with('prefix:foo', 5);
$redis->decrement('foo', 5);
}
public function testStoreItemForeverProperlyCallsRedis()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('set')->once()->with('prefix:foo', serialize('foo'))->andReturn('OK');
$result = $redis->forever('foo', 'foo', 60);
$this->assertTrue($result);
}
public function testForgetMethodProperlyCallsRedis()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('del')->once()->with('prefix:foo');
$redis->forget('foo');
}
public function testFlushesCached()
{
$redis = $this->getRedis();
$redis->getRedis()->shouldReceive('connection')->once()->with('default')->andReturn($redis->getRedis());
$redis->getRedis()->shouldReceive('flushdb')->once()->andReturn('ok');
$result = $redis->flush();
$this->assertTrue($result);
}
public function testGetAndSetPrefix()
{
$redis = $this->getRedis();
$this->assertSame('prefix:', $redis->getPrefix());
$redis->setPrefix('foo');
$this->assertSame('foo', $redis->getPrefix());
$redis->setPrefix(null);
$this->assertEmpty($redis->getPrefix());
}
protected function getRedis()
{
return new RedisStore(m::mock(Factory::class), 'prefix:');
}
}