forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheEventsTest.php
More file actions
executable file
·189 lines (149 loc) · 8.59 KB
/
CacheEventsTest.php
File metadata and controls
executable file
·189 lines (149 loc) · 8.59 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
<?php
use Mockery as m;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Cache\Events\KeyWritten;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Cache\Events\KeyForgotten;
class CacheEventTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}
public function testHasTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
$this->assertFalse($repository->has('foo'));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux']));
$this->assertTrue($repository->has('baz'));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
$this->assertFalse($repository->tags('taylor')->has('foo'));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux', 'tags' => ['taylor']]));
$this->assertTrue($repository->tags('taylor')->has('baz'));
}
public function testGetTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
$this->assertNull($repository->get('foo'));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux']));
$this->assertEquals('qux', $repository->get('baz'));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
$this->assertNull($repository->tags('taylor')->get('foo'));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux', 'tags' => ['taylor']]));
$this->assertEquals('qux', $repository->tags('taylor')->get('baz'));
}
public function testPullTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux']));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz']));
$this->assertEquals('qux', $repository->pull('baz'));
}
public function testPullTriggersEventsUsingTags()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux', 'tags' => ['taylor']]));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz', 'tags' => ['taylor']]));
$this->assertEquals('qux', $repository->tags('taylor')->pull('baz'));
}
public function testPutTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar']));
$repository->put('foo', 'bar', 99);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'tags' => ['taylor']]));
$repository->tags('taylor')->put('foo', 'bar', 99);
}
public function testAddTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar']));
$this->assertTrue($repository->add('foo', 'bar', 99));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'tags' => ['taylor']]));
$this->assertTrue($repository->tags('taylor')->add('foo', 'bar', 99));
}
public function testForeverTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar']));
$repository->forever('foo', 'bar');
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'tags' => ['taylor']]));
$repository->tags('taylor')->forever('foo', 'bar');
}
public function testRememberTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar']));
$this->assertEquals('bar', $repository->remember('foo', 99, function () {
return 'bar';
}));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'tags' => ['taylor']]));
$this->assertEquals('bar', $repository->tags('taylor')->remember('foo', 99, function () {
return 'bar';
}));
}
public function testRememberForeverTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar']));
$this->assertEquals('bar', $repository->rememberForever('foo', function () {
return 'bar';
}));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'tags' => ['taylor']]));
$this->assertEquals('bar', $repository->tags('taylor')->rememberForever('foo', function () {
return 'bar';
}));
}
public function testForgetTriggersEvents()
{
$dispatcher = $this->getDispatcher();
$repository = $this->getRepository($dispatcher);
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz']));
$this->assertTrue($repository->forget('baz'));
$dispatcher->shouldReceive('fire')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz', 'tags' => ['taylor']]));
$this->assertTrue($repository->tags('taylor')->forget('baz'));
}
protected function assertEventMatches($eventClass, $properties = [])
{
return m::on(function ($event) use ($eventClass, $properties) {
if (! $event instanceof $eventClass) {
return false;
}
foreach ($properties as $name => $value) {
if ($event->$name != $value) {
return false;
}
}
return true;
});
}
protected function getDispatcher()
{
return m::mock('Illuminate\Events\Dispatcher');
}
protected function getRepository($dispatcher)
{
$repository = new \Illuminate\Cache\Repository(new \Illuminate\Cache\ArrayStore());
$repository->put('baz', 'qux', 99);
$repository->tags('taylor')->put('baz', 'qux', 99);
$repository->setEventDispatcher($dispatcher);
return $repository;
}
}