forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheRedisStoreTest.php
More file actions
executable file
·136 lines (120 loc) · 5.19 KB
/
CacheRedisStoreTest.php
File metadata and controls
executable file
·136 lines (120 loc) · 5.19 KB
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
<?php
use Mockery as m;
class CacheRedisStoreTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
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->assertEquals('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'])
->andReturn([
serialize('bar'),
serialize('buzz'),
serialize('quz'),
]);
$this->assertEquals([
'foo' => 'bar',
'fizz' => 'buzz',
'norf' => 'quz',
], $redis->many([
'foo', 'fizz', 'norf',
]));
}
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 * 60, serialize('foo'));
$redis->put('foo', 'foo', 60);
}
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')->with('prefix:foo', 60 * 60, serialize('bar'));
$redis->getRedis()->shouldReceive('setex')->with('prefix:baz', 60 * 60, serialize('qux'));
$redis->getRedis()->shouldReceive('setex')->with('prefix:bar', 60 * 60, serialize('norf'));
$connection->shouldReceive('exec')->once();
$redis->putMany([
'foo' => 'bar',
'baz' => 'qux',
'bar' => 'norf',
], 60);
}
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 * 60, 1);
$redis->put('foo', 1, 60);
}
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'));
$redis->forever('foo', 'foo', 60);
}
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 testGetAndSetPrefix()
{
$redis = $this->getRedis();
$this->assertEquals('prefix:', $redis->getPrefix());
$redis->setPrefix('foo');
$this->assertEquals('foo:', $redis->getPrefix());
$redis->setPrefix(null);
$this->assertEmpty($redis->getPrefix());
}
protected function getRedis()
{
return new Illuminate\Cache\RedisStore(m::mock('Illuminate\Redis\Database'), 'prefix');
}
}