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

testChunkModelResultsIsNotCached()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 9.2
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration;
2
3
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
5
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6
7
/**
8
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
9
* @SuppressWarnings(PHPMD.TooManyMethods)
10
 */
11
class DisabledCachedBuilderTest 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...
12
{
13
    public function testAvgModelResultsIsNotCached()
14
    {
15
        $authorId = (new Author)
16
            ->with('books', 'profile')
17
            ->disableCache()
18
            ->avg('id');
19
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-avg_id");
20
        $tags = [
21
            'genealabslaravelmodelcachingtestsfixturesauthor',
22
            'genealabslaravelmodelcachingtestsfixturesbook',
23
            'genealabslaravelmodelcachingtestsfixturesprofile',
24
        ];
25
26
        $cachedResult = $this->cache()
27
            ->tags($tags)
28
            ->get($key);
29
        $liveResult = (new UncachedAuthor)
30
            ->with('books', 'profile')
31
            ->avg('id');
32
33
        $this->assertEquals($authorId, $liveResult);
34
        $this->assertNull($cachedResult);
35
    }
36
37
    public function testChunkModelResultsIsNotCached()
38
    {
39
        $cachedChunks = collect([
40
            'authors' => collect(),
41
            'keys' => collect(),
42
        ]);
43
        $chunkSize = 3;
44
        $tags = [
45
            'genealabslaravelmodelcachingtestsfixturesauthor',
46
            'genealabslaravelmodelcachingtestsfixturesbook',
47
            'genealabslaravelmodelcachingtestsfixturesprofile',
48
        ];
49
        $uncachedChunks = collect();
50
51
        $authors = (new Author)->with('books', 'profile')
52
            ->disableCache()
53
            ->chunk($chunkSize, function ($chunk) use (&$cachedChunks, $chunkSize) {
54
                $offset = '';
55
56
                if ($cachedChunks['authors']->count()) {
57
                    $offsetIncrement = $cachedChunks['authors']->count() * $chunkSize;
58
                    $offset = "-offset_{$offsetIncrement}";
59
                }
60
61
                $cachedChunks['authors']->push($chunk);
62
                $cachedChunks['keys']->push(sha1(
63
                    "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile_orderBy_authors.id_asc{$offset}-limit_3"
64
                ));
65
            });
66
67
        $liveResults = (new UncachedAuthor)->with('books', 'profile')
68
            ->chunk($chunkSize, function ($chunk) use (&$uncachedChunks) {
69
                $uncachedChunks->push($chunk);
70
            });
71
72
        for ($index = 0; $index < $cachedChunks['authors']->count(); $index++) {
73
            $key = $cachedChunks['keys'][$index];
74
            $cachedResults = $this->cache()
75
                ->tags($tags)
76
                ->get($key);
77
78
            $this->assertNull($cachedResults);
79
            $this->assertEquals($authors, $liveResults);
80
        }
81
    }
82
83
    public function testCountModelResultsIsNotCached()
84
    {
85
        $authors = (new Author)
86
            ->with('books', 'profile')
87
            ->disableCache()
88
            ->count();
89
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-count");
90
        $tags = [
91
            'genealabslaravelmodelcachingtestsfixturesauthor',
92
            'genealabslaravelmodelcachingtestsfixturesbook',
93
            'genealabslaravelmodelcachingtestsfixturesprofile',
94
        ];
95
96
        $cachedResults = $this->cache()
97
            ->tags($tags)
98
            ->get($key);
99
        $liveResults = (new UncachedAuthor)
100
            ->with('books', 'profile')
101
            ->count();
102
103
        $this->assertEquals($authors, $liveResults);
104
        $this->assertNull($cachedResults);
105
    }
106
107
    public function testCursorModelResultsIsNotCached()
108
    {
109
        $authors = (new Author)
110
            ->with('books', 'profile')
111
            ->disableCache()
112
            ->cursor();
113
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-cursor");
114
        $tags = [
115
            'genealabslaravelmodelcachingtestsfixturesauthor',
116
            'genealabslaravelmodelcachingtestsfixturesbook',
117
            'genealabslaravelmodelcachingtestsfixturesprofile',
118
        ];
119
120
        $cachedResults = $this->cache()
121
            ->tags($tags)
122
            ->get($key);
123
        $liveResults = collect(
124
            (new UncachedAuthor)
125
                ->with('books', 'profile')
126
                ->cursor()
127
        );
128
129
        $this->assertEmpty($liveResults->diffKeys($authors));
130
        $this->assertNull($cachedResults);
131
    }
132
133
    public function testFindModelResultsIsNotCached()
134
    {
135
        $author = (new Author)
136
            ->with('books')
137
            ->disableCache()
138
            ->find(1);
139
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_1");
140
        $tags = [
141
            'genealabslaravelmodelcachingtestsfixturesauthor',
142
        ];
143
144
        $cachedResult = $this->cache()
145
            ->tags($tags)
146
            ->get($key);
147
        $liveResult = (new UncachedAuthor)
148
            ->find(1);
149
150
        $this->assertEquals($liveResult->name, $author->name);
151
        $this->assertNull($cachedResult);
152
    }
153
154 View Code Duplication
    public function testGetModelResultsIsNotCached()
155
    {
156
        $authors = (new Author)
157
            ->with('books', 'profile')
158
            ->disableCache()
159
            ->get();
160
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile");
161
        $tags = [
162
            'genealabslaravelmodelcachingtestsfixturesauthor',
163
            'genealabslaravelmodelcachingtestsfixturesbook',
164
            'genealabslaravelmodelcachingtestsfixturesprofile',
165
        ];
166
167
        $cachedResults = $this->cache()
168
            ->tags($tags)
169
            ->get($key);
170
        $liveResults = (new UncachedAuthor)
171
            ->with('books', 'profile')
172
            ->get();
173
174
        $this->assertEmpty($liveResults->diffKeys($authors));
175
        $this->assertNull($cachedResults);
176
    }
177
178
    public function testMaxModelResultsIsNotCached()
179
    {
180
        $authorId = (new Author)
181
            ->with('books', 'profile')
182
            ->disableCache()
183
            ->max('id');
184
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-max_id");
185
        $tags = [
186
            'genealabslaravelmodelcachingtestsfixturesauthor',
187
            'genealabslaravelmodelcachingtestsfixturesbook',
188
            'genealabslaravelmodelcachingtestsfixturesprofile',
189
        ];
190
191
        $cachedResult = $this->cache()
192
            ->tags($tags)
193
            ->get($key);
194
        $liveResult = (new UncachedAuthor)
195
            ->with('books', 'profile')
196
            ->max('id');
197
198
        $this->assertEquals($authorId, $liveResult);
199
        $this->assertNull($cachedResult);
200
    }
201
202
    public function testMinModelResultsIsNotCached()
203
    {
204
        $authorId = (new Author)
205
            ->with('books', 'profile')
206
            ->disableCache()
207
            ->min('id');
208
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-min_id");
209
        $tags = [
210
            'genealabslaravelmodelcachingtestsfixturesauthor',
211
            'genealabslaravelmodelcachingtestsfixturesbook',
212
            'genealabslaravelmodelcachingtestsfixturesprofile',
213
        ];
214
215
        $cachedResult = $this->cache()
216
            ->tags($tags)
217
            ->get($key);
218
        $liveResult = (new UncachedAuthor)
219
            ->with('books', 'profile')
220
            ->min('id');
221
222
        $this->assertEquals($authorId, $liveResult);
223
        $this->assertNull($cachedResult);
224
    }
225
226
    public function testPluckModelResultsIsNotCached()
227
    {
228
        $authors = (new Author)
229
            ->with('books', 'profile')
230
            ->disableCache()
231
            ->pluck('name', 'id');
232
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_name-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-pluck_name_id");
233
        $tags = [
234
            'genealabslaravelmodelcachingtestsfixturesauthor',
235
            'genealabslaravelmodelcachingtestsfixturesbook',
236
            'genealabslaravelmodelcachingtestsfixturesprofile',
237
        ];
238
239
        $cachedResults = $this->cache()
240
            ->tags($tags)
241
            ->get($key);
242
        $liveResults = (new UncachedAuthor)
243
            ->with('books', 'profile')
244
            ->pluck('name', 'id');
245
246
        $this->assertEmpty($liveResults->diffKeys($authors));
247
        $this->assertNull($cachedResults);
248
    }
249
250
    public function testSumModelResultsIsNotCached()
251
    {
252
        $authorId = (new Author)
253
            ->with('books', 'profile')
254
            ->disableCache()
255
            ->sum('id');
256
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-sum_id");
257
        $tags = [
258
            'genealabslaravelmodelcachingtestsfixturesauthor',
259
            'genealabslaravelmodelcachingtestsfixturesbook',
260
            'genealabslaravelmodelcachingtestsfixturesprofile',
261
        ];
262
263
        $cachedResult = $this->cache()
264
            ->tags($tags)
265
            ->get($key);
266
        $liveResult = (new UncachedAuthor)
267
            ->with('books', 'profile')
268
            ->sum('id');
269
270
        $this->assertEquals($authorId, $liveResult);
271
        $this->assertNull($cachedResult);
272
    }
273
274
    public function testValueModelResultsIsNotCached()
275
    {
276
        $author = (new Author)
277
            ->with('books', 'profile')
278
            ->disableCache()
279
            ->value('name');
280
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_name-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile-first");
281
        $tags = [
282
            'genealabslaravelmodelcachingtestsfixturesauthor',
283
            'genealabslaravelmodelcachingtestsfixturesbook',
284
            'genealabslaravelmodelcachingtestsfixturesprofile',
285
        ];
286
287
        $cachedResult = $this->cache()
288
            ->tags($tags)
289
            ->get($key);
290
291
        $liveResult = (new UncachedAuthor)
292
            ->with('books', 'profile')
293
            ->value('name');
294
295
        $this->assertEquals($author, $liveResult);
296
        $this->assertNull($cachedResult);
297
    }
298
299
    public function testPaginationIsCached()
300
    {
301
        $authors = (new Author)
302
            ->disableCache()
303
            ->paginate(3);
304
305
        $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1");
306
        $tags = [
307
            "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
308
        ];
309
310
        $cachedResults = $this->cache()
311
            ->tags($tags)
312
            ->get($key)['value']
313
            ?? null;
314
        $liveResults = (new UncachedAuthor)
315
            ->paginate(3);
316
317
        $this->assertNull($cachedResults);
318
        $this->assertEquals($liveResults->toArray(), $authors->toArray());
319
    }
320
}
321