-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryCacheTest.php
More file actions
237 lines (182 loc) · 6.91 KB
/
QueryCacheTest.php
File metadata and controls
237 lines (182 loc) · 6.91 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Facade;
use SimpleSoftwareIO\Cache\QueryCache;
use SimpleSoftwareIO\Cache\QueryCacheBuilder;
class QueryCacheTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
Facade::clearResolvedInstances();
}
public function tearDown()
{
Mockery::close();
}
/** @test */
public function enable_returns_true_when_a_length_exists()
{
$queryCache = new QueryCache(null, 30);
$this->assertTrue($queryCache->enabled());
}
/** @test */
public function enable_returns_false_when_a_length_does_not_exists()
{
$queryCache = new QueryCache(null, 0);
$this->assertFalse($queryCache->enabled());
}
/** @test */
public function disable_turns_off_caching()
{
$queryCache = new QueryCache(null, 30);
$queryCache->disable();
$this->assertFalse($queryCache->enabled());
}
/** @test */
public function enable_turns_on_caching()
{
$queryCache = new QueryCache(null, 0);
$queryCache->enable();
$this->assertTrue($queryCache->enabled());
}
/** @test */
public function length_sets_the_length_to_cache()
{
$queryCache = new QueryCache(null, 30);
$queryCache->length(20);
$this->assertAttributeEquals(20, 'length', $queryCache);
}
/** @test */
public function cache_is_not_called_when_it_is_disabled()
{
$queryCacheBuilder = Mockery::mock(QueryCacheBuilder::class);
$queryCache = Mockery::mock('SimpleSoftwareIO\Cache\QueryCache[performQuery]', [null, 0])
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
Cache::shouldReceive('remember')
->times(0);
$queryCache->get($queryCacheBuilder);
}
/** @test */
public function cache_is_called_when_it_is_enabled()
{
$queryCacheBuilder = Mockery::mock(QueryCacheBuilder::class);
$queryCache = Mockery::mock('SimpleSoftwareIO\Cache\QueryCache[generateKey,performQuery]', [null, 30])
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
Cache::shouldReceive('getStore');
Cache::shouldReceive('store->remember')
->once();
$queryCache->get($queryCacheBuilder);
}
/** @test */
public function cache_is_called_on_the_correct_store()
{
$queryCacheBuilder = Mockery::mock(QueryCacheBuilder::class);
$queryCache = Mockery::mock('SimpleSoftwareIO\Cache\QueryCache[generateKey,performQuery]', ['fooStore', 30])
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
Cache::shouldReceive('getStore');
Cache::shouldReceive('store')
->withArgs(['fooStore'])
->once()
->andReturn(Mockery::self())
->shouldReceive('remember');
$queryCache->get($queryCacheBuilder);
}
/** @test */
public function cache_is_called_with_correct_key_and_length()
{
$queryCacheBuilder = Mockery::mock(QueryCacheBuilder::class);
$queryCache = Mockery::mock('SimpleSoftwareIO\Cache\QueryCache[generateKey,performQuery]', [null, 30])
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
$queryCache->shouldReceive('generateKey')
->once()
->andReturn('fooKey');
Cache::shouldReceive('getStore');
Cache::shouldReceive('store->remember')
->withArgs(['fooKey', 30, Mockery::any()])
->once();
$queryCache->get($queryCacheBuilder);
}
/** @test */
public function cache_is_called_with_tags_if_the_driver_supports_tags()
{
$queryCacheBuilder = Mockery::mock(QueryCacheBuilder::class);
$queryCache = Mockery::mock('SimpleSoftwareIO\Cache\QueryCache[generateKey,performQuery,isTaggable]', [null, 30])
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
$queryCache->shouldReceive('isTaggable')
->andReturn(true);
Cache::shouldReceive('store->tags')
->once()
->andReturn(Mockery::self())
->shouldReceive('remember');
$queryCache->get($queryCacheBuilder);
}
/** @test */
public function cache_is_called_without_tags_if_the_driver_does_not_support_tags()
{
$queryCacheBuilder = Mockery::mock(QueryCacheBuilder::class);
$queryCache = Mockery::mock('SimpleSoftwareIO\Cache\QueryCache[generateKey,performQuery,isTaggable]', [null, 30])
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
$queryCache->shouldReceive('isTaggable')
->andReturn(false);
Cache::shouldReceive('tags')
->times(0)
->andReturn(Mockery::self())
->shouldReceive('store->remember');
$queryCache->get($queryCacheBuilder);
}
/** @test */
public function cache_is_tagged_with_the_correct_model()
{
$queryCacheBuilder = Mockery::mock(QueryCacheBuilder::class);
$queryCache = Mockery::mock('SimpleSoftwareIO\Cache\QueryCache[generateKey,performQuery,isTaggable]', [null, 30])
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
$queryCacheBuilder->from = 'fooTable';
$queryCache->shouldReceive('isTaggable')
->andReturn(true);
Cache::shouldReceive('store->tags')
->once()
->withArgs(['fooTable'])
->andReturn(Mockery::self())
->shouldReceive('remember');
$queryCache->get($queryCacheBuilder);
}
/** @test */
public function cache_is_flushed_for_a_model_if_tagging_is_supported()
{
$queryCache = Mockery::mock('SimpleSoftwareIO\Cache\QueryCache[isTaggable]', [null, 30])
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
$queryCache->shouldReceive('isTaggable')
->andReturn(true);
Cache::shouldReceive('tags')
->once()
->withArgs(['fooTable'])
->andReturn(Mockery::self())
->shouldReceive('flush')
->once();
$queryCache->flush('fooTable');
}
/** @test */
public function cache_is_flushed_for_everything_if_tagging_is_not_supported()
{
$queryCacheBuilder = Mockery::mock(QueryCacheBuilder::class);
$queryCache = Mockery::mock('SimpleSoftwareIO\Cache\QueryCache[isTaggable]', [null, 30])
->shouldAllowMockingProtectedMethods()
->shouldIgnoreMissing();
$queryCacheBuilder->from = 'fooTable';
$queryCache->shouldReceive('isTaggable')
->andReturn(false);
Cache::shouldReceive('tags')
->times(0);
Cache::shouldReceive('flush')
->once();
$queryCache->flush($queryCacheBuilder);
}
}