forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheRepositoryTest.php
More file actions
executable file
·130 lines (111 loc) · 4.93 KB
/
CacheRepositoryTest.php
File metadata and controls
executable file
·130 lines (111 loc) · 4.93 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
<?php
use Mockery as m;
use Carbon\Carbon;
class CacheRepositoryTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}
public function testGetReturnsValueFromCache()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn('bar');
$this->assertEquals('bar', $repo->get('foo'));
}
public function testGetReturnsMultipleValuesFromCacheWhenGivenAnArray()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('many')->once()->with(['foo', 'bar'])->andReturn(['foo' => 'bar', 'bar' => 'baz']);
$this->assertEquals(['foo' => 'bar', 'bar' => 'baz'], $repo->get(['foo', 'bar']));
}
public function testGetReturnsMultipleValuesFromCacheWhenGivenAnArrayWithDefaultValues()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('many')->once()->with(['foo', 'bar'])->andReturn(['foo' => null, 'bar' => 'baz']);
$this->assertEquals(['foo' => 'default', 'bar' => 'baz'], $repo->get(['foo' => 'default', 'bar']));
}
public function testDefaultValueIsReturned()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('get')->andReturn(null);
$this->assertEquals('bar', $repo->get('foo', 'bar'));
$this->assertEquals('baz', $repo->get('boom', function () { return 'baz'; }));
}
public function testSettingDefaultCacheTime()
{
$repo = $this->getRepository();
$repo->setDefaultCacheTime(10);
$this->assertEquals(10, $repo->getDefaultCacheTime());
}
public function testHasMethod()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('get')->once()->with('foo')->andReturn(null);
$repo->getStore()->shouldReceive('get')->once()->with('bar')->andReturn('bar');
$this->assertTrue($repo->has('bar'));
$this->assertFalse($repo->has('foo'));
}
public function testRememberMethodCallsPutAndReturnsDefault()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('get')->andReturn(null);
$repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', 10);
$result = $repo->remember('foo', 10, function () { return 'bar'; });
$this->assertEquals('bar', $result);
/*
* Use Carbon object...
*/
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('get')->andReturn(null);
$repo->getStore()->shouldReceive('put')->once()->with('foo', 'bar', 10);
$repo->getStore()->shouldReceive('put')->once()->with('baz', 'qux', 9);
$result = $repo->remember('foo', Carbon::now()->addMinutes(10)->addSeconds(2), function () { return 'bar'; });
$this->assertEquals('bar', $result);
$result = $repo->remember('baz', Carbon::now()->addMinutes(10)->subSeconds(2), function () { return 'qux'; });
$this->assertEquals('qux', $result);
}
public function testRememberForeverMethodCallsForeverAndReturnsDefault()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('get')->andReturn(null);
$repo->getStore()->shouldReceive('forever')->once()->with('foo', 'bar');
$result = $repo->rememberForever('foo', function () { return 'bar'; });
$this->assertEquals('bar', $result);
}
public function testPuttingMultipleItemsInCache()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('putMany')->once()->with(['foo' => 'bar', 'bar' => 'baz'], 1);
$repo->put(['foo' => 'bar', 'bar' => 'baz'], 1);
}
public function testPutWithDatetimeInPastOrZeroMinutesDoesntSaveItem()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('put')->never();
$repo->put('foo', 'bar', Carbon::now()->subMinutes(10));
$repo->put('foo', 'bar', Carbon::now()->addSeconds(5));
}
public function testAddWithDatetimeInPastOrZeroMinutesReturnsImmediately()
{
$repo = $this->getRepository();
$repo->getStore()->shouldReceive('add', 'get', 'put')->never();
$result = $repo->add('foo', 'bar', Carbon::now()->subMinutes(10));
$this->assertFalse($result);
$result = $repo->add('foo', 'bar', Carbon::now()->addSeconds(5));
$this->assertFalse($result);
}
public function testRegisterMacroWithNonStaticCall()
{
$repo = $this->getRepository();
$repo::macro(__CLASS__, function () { return 'Taylor'; });
$this->assertEquals($repo->{__CLASS__}(), 'Taylor');
}
protected function getRepository()
{
$dispatcher = new Illuminate\Events\Dispatcher(m::mock('Illuminate\Container\Container'));
$repository = new Illuminate\Cache\Repository(m::mock('Illuminate\Contracts\Cache\Store'));
$repository->setEventDispatcher($dispatcher);
return $repository;
}
}