1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit; |
2
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author; |
4
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; |
5
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile; |
6
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store; |
7
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor; |
8
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook; |
9
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile; |
10
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore; |
11
|
|
|
use GeneaLabs\LaravelModelCaching\Tests\TestCase; |
12
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
16
|
|
|
*/ |
17
|
|
|
class CachedBuilderTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
use RefreshDatabase; |
20
|
|
|
|
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
parent::setUp(); |
24
|
|
|
|
25
|
|
|
cache()->flush(); |
26
|
|
|
factory(Author::class, 10)->create() |
27
|
|
|
->each(function($author) { |
28
|
|
|
factory(Book::class, random_int(2, 10))->make() |
29
|
|
|
->each(function ($book) use ($author) { |
30
|
|
|
$book->author()->associate($author); |
31
|
|
|
$book->save(); |
32
|
|
|
}); |
33
|
|
|
factory(Profile::class)->make([ |
34
|
|
|
'author_id' => $author->id, |
35
|
|
|
]); |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
$bookIds = (new Book)->all()->pluck('id'); |
39
|
|
|
factory(Store::class, 10)->create() |
40
|
|
|
->each(function ($store) use ($bookIds) { |
41
|
|
|
$store->books()->sync(rand($bookIds->min(), $bookIds->max())); |
42
|
|
|
}); |
43
|
|
|
cache()->flush(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testCacheIsEmptyBeforeLoadingModels() |
47
|
|
|
{ |
48
|
|
|
$results = cache()->tags([ |
49
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
50
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook' |
51
|
|
|
]) |
52
|
|
|
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
53
|
|
|
|
54
|
|
|
$this->assertNull($results); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testCacheIsNotEmptyAfterLoadingModels() |
58
|
|
|
{ |
59
|
|
|
(new Author)->with('books')->get(); |
60
|
|
|
|
61
|
|
|
$results = cache()->tags([ |
62
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
63
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook' |
64
|
|
|
]) |
65
|
|
|
->get('genealabslaravelmodelcachingtestsfixturesauthor-books'); |
66
|
|
|
|
67
|
|
|
$this->assertNotNull($results); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testCreatingModelClearsCache() |
71
|
|
|
{ |
72
|
|
|
(new Author)->with('books')->get(); |
73
|
|
|
|
74
|
|
|
factory(Author::class)->create(); |
75
|
|
|
|
76
|
|
|
$results = cache()->tags([ |
77
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
78
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook' |
79
|
|
|
]) |
80
|
|
|
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
81
|
|
|
|
82
|
|
|
$this->assertNull($results); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testUpdatingModelClearsCache() |
86
|
|
|
{ |
87
|
|
|
$author = (new Author)->with('books')->get()->first(); |
88
|
|
|
$author->name = "John Jinglheimer"; |
89
|
|
|
$author->save(); |
90
|
|
|
|
91
|
|
|
$results = cache()->tags([ |
92
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
93
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook' |
94
|
|
|
]) |
95
|
|
|
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
96
|
|
|
|
97
|
|
|
$this->assertNull($results); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testDeletingModelClearsCache() |
101
|
|
|
{ |
102
|
|
|
$author = (new Author)->with('books')->get()->first(); |
103
|
|
|
$author->delete(); |
104
|
|
|
|
105
|
|
|
$results = cache()->tags([ |
106
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
107
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook' |
108
|
|
|
]) |
109
|
|
|
->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); |
110
|
|
|
|
111
|
|
|
$this->assertNull($results); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testHasManyRelationshipIsCached() |
115
|
|
|
{ |
116
|
|
|
$authors = (new Author)->with('books')->get(); |
117
|
|
|
|
118
|
|
|
$results = collect(cache()->tags([ |
119
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
120
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook' |
121
|
|
|
]) |
122
|
|
|
->get("genealabslaravelmodelcachingtestsfixturesauthor-books")); |
123
|
|
|
|
124
|
|
|
$this->assertNotNull($results); |
125
|
|
|
$this->assertEmpty($authors->diffAssoc($results)); |
126
|
|
|
$this->assertNotEmpty($authors); |
127
|
|
|
$this->assertNotEmpty($results); |
128
|
|
|
$this->assertEquals($authors->count(), $results->count()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testBelongsToRelationshipIsCached() |
132
|
|
|
{ |
133
|
|
|
$books = (new Book)->with('author')->get(); |
134
|
|
|
|
135
|
|
|
$results = collect(cache()->tags([ |
136
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook', |
137
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor' |
138
|
|
|
]) |
139
|
|
|
->get("genealabslaravelmodelcachingtestsfixturesbook-author")); |
140
|
|
|
|
141
|
|
|
$this->assertNotNull($results); |
142
|
|
|
$this->assertEmpty($books->diffAssoc($results)); |
143
|
|
|
$this->assertNotEmpty($books); |
144
|
|
|
$this->assertNotEmpty($results); |
145
|
|
|
$this->assertEquals($books->count(), $results->count()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testBelongsToManyRelationshipIsCached() |
149
|
|
|
{ |
150
|
|
|
$books = (new Book)->with('stores')->get(); |
151
|
|
|
|
152
|
|
|
$results = collect(cache()->tags([ |
153
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook', |
154
|
|
|
'genealabslaravelmodelcachingtestsfixturesstore' |
155
|
|
|
]) |
156
|
|
|
->get("genealabslaravelmodelcachingtestsfixturesbook-stores")); |
157
|
|
|
|
158
|
|
|
$this->assertNotNull($results); |
159
|
|
|
$this->assertEmpty($books->diffAssoc($results)); |
160
|
|
|
$this->assertNotEmpty($books); |
161
|
|
|
$this->assertNotEmpty($results); |
162
|
|
|
$this->assertEquals($books->count(), $results->count()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testHasOneRelationshipIsCached() |
166
|
|
|
{ |
167
|
|
|
$authors = (new Author)->with('profile')->get(); |
168
|
|
|
|
169
|
|
|
$results = collect(cache() |
170
|
|
|
->tags([ |
171
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
172
|
|
|
'genealabslaravelmodelcachingtestsfixturesprofile' |
173
|
|
|
]) |
174
|
|
|
->get("genealabslaravelmodelcachingtestsfixturesauthor-profile")); |
175
|
|
|
|
176
|
|
|
$this->assertNotNull($results); |
177
|
|
|
$this->assertEmpty($authors->diffAssoc($results)); |
178
|
|
|
$this->assertNotEmpty($authors); |
179
|
|
|
$this->assertNotEmpty($results); |
180
|
|
|
$this->assertEquals($authors->count(), $results->count()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testAvgModelResultsCreatesCache() |
184
|
|
|
{ |
185
|
|
|
$authorId = (new Author)->with('books', 'profile') |
186
|
|
|
->avg('id'); |
187
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-avg_id'; |
188
|
|
|
$tags = [ |
189
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
190
|
|
|
]; |
191
|
|
|
|
192
|
|
|
$cachedResult = cache()->tags($tags) |
193
|
|
|
->get($key); |
194
|
|
|
$liveResult = (new UncachedAuthor)->with('books', 'profile') |
195
|
|
|
->avg('id'); |
196
|
|
|
|
197
|
|
|
$this->assertEquals($authorId, $cachedResult); |
198
|
|
|
$this->assertEquals($liveResult, $cachedResult); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testChunkModelResultsCreatesCache() |
202
|
|
|
{ |
203
|
|
|
$cachedChunks = collect([ |
204
|
|
|
'authors' => collect(), |
205
|
|
|
'keys' => collect(), |
206
|
|
|
]); |
207
|
|
|
$chunkSize = 3; |
208
|
|
|
$tags = [ |
209
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
210
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook', |
211
|
|
|
'genealabslaravelmodelcachingtestsfixturesprofile', |
212
|
|
|
]; |
213
|
|
|
$uncachedChunks = collect(); |
214
|
|
|
|
215
|
|
|
(new Author)->with('books', 'profile') |
216
|
|
|
->chunk($chunkSize, function ($chunk) use (&$cachedChunks, $chunkSize) { |
217
|
|
|
$offset = ''; |
218
|
|
|
|
219
|
|
|
if ($cachedChunks['authors']->count()) { |
220
|
|
|
$offsetIncrement = $cachedChunks['authors']->count() * $chunkSize; |
221
|
|
|
$offset = "-offset_{$offsetIncrement}"; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$cachedChunks['authors']->push($chunk); |
225
|
|
|
$cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile{$offset}-limit_3"); |
226
|
|
|
}); |
227
|
|
|
|
228
|
|
|
(new UncachedAuthor)->with('books', 'profile') |
229
|
|
|
->chunk($chunkSize, function ($chunk) use (&$uncachedChunks) { |
230
|
|
|
$uncachedChunks->push($chunk); |
231
|
|
|
}); |
232
|
|
|
|
233
|
|
|
for ($index = 0; $index < $cachedChunks['authors']->count(); $index++) { |
234
|
|
|
$key = $cachedChunks['keys'][$index]; |
235
|
|
|
$cachedResults = cache()->tags($tags) |
236
|
|
|
->get($key); |
237
|
|
|
|
238
|
|
|
$this->assertEmpty($cachedChunks['authors'][$index]->diffAssoc($cachedResults)); |
239
|
|
|
$this->assertEmpty($uncachedChunks[$index]->diffAssoc($cachedResults)); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testCountModelResultsCreatesCache() |
244
|
|
|
{ |
245
|
|
|
$authors = (new Author)->with('books', 'profile') |
246
|
|
|
->count(); |
247
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-count'; |
248
|
|
|
$tags = [ |
249
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
250
|
|
|
]; |
251
|
|
|
|
252
|
|
|
$cachedResults = cache()->tags($tags) |
253
|
|
|
->get($key); |
254
|
|
|
$liveResults = (new UncachedAuthor)->with('books', 'profile') |
255
|
|
|
->count(); |
256
|
|
|
|
257
|
|
|
$this->assertEquals($authors, $cachedResults); |
258
|
|
|
$this->assertEquals($liveResults, $cachedResults); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function testCursorModelResultsCreatesCache() |
262
|
|
|
{ |
263
|
|
|
$authors = (new Author)->with('books', 'profile') |
264
|
|
|
->cursor(); |
265
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-cursor'; |
266
|
|
|
$tags = [ |
267
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
268
|
|
|
]; |
269
|
|
|
|
270
|
|
|
$cachedResults = cache()->tags($tags) |
271
|
|
|
->get($key); |
272
|
|
|
$liveResults = collect((new UncachedAuthor)->with('books', 'profile') |
273
|
|
|
->cursor()); |
274
|
|
|
|
275
|
|
|
$this->assertEmpty($authors->diffAssoc($cachedResults)); |
276
|
|
|
$this->assertEmpty($liveResults->diffAssoc($cachedResults)); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function testFindModelResultsCreatesCache() |
280
|
|
|
{ |
281
|
|
|
$author = collect()->push((new Author)->find(1)); |
282
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_1'; |
283
|
|
|
$tags = [ |
284
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
285
|
|
|
]; |
286
|
|
|
|
287
|
|
|
$cachedResults = collect()->push(cache()->tags($tags) |
288
|
|
|
->get($key)); |
289
|
|
|
$liveResults = collect()->push((new UncachedAuthor)->find(1)); |
290
|
|
|
|
291
|
|
|
$this->assertEmpty($author->diffAssoc($cachedResults)); |
292
|
|
|
$this->assertEmpty($liveResults->diffAssoc($cachedResults)); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function testGetModelResultsCreatesCache() |
296
|
|
|
{ |
297
|
|
|
$authors = (new Author)->with('books', 'profile') |
298
|
|
|
->get(); |
299
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile'; |
300
|
|
|
$tags = [ |
301
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
302
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook', |
303
|
|
|
'genealabslaravelmodelcachingtestsfixturesprofile', |
304
|
|
|
]; |
305
|
|
|
|
306
|
|
|
$cachedResults = cache()->tags($tags) |
307
|
|
|
->get($key); |
308
|
|
|
$liveResults = (new UncachedAuthor)->with('books', 'profile') |
309
|
|
|
->get(); |
310
|
|
|
|
311
|
|
|
$this->assertEquals($authors, $cachedResults); |
312
|
|
|
$this->assertEmpty($liveResults->diffAssoc($cachedResults)); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function testMaxModelResultsCreatesCache() |
316
|
|
|
{ |
317
|
|
|
$authorId = (new Author)->with('books', 'profile') |
318
|
|
|
->max('id'); |
319
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-max_id'; |
320
|
|
|
$tags = [ |
321
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
322
|
|
|
]; |
323
|
|
|
|
324
|
|
|
$cachedResult = cache()->tags($tags) |
325
|
|
|
->get($key); |
326
|
|
|
$liveResult = (new UncachedAuthor)->with('books', 'profile') |
327
|
|
|
->max('id'); |
328
|
|
|
|
329
|
|
|
$this->assertEquals($authorId, $cachedResult); |
330
|
|
|
$this->assertEquals($liveResult, $cachedResult); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function testMinModelResultsCreatesCache() |
334
|
|
|
{ |
335
|
|
|
$authorId = (new Author)->with('books', 'profile') |
336
|
|
|
->min('id'); |
337
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-min_id'; |
338
|
|
|
$tags = [ |
339
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
340
|
|
|
]; |
341
|
|
|
|
342
|
|
|
$cachedResult = cache()->tags($tags) |
343
|
|
|
->get($key); |
344
|
|
|
$liveResult = (new UncachedAuthor)->with('books', 'profile') |
345
|
|
|
->min('id'); |
346
|
|
|
|
347
|
|
|
$this->assertEquals($authorId, $cachedResult); |
348
|
|
|
$this->assertEquals($liveResult, $cachedResult); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testPluckModelResultsCreatesCache() |
352
|
|
|
{ |
353
|
|
|
$authors = (new Author)->with('books', 'profile') |
354
|
|
|
->pluck('name', 'id'); |
355
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-pluck_name_id'; |
356
|
|
|
$tags = [ |
357
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
358
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook', |
359
|
|
|
'genealabslaravelmodelcachingtestsfixturesprofile', |
360
|
|
|
]; |
361
|
|
|
|
362
|
|
|
$cachedResults = cache()->tags($tags) |
363
|
|
|
->get($key); |
364
|
|
|
$liveResults = (new UncachedAuthor)->with('books', 'profile') |
365
|
|
|
->pluck('name', 'id'); |
366
|
|
|
|
367
|
|
|
$this->assertEmpty($authors->diffAssoc($cachedResults)); |
368
|
|
|
$this->assertEmpty($liveResults->diffAssoc($cachedResults)); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function testSumModelResultsCreatesCache() |
372
|
|
|
{ |
373
|
|
|
$authorId = (new Author)->with('books', 'profile') |
374
|
|
|
->sum('id'); |
375
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-sum_id'; |
376
|
|
|
$tags = [ |
377
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
378
|
|
|
]; |
379
|
|
|
|
380
|
|
|
$cachedResult = cache()->tags($tags) |
381
|
|
|
->get($key); |
382
|
|
|
$liveResult = (new UncachedAuthor)->with('books', 'profile') |
383
|
|
|
->sum('id'); |
384
|
|
|
|
385
|
|
|
$this->assertEquals($authorId, $cachedResult); |
386
|
|
|
$this->assertEquals($liveResult, $cachedResult); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
public function testValueModelResultsCreatesCache() |
390
|
|
|
{ |
391
|
|
|
$authors = (new Author)->with('books', 'profile') |
392
|
|
|
->value('name'); |
393
|
|
|
$key = 'genealabslaravelmodelcachingtestsfixturesauthor_name-books-profile-first'; |
394
|
|
|
$tags = [ |
395
|
|
|
'genealabslaravelmodelcachingtestsfixturesauthor', |
396
|
|
|
'genealabslaravelmodelcachingtestsfixturesbook', |
397
|
|
|
'genealabslaravelmodelcachingtestsfixturesprofile', |
398
|
|
|
]; |
399
|
|
|
|
400
|
|
|
$cachedResults = cache()->tags($tags) |
401
|
|
|
->get($key) |
402
|
|
|
->name; |
403
|
|
|
|
404
|
|
|
$liveResults = (new UncachedAuthor)->with('books', 'profile') |
405
|
|
|
->value('name'); |
406
|
|
|
|
407
|
|
|
$this->assertEquals($authors, $cachedResults); |
408
|
|
|
$this->assertEquals($liveResults, $cachedResults); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|