Passed
Push — master ( c2fbb6...5d6416 )
by Mike
02:47
created

CacheTest::testMaxModelResultsCreatesCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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
class CacheTest extends TestCase
15
{
16
    use RefreshDatabase;
17
18
    public function setUp()
19
    {
20
        parent::setUp();
21
22
        cache()->flush();
23
        factory(Author::class, 10)->create()
24
            ->each(function($author) {
25
                factory(Book::class, random_int(2, 10))->make()
26
                    ->each(function ($book) use ($author) {
27
                        $book->author()->associate($author);
28
                        $book->save();
29
                    });
30
                factory(Profile::class)->make([
31
                    'author_id' => $author->id,
32
                ]);
33
            });
34
35
        $bookIds = (new Book)->all()->pluck('id');
36
        factory(Store::class, 10)->create()
37
            ->each(function ($store) use ($bookIds) {
38
                $store->books()->sync(rand($bookIds->min(), $bookIds->max()));
39
            });
40
        cache()->flush();
41
    }
42
43
    // public function testCacheIsEmptyBeforeLoadingModels()
44
    // {
45
    //     $results = cache()->tags([
46
    //             'genealabslaravelmodelcachingtestsfixturesauthor',
47
    //             'genealabslaravelmodelcachingtestsfixturesbook'
48
    //         ])
49
    //         ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
50
    //
51
    //     $this->assertNull($results);
52
    // }
53
    //
54
    // public function testCacheIsNotEmptyAfterLoadingModels()
55
    // {
56
    //     (new Author)->with('books')->get();
57
    //
58
    //     $results = cache()->tags([
59
    //             'genealabslaravelmodelcachingtestsfixturesauthor',
60
    //             'genealabslaravelmodelcachingtestsfixturesbook'
61
    //         ])
62
    //         ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
63
    //
64
    //     $this->assertNotNull($results);
65
    // }
66
    //
67
    // public function testCreatingModelClearsCache()
68
    // {
69
    //     (new Author)->with('books')->get();
70
    //
71
    //     factory(Author::class)->create();
72
    //
73
    //     $results = cache()->tags([
74
    //             'genealabslaravelmodelcachingtestsfixturesauthor',
75
    //             'genealabslaravelmodelcachingtestsfixturesbook'
76
    //         ])
77
    //         ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
78
    //
79
    //     $this->assertNull($results);
80
    // }
81
    //
82
    // public function testUpdatingModelClearsCache()
83
    // {
84
    //     $author = (new Author)->with('books')->get()->first();
85
    //     $author->name = "John Jinglheimer";
86
    //     $author->save();
87
    //
88
    //     $results = cache()->tags([
89
    //             'genealabslaravelmodelcachingtestsfixturesauthor',
90
    //             'genealabslaravelmodelcachingtestsfixturesbook'
91
    //         ])
92
    //         ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
93
    //
94
    //     $this->assertNull($results);
95
    // }
96
    //
97
    // public function testDeletingModelClearsCache()
98
    // {
99
    //     $author = (new Author)->with('books')->get()->first();
100
    //     $author->delete();
101
    //
102
    //     $results = cache()->tags([
103
    //             'genealabslaravelmodelcachingtestsfixturesauthor',
104
    //             'genealabslaravelmodelcachingtestsfixturesbook'
105
    //         ])
106
    //         ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks');
107
    //
108
    //     $this->assertNull($results);
109
    // }
110
    //
111
    // public function testHasManyRelationshipIsCached()
112
    // {
113
    //     $authors = (new Author)->with('books')->get();
114
    //     $authorIds = implode('_', $authors->pluck('id')->toArray());
115
    //
116
    //     $results = collect(cache()->tags([
117
    //             'genealabslaravelmodelcachingtestsfixturesauthor',
118
    //             'genealabslaravelmodelcachingtestsfixturesbook'
119
    //         ])
120
    //         ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesbooks"));
121
    //
122
    //     $this->assertNotNull($results);
123
    //     $this->assertEmpty($authors->diffAssoc($results));
124
    //     $this->assertNotEmpty($authors);
125
    //     $this->assertNotEmpty($results);
126
    //     $this->assertEquals($authors->count(), $results->count());
127
    // }
128
    //
129
    // public function testBelongsToRelationshipIsCached()
130
    // {
131
    //     $books = (new Book)->with('author')->get();
132
    //     $bookIds = implode('_', $books->pluck('id')->toArray());
133
    //
134
    //     $results = collect(cache()->tags([
135
    //             'genealabslaravelmodelcachingtestsfixturesbook',
136
    //             'genealabslaravelmodelcachingtestsfixturesauthor'
137
    //         ])
138
    //         ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesauthors"));
139
    //
140
    //     $this->assertNotNull($results);
141
    //     $this->assertEmpty($books->diffAssoc($results));
142
    //     $this->assertNotEmpty($books);
143
    //     $this->assertNotEmpty($results);
144
    //     $this->assertEquals($books->count(), $results->count());
145
    // }
146
    //
147
    // public function testBelongsToManyRelationshipIsCached()
148
    // {
149
    //     $books = (new Book)->with('stores')->get();
150
    //     $bookIds = implode('_', $books->pluck('id')->toArray());
151
    //
152
    //     $results = collect(cache()->tags([
153
    //             'genealabslaravelmodelcachingtestsfixturesbook',
154
    //             'genealabslaravelmodelcachingtestsfixturesstore'
155
    //         ])
156
    //         ->get("genealabslaravelmodelcachingtestsfixturesbook_{$bookIds}-genealabslaravelmodelcachingtestsfixturesstores"));
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
    //     $authorIds = implode('_', $authors->pluck('id')->toArray());
169
    //
170
    //     $results = collect(cache()
171
    //         ->tags([
172
    //             'genealabslaravelmodelcachingtestsfixturesauthor',
173
    //             'genealabslaravelmodelcachingtestsfixturesprofile'
174
    //         ])
175
    //         ->get("genealabslaravelmodelcachingtestsfixturesauthor_{$authorIds}-genealabslaravelmodelcachingtestsfixturesprofiles"));
176
    //
177
    //     $this->assertNotNull($results);
178
    //     $this->assertEmpty($authors->diffAssoc($results));
179
    //     $this->assertNotEmpty($authors);
180
    //     $this->assertNotEmpty($results);
181
    //     $this->assertEquals($authors->count(), $results->count());
182
    // }
183
184
    public function testAllModelResultsCreatesCache()
185
    {
186
        $authors = (new Author)->all();
187
        $key = 'genealabslaravelmodelcachingtestsfixturesauthor';
188
        $tags = [
189
            'genealabslaravelmodelcachingtestsfixturesauthor',
190
        ];
191
192
        $cachedResults = cache()->tags($tags)
193
            ->get($key);
194
        $liveResults = (new UncachedAuthor)->all();
195
196
        $this->assertEquals($authors, $cachedResults);
197
        $this->assertEmpty($liveResults->diffAssoc($cachedResults));
198
    }
199
200 View Code Duplication
    public function testAvgModelResultsCreatesCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
218
    public function testChunkModelResultsCreatesCache()
219
    {
220
        $cachedChunks = collect([
221
            'authors' => collect(),
222
            'keys' => collect(),
223
        ]);
224
        $chunkSize = 3;
225
        $tags = [
226
            'genealabslaravelmodelcachingtestsfixturesauthor',
227
            'genealabslaravelmodelcachingtestsfixturesbook',
228
            'genealabslaravelmodelcachingtestsfixturesprofile',
229
        ];
230
        $uncachedChunks = collect();
231
232
        (new Author)->with('books', 'profile')
233
            ->chunk($chunkSize, function ($chunk) use (&$cachedChunks, $chunkSize) {
234
                $offset = '';
235
236
                if ($cachedChunks['authors']->count()) {
237
                    $offsetIncrement = $cachedChunks['authors']->count() * $chunkSize;
238
                    $offset = "-offset_{$offsetIncrement}";
239
                }
240
241
                $cachedChunks['authors']->push($chunk);
242
                $cachedChunks['keys']->push("genealabslaravelmodelcachingtestsfixturesauthor-books-profile{$offset}-limit_3");
243
            });
244
245
        (new UncachedAuthor)->with('books', 'profile')
246
            ->chunk($chunkSize, function ($chunk) use (&$uncachedChunks) {
247
                $uncachedChunks->push($chunk);
248
            });
249
250
        for ($index = 0; $index < $cachedChunks['authors']->count(); $index++) {
251
            $key = $cachedChunks['keys'][$index];
252
            $cachedResults = cache()->tags($tags)
253
                ->get($key);
254
255
            $this->assertEmpty($cachedChunks['authors'][$index]->diffAssoc($cachedResults));
256
            $this->assertEmpty($uncachedChunks[$index]->diffAssoc($cachedResults));
257
        }
258
    }
259
260 View Code Duplication
    public function testCountModelResultsCreatesCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
    {
262
        $authors = (new Author)->with('books', 'profile')
263
            ->count();
264
        $key = 'genealabslaravelmodelcachingtestsfixturesauthor-count';
265
        $tags = [
266
            'genealabslaravelmodelcachingtestsfixturesauthor',
267
        ];
268
269
        $cachedResults = cache()->tags($tags)
270
            ->get($key);
271
        $liveResults = (new UncachedAuthor)->with('books', 'profile')
272
            ->count();
273
274
        $this->assertEquals($authors, $cachedResults);
275
        $this->assertEquals($liveResults, $cachedResults);
276
    }
277
278 View Code Duplication
    public function testCursorModelResultsCreatesCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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));
