Skip to content

Commit 5d64164

Browse files
committed
Add remaining tests
1 parent c2fbb64 commit 5d64164

File tree

2 files changed

+197
-7
lines changed

2 files changed

+197
-7
lines changed

src/Builder.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ protected function getCacheTags() : array
100100
->toArray();
101101
}
102102

103+
public function avg($column)
104+
{
105+
$tags = [str_slug(get_class($this->model))];
106+
$key = str_slug(get_class($this->model)) ."-avg_{$column}";
107+
108+
return $this->cache($tags)
109+
->rememberForever($key, function () use ($column) {
110+
return parent::avg($column);
111+
});
112+
}
113+
103114
public function count($columns = ['*'])
104115
{
105116
$tags = [str_slug(get_class($this->model))];
@@ -111,6 +122,17 @@ public function count($columns = ['*'])
111122
});
112123
}
113124

125+
public function cursor()
126+
{
127+
$tags = [str_slug(get_class($this->model))];
128+
$key = str_slug(get_class($this->model)) ."-cursor";
129+
130+
return $this->cache($tags)
131+
->rememberForever($key, function () {
132+
return collect(parent::cursor());
133+
});
134+
}
135+
114136
/**
115137
* @SuppressWarnings(PHPMD.ShortVariable)
116138
*/
@@ -128,7 +150,7 @@ public function find($id, $columns = ['*'])
128150
public function first($columns = ['*'])
129151
{
130152
$tags = $this->getCacheTags();
131-
$key = $this->getCacheKey($columns);
153+
$key = $this->getCacheKey($columns) . '-first';
132154

133155
return $this->cache($tags)
134156
->rememberForever($key, function () use ($columns) {
@@ -146,4 +168,52 @@ public function get($columns = ['*'])
146168
return parent::get($columns);
147169
});
148170
}
171+
172+
public function max($column)
173+
{
174+
$tags = [str_slug(get_class($this->model))];
175+
$key = str_slug(get_class($this->model)) ."-max_{$column}";
176+
177+
return $this->cache($tags)
178+
->rememberForever($key, function () use ($column) {
179+
return parent::max($column);
180+
});
181+
}
182+
183+
public function min($column)
184+
{
185+
$tags = [str_slug(get_class($this->model))];
186+
$key = str_slug(get_class($this->model)) ."-min_{$column}";
187+
188+
return $this->cache($tags)
189+
->rememberForever($key, function () use ($column) {
190+
return parent::min($column);
191+
});
192+
}
193+
194+
public function pluck($column, $key = null)
195+
{
196+
$tags = $this->getCacheTags();
197+
$cacheKey = $this->getCacheKey([$column]) . "-pluck_{$column}";
198+
199+
if ($key) {
200+
$cacheKey .= "_{$key}";
201+
}
202+
203+
return $this->cache($tags)
204+
->rememberForever($cacheKey, function () use ($column, $key) {
205+
return parent::pluck($column, $key);
206+
});
207+
}
208+
209+
public function sum($column)
210+
{
211+
$tags = [str_slug(get_class($this->model))];
212+
$key = str_slug(get_class($this->model)) ."-sum_{$column}";
213+
214+
return $this->cache($tags)
215+
->rememberForever($key, function () use ($column) {
216+
return parent::sum($column);
217+
});
218+
}
149219
}

tests/Unit/CacheTest.php

Lines changed: 126 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ public function testAllModelResultsCreatesCache()
197197
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
198198
}
199199

