Completed
Pull Request — master (#372)
by Mike
31:01 queued 16:07
created

CachedBuilderTest::testHashCollision()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration;
2
3
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
6
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource;
7
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
8
use Illuminate\Support\Str;
9
10
/**
11
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
12
* @SuppressWarnings(PHPMD.TooManyMethods)
13
 */
14
class CachedBuilderTest extends IntegrationTestCase
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: artisan, be, call, seed
Loading history...
15
{
16
    public function testCacheIsEmptyBeforeLoadingModels()
17
    {
18
        $results = $this->cache()->tags([
19
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
20
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"
21
            ])
22
            ->get("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-testing:{$this->testingSqlitePath}testing.sqlite:books");
23
24
        $this->assertNull($results);
25
    }
26
27
    public function testCacheIsNotEmptyAfterLoadingModels()
28
    {
29
        (new Author)->with('books')->get();
30
31
        $results = $this->cache()->tags([
32
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
33
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"
34
            ])
35
            ->get(sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books"));
36
37
        $this->assertNotNull($results);
38
    }
39
40
    public function testCreatingModelClearsCache()
41
    {
42
        (new Author)->with('books')->get();
43
44
        factory(Author::class)->create();
45
46
        $results = $this->cache()->tags([
47
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
48
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"
49
            ])
50
            ->get(sha1(
51
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_" .
52
                "7_8_9_10-genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbooks"
53
            ));
54
55
        $this->assertNull($results);
56
    }
57
58 View Code Duplication
    public function testUpdatingModelClearsCache()
59
    {
60
        $author = (new Author)->with('books')->get()->first();
61
        $author->name = "John Jinglheimer";
62
        $author->save();
63
64
        $results = $this->cache()->tags([
65
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
66
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"
67
            ])
68
            ->get(sha1(
69
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_" .
70
                "7_8_9_10-genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbooks"
71
            ));
72
73
        $this->assertNull($results);
74
    }
75
76 View Code Duplication
    public function testDeletingModelClearsCache()
77
    {
78
        $author = (new Author)->with('books')->get()->first();
79
        $author->delete();
80
81
        $results = $this->cache()->tags([
82
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
83
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"
84
            ])
85
            ->get(sha1(
86
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_" .
87
                "7_8_9_10-genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbooks"
88
            ));
89
90
        $this->assertNull($results);
91
    }
92
93 View Code Duplication
    public function testHasManyRelationshipIsCached()
94
    {
95
        $authors = (new Author)->with('books')->get();
96
97
        $results = collect($this->cache()->tags([
98
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
99
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"
100
            ])
101
            ->get(sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books"))['value']);
102
103
        $this->assertNotNull($results);
104
        $this->assertEmpty($authors->diffKeys($results));
105
        $this->assertNotEmpty($authors);
106
        $this->assertNotEmpty($results);
107
        $this->assertEquals($authors->count(), $results->count());
108
    }
109
110 View Code Duplication
    public function testBelongsToRelationshipIsCached()
111
    {
112
        $books = (new Book)->with('author')->get();
113
114
        $results = collect($this->cache()->tags([
115
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
116
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"
117
            ])
118
            ->get(sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-testing:{$this->testingSqlitePath}testing.sqlite:author"))['value']);
119
120
        $this->assertNotNull($results);
121
        $this->assertEmpty($books->diffKeys($results));
122
        $this->assertNotEmpty($books);
123
        $this->assertNotEmpty($results);
124
        $this->assertEquals($books->count(), $results->count());
125
    }
126
127 View Code Duplication
    public function testBelongsToManyRelationshipIsCached()
128
    {
129
        $books = (new Book)->with('stores')->get();
130
131
        $results = collect($this->cache()->tags([
132
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
133
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore"
134
            ])
135
            ->get(sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-testing:{$this->testingSqlitePath}testing.sqlite:stores"))['value']);
136
137
        $this->assertNotNull($results);
138
        $this->assertEmpty($books->diffKeys($results));
139
        $this->assertNotEmpty($books);
140
        $this->assertNotEmpty($results);
141
        $this->assertEquals($books->count(), $results->count());
142
    }
143
144 View Code Duplication
    public function testHasOneRelationshipIsCached()
145
    {
146
        $authors = (new Author)->with('profile')->get();
147
148
        $results = collect($this->cache()
149
            ->tags([
150
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
151
                "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile"
152
            ])
153
            ->get(sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:profile"))['value']);
154
155
        $this->assertNotNull($results);
156
        $this->assertEmpty($authors->diffKeys($results));
157
        $this->assertNotEmpty($authors);
158
        $this->assertNotEmpty($results);
159
        $this->assertEquals($authors->count(), $results->count());
160
    }
161
162 View Code Duplication
    public function testAvgModelResultsCreatesCache()
163
    {
164
        $authorId = (new Author)->with('books', 'profile')
165
            ->avg('id');
166
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-avg_id");
167
        $tags = [
168
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
169
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
170
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile",
171
        ];
172
173
        $cachedResult = $this->cache()
174
            ->tags($tags)
175
            ->get($key)['value'];
176
        $liveResult = (new UncachedAuthor)->with('books', 'profile')
177
            ->avg('id');
178
179
        $this->assertEquals($authorId, $cachedResult);
180
        $this->assertEquals($liveResult, $cachedResult);
181
    }
182
183
    public function testChunkModelResultsCreatesCache()
184
    {
185
        $chunkedAuthors = [];
186
        $chunkedKeys = [];
187
        $tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"];
188
        (new Author)
189
            ->chunk(3, function ($authors) use (&$chunkedAuthors, &$chunkedKeys) {
190
                $offset = "";
191
192
                if (count($chunkedKeys)) {
193
                    $offset = "-offset_" . (count($chunkedKeys) * 3);
194
                }
195
196
                $key = "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null_orderBy_authors.id_asc{$offset}-limit_3";
197
                array_push($chunkedAuthors, $authors);
198
                array_push($chunkedKeys, $key);
199
            });
200
201
202
        for ($index = 0; $index < count($chunkedAuthors); $index++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
203
            $cachedAuthors = $this
204
                ->cache()
205
                ->tags($tags)
206
                ->get(sha1($chunkedKeys[$index]))['value'];
207
208
            $this->assertEquals(count($chunkedAuthors[$index]), count($cachedAuthors));
209
            $this->assertEquals($chunkedAuthors[$index], $cachedAuthors);
210
        }
211
212
        $this->assertCount(4, $chunkedAuthors);
0 ignored issues
show
Documentation introduced by
$chunkedAuthors is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
213
    }
214
215
    public function testCountModelResultsCreatesCache()
216
    {
217
        $authors = (new Author)
218
            ->with('books', 'profile')
219
            ->count();
220
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-count");
221
        $tags = [
222
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
223
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
224
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile",
225
        ];
226
227
        $cachedResults = $this->cache()->tags($tags)
228
            ->get($key)['value'];
229
        $liveResults = (new UncachedAuthor)
230
            ->with('books', 'profile')
231
            ->count();
232
233
        $this->assertEquals($authors, $cachedResults);
234
        $this->assertEquals($liveResults, $cachedResults);
235
    }
236
237 View Code Duplication
    public function testCountWithStringCreatesCache()
238
    {
239
        $authors = (new Author)
240
            ->with('books', 'profile')
241
            ->count("id");
242
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_id-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-count");
243
        $tags = [
244
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
245
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
246
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile",
247
        ];
248
249
        $cachedResults = $this->cache()->tags($tags)
250
            ->get($key)['value'];
251
        $liveResults = (new UncachedAuthor)
252
            ->with('books', 'profile')
253
            ->count("id");
254
255
        $this->assertEquals($authors, $cachedResults);
256
        $this->assertEquals($liveResults, $cachedResults);
257
    }
258
259
    public function testFirstModelResultsCreatesCache()
260
    {
261
        $author = (new Author)
262
            ->first();
263
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-first");
264
        $tags = [
265
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
266
        ];
267
268
        $cachedResult = $this->cache()->tags($tags)
269
            ->get($key)['value'];
270
271
        $liveResult = (new UncachedAuthor)
272
            ->with('books', 'profile')
273
            ->first();
274
275
        $this->assertEquals($cachedResult->id, $author->id);
276
        $this->assertEquals($liveResult->id, $author->id);
277
    }
278
279 View Code Duplication
    public function testMaxModelResultsCreatesCache()
280
    {
281
        $authorId = (new Author)->with('books', 'profile')
282
            ->max('id');
283
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-max_id");
284
        $tags = [
285
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
286
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
287
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile",
288
        ];
289
290
        $cachedResult = $this->cache()->tags($tags)
291
            ->get($key)['value'];
292
        $liveResult = (new UncachedAuthor)->with('books', 'profile')
293
            ->max('id');
294
295
        $this->assertEquals($authorId, $cachedResult);
296
        $this->assertEquals($liveResult, $cachedResult);
297
    }
298
299 View Code Duplication
    public function testMinModelResultsCreatesCache()
300
    {
301
        $authorId = (new Author)->with('books', 'profile')
302
            ->min('id');
303
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-min_id");
304
        $tags = [
305
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
306
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
307
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile",
308
        ];
309
310
        $cachedResult = $this->cache()->tags($tags)
311
            ->get($key)['value'];
312
        $liveResult = (new UncachedAuthor)->with('books', 'profile')
313
            ->min('id');
314
315
        $this->assertEquals($authorId, $cachedResult);
316
        $this->assertEquals($liveResult, $cachedResult);
317
    }
318
319 View Code Duplication
    public function testPluckModelResultsCreatesCache()
320
    {
321
        $authors = (new Author)->with('books', 'profile')
322
            ->pluck('name', 'id');
323
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_name-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-pluck_name_id");
324
        $tags = [
325
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
326
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
327
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile",
328
        ];
329
330
        $cachedResults = $this->cache()->tags($tags)
331
            ->get($key)['value'];
332
        $liveResults = (new UncachedAuthor)->with('books', 'profile')
333
            ->pluck('name', 'id');
334
335
        $this->assertEmpty($authors->diffKeys($cachedResults));
336
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
337
    }
338
339 View Code Duplication
    public function testSumModelResultsCreatesCache()
340
    {
341
        $authorId = (new Author)->with('books', 'profile')
342
            ->sum('id');
343
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-sum_id");
344
        $tags = [
345
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
346
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
347
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile",
348
        ];
349
350
        $cachedResult = $this->cache()->tags($tags)
351
            ->get($key)['value'];
352
        $liveResult = (new UncachedAuthor)->with('books', 'profile')
353
            ->sum('id');
354
355
        $this->assertEquals($authorId, $cachedResult);
356
        $this->assertEquals($liveResult, $cachedResult);
357
    }
358
359 View Code Duplication
    public function testValueModelResultsCreatesCache()
360
    {
361
        $authorName = (new Author)->with('books', 'profile')
362
            ->value('name');
363
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-value_name");
364
        $tags = [
365
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
366
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
367
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprofile",
368
        ];
369
370
        $cachedResult = $this->cache()->tags($tags)
371
            ->get($key)['value'];
372
        $liveResult = (new UncachedAuthor)->with('books', 'profile')
373
            ->value('name');
374
375
        $this->assertEquals($authorName, $cachedResult);
376
        $this->assertEquals($authorName, $liveResult);
377
    }
378
379
    public function testNestedRelationshipEagerLoading()
380
    {
381
        $authors = collect([(new Author)->with('books.publisher')
382
                ->first()]);
383
384
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-books.publisher-first");
385
        $tags = [
386
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
387
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
388
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturespublisher",
389
        ];
390
391
        $cachedResults = collect([$this->cache()->tags($tags)
392
                ->get($key)['value']]);
393
        $liveResults = collect([(new UncachedAuthor)->with('books.publisher')
394
                ->first()]);
395
396
        $this->assertEmpty($authors->diffKeys($cachedResults));
397
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
398
    }
399
400 View Code Duplication
    public function testLazyLoadedRelationshipResolvesThroughCachedBuilder()
401
    {
402
        $books = (new Author)->first()->books;
403
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull");
404
        $tags = [
405
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
406
        ];
407
408
        $cachedResults = $this->cache()
409
            ->tags($tags)
410
            ->get($key)['value'];
411
        $liveResults = (new UncachedAuthor)->first()->books;
412
413
        $this->assertEmpty($books->diffKeys($cachedResults));
414
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
415
    }
416
417
    public function testLazyLoadingOnResourceIsCached()
418
    {
419
        if (Str::startsWith(app()->version(), "5.4")) {
420
            $this->markTestIncomplete("Resources don't exist in Laravel 5.4.");
421
        }
422
423
        $books = (new AuthorResource((new Author)->first()))->books;
424
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull");
425
        $tags = [
426
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
427
        ];
428
429
        $cachedResults = $this->cache()
430
            ->tags($tags)
431
            ->get($key)['value'];
432
        $liveResults = (new UncachedAuthor)->first()->books;
433
434
        $this->assertEmpty($books->diffKeys($cachedResults));
435
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
436
    }
437
438
    public function testOrderByClauseParsing()
439
    {
440
        $authors = (new Author)->orderBy('name')->get();
441
442
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null_orderBy_name_asc");
443
        $tags = [
444
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
445
        ];
446
447
        $cachedResults = $this->cache()
448
            ->tags($tags)
449
            ->get($key)['value'];
450
        $liveResults = (new UncachedAuthor)->orderBy('name')->get();
451
452
        $this->assertEmpty($authors->diffKeys($cachedResults));
453
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
454
    }
455
456 View Code Duplication
    public function testNestedRelationshipWhereClauseParsing()
457
    {
458
        $authors = (new Author)
459
            ->with('books.publisher')
460
            ->get();
461
462
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-books.publisher");
463
        $tags = [
464
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
465
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
466
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturespublisher",
467
        ];
468
469
        $cachedResults = $this->cache()
470
            ->tags($tags)
471
            ->get($key)['value'];
472
473
        $liveResults = (new UncachedAuthor)->with('books.publisher')
474
            ->get();
475
476
        $this->assertEmpty($authors->diffKeys($cachedResults));
477
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
478
    }
479
480
    public function testExistsRelationshipWhereClauseParsing()
481
    {
482
        $authors = (new Author)->whereHas('books')
483
            ->get();
484
485
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-exists-and_authors.id_=_books.author_id-authors.deleted_at_null");
486
        $tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"];
487
488
        $cachedResults = $this->cache()
489
            ->tags($tags)
490
            ->get($key)['value'];
491
        $liveResults = (new UncachedAuthor)->whereHas('books')
492
            ->get();
493
494
        $this->assertEmpty($authors->diffKeys($cachedResults));
495
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
496
    }
497
498
    public function testDoesntHaveWhereClauseParsing()
499
    {
500
        $authors = (new Author)
501
            ->doesntHave('books')
502
            ->get();
503
504
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-notexists-and_authors.id_=_books.author_id-authors.deleted_at_null");
505
        $tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"];
506
507
        $cachedResults = $this->cache()
508
            ->tags($tags)
509
            ->get($key)['value'];
510
        $liveResults = (new UncachedAuthor)
511
            ->doesntHave('books')
512
            ->get();
513
514
        $this->assertEmpty($authors->diffKeys($cachedResults));
515
        $this->assertEmpty($liveResults->diffKeys($cachedResults));
516
    }
517
518 View Code Duplication
    public function testRelationshipQueriesAreCached()
519
    {
520
        $books = (new Author)
521
            ->first()
522
            ->books()
523
            ->get();
524
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull");
525
        $tags = [
526
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"
527
        ];
528
529
        $cachedResults = $this->cache()
530
            ->tags($tags)
531
            ->get($key)['value'];
532
        $liveResults = (new UncachedAuthor)
533
            ->first()
534
            ->books()
535
            ->get();
536
537
        $this->assertTrue($cachedResults->diffKeys($books)->isEmpty());
538
        $this->assertTrue($liveResults->diffKeys($books)->isEmpty());
539
    }
540
541 View Code Duplication
    public function testRawOrderByWithoutColumnReference()
542
    {
543
        $authors = (new Author)
544
            ->orderByRaw('DATE()')
545
            ->get();
546
547
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null_orderByRaw_date");
548
        $tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"];
549
550
        $cachedResults = $this->cache()
551
            ->tags($tags)
552
            ->get($key)['value'];
553
554
        $liveResults = (new UncachedAuthor)
555
            ->orderByRaw('DATE()')
556
            ->get();
557
558
        $this->assertTrue($cachedResults->diffKeys($authors)->isEmpty());
559
        $this->assertTrue($liveResults->diffKeys($authors)->isEmpty());
560
    }
561
562
    public function testDelete()
563
    {
564
        $author = (new Author)
565
            ->first();
566
        $liveResult = (new UncachedAuthor)
567
            ->first();
568
        $authorId = $author->id;
569
        $liveResultId = $liveResult->id;
570
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-first");
571
        $tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"];
572
573
        $author->delete();
574
        $liveResult->delete();
575
        $cachedResult = $this->cache()
576
            ->tags($tags)
577
            ->get($key)['value']
578
            ?? null;
579
        $deletedAuthor = (new Author)->find($authorId);
580
581
        $this->assertEquals($liveResultId, $authorId);
582
        $this->assertNull($cachedResult);
583
        $this->assertNull($deletedAuthor);
584
    }
585
586 View Code Duplication
    public function testWhereBetweenIdsResults()
587
    {
588
        $books = (new Book)
589
            ->whereBetween('price', [5, 10])
590
            ->get();
591
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-price_between_5_10");
592
        $tags = [
593
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
594
        ];
595
596
        $cachedResults = $this->cache()
597
            ->tags($tags)
598
            ->get($key)['value'];
599
        $liveResults = (new UncachedAuthor)
600
            ->whereBetween('price', [5, 10])
601
            ->get();
602
603
        $this->assertTrue($cachedResults->diffKeys($books)->isEmpty());
604
        $this->assertTrue($liveResults->diffKeys($books)->isEmpty());
605
    }
606
607 View Code Duplication
    public function testWhereBetweenDatesResults()
608
    {
609
        $books = (new Book)
610
            ->whereBetween('created_at', ['2018-01-01', '2018-12-31'])
611
            ->get();
612
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-created_at_between_2018-01-01_2018-12-31");
613
        $tags = [
614
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
615
        ];
616
617
        $cachedResults = $this->cache()
618
            ->tags($tags)
619
            ->get($key)['value'];
620
        $liveResults = (new UncachedAuthor)
621
            ->whereBetween('price', [5, 10])
622
            ->get();
623
624
        $this->assertTrue($cachedResults->diffKeys($books)->isEmpty());
625
        $this->assertTrue($liveResults->diffKeys($books)->isEmpty());
626
    }
627
628 View Code Duplication
    public function testWhereDatesResults()
629
    {
630
        $books = (new Book)
631
            ->whereDate('created_at', '>=', '2018-01-01')
632
            ->get();
633
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-created_at_>=_2018-01-01");
634
        $tags = [
635
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
636
        ];
637
638
        $cachedResults = $this->cache()
639
            ->tags($tags)
640
            ->get($key)['value'];
641
        $liveResults = (new UncachedAuthor)
642
            ->whereBetween('price', [5, 10])
643
            ->get();
644
645
        $this->assertTrue($cachedResults->diffKeys($books)->isEmpty());
646
        $this->assertTrue($liveResults->diffKeys($books)->isEmpty());
647
    }
648
649
    public function testHashCollision()
650
    {
651
        $key1 = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-id_notin_1_2");
652
        $tags1 = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook"];
653
        $books = (new Book)
654
            ->whereNotIn('id', [1, 2])
655
            ->get();
656
        $this->cache()->tags($tags1)->flush();
657
658
        $authors = (new Author)
659
            ->disableCache()
660
            ->get();
661
        $key2 = "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor";
662
        $this->cache()
663
            ->tags($tags1)
664
            ->rememberForever(
665
                $key1,
666
                function () use ($key2, $authors) {
667
                    return [
668
                        'key' => $key2,
669
                        'value' => $authors,
670
                    ];
671
                }
672
            );
673
        $cachedBooks = (new Book)
0 ignored issues
show
Unused Code introduced by
$cachedBooks is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
674
            ->whereNotIn('id', [1, 2])
675
            ->get();
676
        $cachedResults = $this->cache()
677
            ->tags($tags1)
678
            ->get($key1)['value'];
679
680
        $this->assertTrue($cachedResults->keyBy('id')->diffKeys($books->keyBy('id'))->isEmpty());
681
        $this->assertTrue($cachedResults->diff($authors)->isNotEmpty());
682
    }
683
684
    public function testSubsequentDisabledCacheQueriesDoNotCache()
685
    {
686
        (new Author)->disableCache()->get();
687
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor");
688
        $tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor"];
689
        $cachedAuthors1 = $this->cache()
690
            ->tags($tags)
691
            ->get($key)['value']
692
            ?? null;
693
694
        (new Author)->disableCache()->get();
695
        $cachedAuthors2 = $this->cache()
696
            ->tags($tags)
697
            ->get($key)['value']
698
            ?? null;
699
700
        $this->assertEmpty($cachedAuthors1);
701
        $this->assertEmpty($cachedAuthors2);
702
    }
703
704
    public function testInsertInvalidatesCache()
705
    {
706
        $authors = (new Author)
707
            ->get();
708
709
        (new Author)
710
            ->insert([
711
                'name' => 'Test Insert',
712
                'email' => '[email protected]',
713
            ]);
714
        $authorsAfterInsert = (new Author)
715
            ->get();
716
        $uncachedAuthors = (new UncachedAuthor)
717
            ->get();
718
719
        $this->assertCount(10, $authors);
720
        $this->assertCount(11, $authorsAfterInsert);
721
        $this->assertCount(11, $uncachedAuthors);
722
    }
723
724
    public function testUpdateInvalidatesCache()
725
    {
726
        $originalAuthor = (new Author)
727
            ->first();
728
        $author = (new Author)
729
            ->first();
730
731
        $author->update([
732
            "name" => "Updated Name",
733
        ]);
734
        $authorAfterUpdate = (new Author)
735
            ->find($author->id);
736
        $uncachedAuthor = (new UncachedAuthor)
737
            ->find($author->id);
738
739
        $this->assertNotEquals($originalAuthor->name, $authorAfterUpdate->name);
740
        $this->assertEquals("Updated Name", $authorAfterUpdate->name);
741
        $this->assertEquals($authorAfterUpdate->name, $uncachedAuthor->name);
742
    }
743
744
    public function testAttachInvalidatesCache()
745
    {
746
        $book = (new Book)
747
            ->find(1);
748
749
        $book->stores()->attach(1);
750
        $cachedBook = (new Book)
0 ignored issues
show
Unused Code introduced by
$cachedBook is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
751
            ->find(1);
752
753
        $this->assertTrue($book->stores->keyBy('id')->has(1));
754
    }
755
}
756