1 ignored issue
show
Bug introduced by
The method diffAssoc() does not exist on Generator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

292
        $this->assertEmpty($authors->/** @scrutinizer ignore-call */ diffAssoc($cachedResults));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
293
        $this->assertEmpty($liveResults->diffAssoc($cachedResults));
294
    }
295
296
    public function testFindModelResultsCreatesCache()
297
    {
298
        $author = (new Author)->find(1);
299
        $key = 'genealabslaravelmodelcachingtestsfixturesauthor_1';
300
        $tags = [
301
            'genealabslaravelmodelcachingtestsfixturesauthor',
302
        ];
303
304
        $cachedResults = collect()->push(cache()->tags($tags)
305
            ->get($key));
306
        $liveResults = collect()->push((new UncachedAuthor)->find(1));
307
308
        $this->assertEquals($author, $cachedResults->first());
309
        $this->assertEmpty($liveResults->diffAssoc($cachedResults));
310
    }
311
312 View Code Duplication
    public function testGetModelResultsCreatesCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
313
    {
314
        $authors = (new Author)->with('books', 'profile')
315
            ->get();
316
        $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books-profile';
317
        $tags = [
318
            'genealabslaravelmodelcachingtestsfixturesauthor',
319
            'genealabslaravelmodelcachingtestsfixturesbook',
320
            'genealabslaravelmodelcachingtestsfixturesprofile',
321
        ];
322
323
        $cachedResults = cache()->tags($tags)
324
            ->get($key);
325
        $liveResults = (new UncachedAuthor)->with('books', 'profile')
326
            ->get();
327
328
        $this->assertEquals($authors, $cachedResults);
329
        $this->assertEmpty($liveResults->diffAssoc($cachedResults));
330
    }
331
332 View Code Duplication
    public function testMaxModelResultsCreatesCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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');
345
346
        $this->assertEquals($authorId, $cachedResult);
347
        $this->assertEquals($liveResult, $cachedResult);
348
    }
349
350 View Code Duplication
    public function testMinModelResultsCreatesCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
351
    {
352
        $authorId = (new Author)->with('books', 'profile')
353
            ->min('id');
354
        $key = 'genealabslaravelmodelcachingtestsfixturesauthor-min_id';
355
        $tags = [
356
            'genealabslaravelmodelcachingtestsfixturesauthor',
357
        ];
358
359
        $cachedResult = cache()->tags($tags)
360
            ->get($key);
361
        $liveResult = (new UncachedAuthor)->with('books', 'profile')
362
            ->min('id');
363
364
        $this->assertEquals($authorId, $cachedResult);
365
        $this->assertEquals($liveResult, $cachedResult);
366
    }
367
368 View Code Duplication
    public function testPluckModelResultsCreatesCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testSumModelResultsCreatesCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testValueModelResultsCreatesCache()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
    }
427
}
428