200+
public function testAvgModelResultsCreatesCache()
201+
{
202+
$authorId = (new Author)->with('books', 'profile')
203+
->avg('id');
204+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-avg_id';
205+
$tags = [
206+
'genealabslaravelmodelcachingtestsfixturesauthor',
207+
];
208+
209+
$cachedResult = cache()->tags($tags)
210+
->get($key);
211+
$liveResult = (new UncachedAuthor)->with('books', 'profile')
212+
->avg('id');
213+
214+
$this->assertEquals($authorId, $cachedResult);
215+
$this->assertEquals($liveResult, $cachedResult);
216+
}
217+
200218
public function testChunkModelResultsCreatesCache()
201219
{
202220
$cachedChunks = collect([
@@ -257,6 +275,24 @@ public function testCountModelResultsCreatesCache()
257275
$this->assertEquals($liveResults, $cachedResults);
258276
}
259277

278+
public function testCursorModelResultsCreatesCache()
279+
{
280+
$authors = (new Author)->with('books', 'profile')
281+
->cursor();
282+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-cursor';
283+
$tags = [
284+
'genealabslaravelmodelcachingtestsfixturesauthor',
285+
];
286+
287+
$cachedResults = cache()->tags($tags)
288+
->get($key);
289+
$liveResults = collect((new UncachedAuthor)->with('books', 'profile')
290+
->cursor());
291+
292+
$this->assertEmpty($authors->diffAssoc($cachedResults));
293+
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
294+
}
295+
260296
public function testFindModelResultsCreatesCache()
261297
{
262298
$author = (new Author)->find(1);
@@ -293,15 +329,99 @@ public function testGetModelResultsCreatesCache()
293329
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
294330
}
295331

296-
// test cursor()
332+
public function testMaxModelResultsCreatesCache()
333+
{
334+
$authorId = (new Author)->with('books', 'profile')
335+
->max('id');
336+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-max_id';
337+
$tags = [
338+
'genealabslaravelmodelcachingtestsfixturesauthor',
339+
];
340+
341+
$cachedResult = cache()->tags($tags)
342+
->get($key);
343+
$liveResult = (new UncachedAuthor)->with('books', 'profile')
344+
->max('id');
297345

298-
// test max()
346+
$this->assertEquals($authorId, $cachedResult);
347+
$this->assertEquals($liveResult, $cachedResult);
348+
}
299349

300-
// test min()
350+
public function testMinModelResultsCreatesCache()
351+
{
352+
$authorId = (new Author)->with('books', 'profile')
353+
->min('id');
354+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-min_id';
355+
$tags = [
356+
'genealabslaravelmodelcachingtestsfixturesauthor',
357+
];
301358

302-
// test avg()
359+
$cachedResult = cache()->tags($tags)
360+
->get($key);
361+
$liveResult = (new UncachedAuthor)->with('books', 'profile')
362+
->min('id');
303363

304-
// test value()
364+
$this->assertEquals($authorId, $cachedResult);
365+
$this->assertEquals($liveResult, $cachedResult);
366+
}
305367

306-
// test pluck()
368+
public function testPluckModelResultsCreatesCache()
369+
{
370+
$authors = (new Author)->with('books', 'profile')
371+
->pluck('id');
372+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_id-books-profile-pluck_id';
373+
$tags = [
374+
'genealabslaravelmodelcachingtestsfixturesauthor',
375+
'genealabslaravelmodelcachingtestsfixturesbook',
376+
'genealabslaravelmodelcachingtestsfixturesprofile',
377+
];
378+
379+
$cachedResults = cache()->tags($tags)
380+
->get($key);
381+
$liveResults = (new UncachedAuthor)->with('books', 'profile')
382+
->pluck('id');
383+
384+
$this->assertEmpty($authors->diffAssoc($cachedResults));
385+
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
386+
}
387+
388+
public function testSumModelResultsCreatesCache()
389+
{
390+
$authorId = (new Author)->with('books', 'profile')
391+
->sum('id');
392+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-sum_id';
393+
$tags = [
394+
'genealabslaravelmodelcachingtestsfixturesauthor',
395+
];
396+
397+
$cachedResult = cache()->tags($tags)
398+
->get($key);
399+
$liveResult = (new UncachedAuthor)->with('books', 'profile')
400+
->sum('id');
401+
402+
$this->assertEquals($authorId, $cachedResult);
403+
$this->assertEquals($liveResult, $cachedResult);
404+
}
405+
406+
public function testValueModelResultsCreatesCache()
407+
{
408+
$authors = (new Author)->with('books', 'profile')
409+
->value('name');
410+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-first';
411+
$tags = [
412+
'genealabslaravelmodelcachingtestsfixturesauthor',
413+
'genealabslaravelmodelcachingtestsfixturesbook',
414+
'genealabslaravelmodelcachingtestsfixturesprofile',
415+
];
416+
417+
$cachedResults = cache()->tags($tags)
418+
->get($key)
419+
->name;
420+
421+
$liveResults = (new UncachedAuthor)->with('books', 'profile')
422+
->value('name');
423+
424+
$this->assertEquals($authors, $cachedResults);
425+
$this->assertEquals($liveResults, $cachedResults);
426+
}
307427
}

0 commit comments

Comments
 (0)