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
·125 lines (94 loc) · 4.49 KB
/
CacheDatabaseStoreTest.php
File metadata and controls
executable file
·125 lines (94 loc) · 4.49 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
<?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', array('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) array('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) array('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', array('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(array('key' => 'prefixfoo', 'value' => 'bar', 'expiration' => 61));
$store->put('foo', 'bar', 1);
}
public function testEncryptedValueIsUpdatedWhenInsertThrowsException()
{
$store = $this->getMock('Illuminate\Cache\DatabaseStore', array('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(array('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(array('value' => 'bar', 'expiration' => 61));
$store->put('foo', 'bar', 1);
}
public function testForeverCallsStoreItemWithReallyLongTime()
{
$store = $this->getMock('Illuminate\Cache\DatabaseStore', array('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();
}
protected function getStore()
{
return new DatabaseStore(m::mock('Illuminate\Database\Connection'), m::mock('Illuminate\Encryption\Encrypter'), 'table', 'prefix');
}
protected function getMocks()
{
return array(m::mock('Illuminate\Database\Connection'), m::mock('Illuminate\Encryption\Encrypter'), 'table', 'prefix');
}
}