forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheDatabaseStoreTest.php
More file actions
executable file
·197 lines (170 loc) · 9.71 KB
/
CacheDatabaseStoreTest.php
File metadata and controls
executable file
·197 lines (170 loc) · 9.71 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
use Mockery as m;
use Illuminate\Cache\DatabaseStore;
class CacheDatabaseStoreTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}
public function testNullIsReturnedWhenItemNotFound()
{
$store = $this->getStore();
$table = m::mock('StdClass');
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('first')->once()->andReturn(null);
$this->assertNull($store->get('foo'));
}
public function testNullIsReturnedAndItemDeletedWhenItemIsExpired()
{
$store = $this->getMock('Illuminate\Cache\DatabaseStore', ['forget'], $this->getMocks());
$table = m::mock('StdClass');
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('first')->once()->andReturn((object) ['expiration' => 1]);
$store->expects($this->once())->method('forget')->with($this->equalTo('foo'))->will($this->returnValue(null));
$this->assertNull($store->get('foo'));
}
public function testDecryptedValueIsReturnedWhenItemIsValid()
{
$store = $this->getStore();
$table = m::mock('StdClass');
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('first')->once()->andReturn((object) ['value' => 'bar', 'expiration' => 999999999999999]);
$store->getEncrypter()->shouldReceive('decrypt')->once()->with('bar')->andReturn('bar');
$this->assertEquals('bar', $store->get('foo'));
}
public function testEncryptedValueIsInsertedWhenNoExceptionsAreThrown()
{
$store = $this->getMock('Illuminate\Cache\DatabaseStore', ['getTime'], $this->getMocks());
$table = m::mock('StdClass');
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$store->getEncrypter()->shouldReceive('encrypt')->once()->with('bar')->andReturn('bar');
$store->expects($this->once())->method('getTime')->will($this->returnValue(1));
$table->shouldReceive('insert')->once()->with(['key' => 'prefixfoo', 'value' => 'bar', 'expiration' => 61]);
$store->put('foo', 'bar', 1);
}
public function testEncryptedValueIsUpdatedWhenInsertThrowsException()
{
$store = $this->getMock('Illuminate\Cache\DatabaseStore', ['getTime'], $this->getMocks());
$table = m::mock('StdClass');
$store->getConnection()->shouldReceive('table')->with('table')->andReturn($table);
$store->getEncrypter()->shouldReceive('encrypt')->once()->with('bar')->andReturn('bar');
$store->expects($this->once())->method('getTime')->will($this->returnValue(1));
$table->shouldReceive('insert')->once()->with(['key' => 'prefixfoo', 'value' => 'bar', 'expiration' => 61])->andReturnUsing(function () {
throw new Exception;
});
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('update')->once()->with(['value' => 'bar', 'expiration' => 61]);
$store->put('foo', 'bar', 1);
}
public function testForeverCallsStoreItemWithReallyLongTime()
{
$store = $this->getMock('Illuminate\Cache\DatabaseStore', ['put'], $this->getMocks());
$store->expects($this->once())->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(5256000));
$store->forever('foo', 'bar');
}
public function testItemsMayBeRemovedFromCache()
{
$store = $this->getStore();
$table = m::mock('StdClass');
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table);
$table->shouldReceive('delete')->once();
$store->forget('foo');
}
public function testItemsMayBeFlushedFromCache()
{
$store = $this->getStore();
$table = m::mock('StdClass');
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('delete')->once();
$store->flush();
}
public function testIncrementReturnsCorrectValues()
{
$store = $this->getStore();
$table = m::mock('StdClass');
$cache = m::mock('StdClass');
$store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) {
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn(null);
$this->assertFalse($store->increment('foo'));
$cache->value = 'bar';
$store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) {
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn($cache);
$store->getEncrypter()->shouldReceive('decrypt')->once()->with('bar')->andReturn('bar');
$this->assertFalse($store->increment('foo'));
$cache->value = 2;
$store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) {
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn($cache);
$store->getEncrypter()->shouldReceive('decrypt')->once()->with(2)->andReturn(2);
$store->getEncrypter()->shouldReceive('encrypt')->once()->with(3)->andReturn(3);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('update')->once()->with(['value' => 3]);
$this->assertEquals(3, $store->increment('foo'));
}
public function testDecrementReturnsCorrectValues()
{
$store = $this->getStore();
$table = m::mock('StdClass');
$cache = m::mock('StdClass');
$store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) {
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn(null);
$this->assertFalse($store->decrement('foo'));
$cache->value = 'bar';
$store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) {
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn($cache);
$store->getEncrypter()->shouldReceive('decrypt')->once()->with('bar')->andReturn('bar');
$this->assertFalse($store->decrement('foo'));
$cache->value = 3;
$store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) {
return $closure();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixbar')->andReturn($table);
$table->shouldReceive('lockForUpdate')->once()->andReturn($table);
$table->shouldReceive('first')->once()->andReturn($cache);
$store->getEncrypter()->shouldReceive('decrypt')->once()->with(3)->andReturn(3);
$store->getEncrypter()->shouldReceive('encrypt')->once()->with(2)->andReturn(2);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('where')->once()->with('key', 'prefixbar')->andReturn($table);
$table->shouldReceive('update')->once()->with(['value' => 2]);
$this->assertEquals(2, $store->decrement('bar'));
}
protected function getStore()
{
return new DatabaseStore(m::mock('Illuminate\Database\Connection'), m::mock('Illuminate\Contracts\Encryption\Encrypter'), 'table', 'prefix');
}
protected function getMocks()
{
return [m::mock('Illuminate\Database\Connection'), m::mock('Illuminate\Contracts\Encryption\Encrypter'), 'table', 'prefix'];
}
}