-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProxyTest.php
More file actions
975 lines (725 loc) · 35.3 KB
/
ProxyTest.php
File metadata and controls
975 lines (725 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
<?php
namespace Tests;
use Carbon\CarbonInterval;
use Illuminate\Cache\Repository as CacheRepository;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Laragear\CacheQuery\Cache;
use Laragear\CacheQuery\Proxy;
use Mockery;
use Orchestra\Testbench\Attributes\WithMigration;
use function floor;
use function max;
use function method_exists;
use function now;
use function today;
#[WithMigration]
class ProxyTest extends TestCase
{
use WithFaker;
protected function setUp(): void
{
$this->afterApplicationCreated(function () {
$this->app->make('db')->table('users')->insert(Collection::times(10, fn () => [
'email' => $this->faker->freeEmail,
'name' => $this->faker->name,
'password' => 'password',
'email_verified_at' => today(),
])->toArray());
$this->app->make('db')->table('posts')->insert(Collection::times(6, fn ($i) => [
'title' => $this->faker->text(20),
'user_id' => (int) floor(max(1, $i / 2)),
])->toArray());
if (method_exists($this, 'withoutDefer')) {
$this->withoutDefer();
}
});
parent::setUp();
}
protected function defineDatabaseMigrations(): void
{
$this->loadLaravelMigrations();
$this->app->make('db.schema')->create('posts', static function (Blueprint $table): void {
$table->id();
$table->string('title');
$table->foreignIdFor(User::class);
$table->timestamps();
});
$this->app->make('db.schema')->create('comments', static function (Blueprint $table): void {
$table->id();
$table->integer('likes')->default(0);
$table->string('body');
$table->foreignIdFor(Post::class);
$table->timestamps();
});
}
public function test_caches_base_query_into_default_store(): void
{
$get = $this->app->make('db')->table('users')->cache()->where('id', 1)->get();
static::assertInstanceOf(Collection::class, $get);
static::assertCount(1, $get);
$cached = $this->app->make('cache')->store()->get('cache-query|X/UPpOGQDQSgAtjm14OWzw');
static::assertEquals(Collection::make($cached), $get);
}
public function test_caches_eloquent_query_into_default_store(): void
{
$first = User::query()->cache()->where('id', 1)->first();
static::assertNotNull($this->app->make('cache')->store()->get('cache-query|fj8Xyz4K1Zh0tdAamPbG1A'));
User::query()->whereKey(1)->delete();
$second = User::query()->cache()->where('id', 1)->first();
static::assertNotNull($second);
static::assertEquals($first, $second);
}
public function test_caches_base_query_for_sixty_seconds(): void
{
$hash = 'cache-query|X/UPpOGQDQSgAtjm14OWzw';
$this->freezeSecond();
$this->app->make('db')->table('users')->cache()->where('id', 1)->get();
static::assertNotNull($this->app->make('cache')->store()->get($hash));
$this->travelTo(now()->addSeconds(61));
static::assertNull($this->app->make('cache')->store()->get($hash));
}
public function test_caches_eloquent_query_for_sixty_seconds(): void
{
$hash = 'cache-query|X/UPpOGQDQSgAtjm14OWzw';
$this->freezeSecond();
User::query()->cache()->where('id', 1)->get();
static::assertNotNull($this->app->make('cache')->store()->get($hash));
$this->travelTo(now()->addSeconds(61));
static::assertNull($this->app->make('cache')->store()->get($hash));
}
public function test_cached_base_query_returns_cached_results_from_same_query(): void
{
$first = $this->app->make('db')->table('users')->cache()->where('id', 1)->first();
$this->app->make('db')->table('users')->where('id', 1)->delete();
$second = $this->app->make('db')->table('users')->cache()->where('id', 1)->first();
static::assertEquals($first, $second);
}
public function test_cached_eloquent_query_returns_cached_results_from_same_query(): void
{
$first = User::query()->cache()->where('id', 1)->first();
User::query()->where('id', 1)->delete();
$second = User::query()->cache()->where('id', 1)->first();
static::assertEquals($first, $second);
}
public function test_cached_base_query_stores_empty_array_and_null_results(): void
{
$hash = 'cache-query|6SHtUJfPv2GbKc4ikp7cLQ';
$null = $this->app->make('db')->table('users')->cache()->where('id', 11)->first();
static::assertNull($null);
static::assertIsArray($this->app->make('cache')->store()->get($hash));
static::assertEmpty($this->app->make('cache')->store()->get($hash));
static::assertTrue($this->app->make('cache')->store()->has($hash));
}
public function test_cached_eloquent_query_stores_empty_array_and_null_results(): void
{
$hash = 'cache-query|6SHtUJfPv2GbKc4ikp7cLQ';
$null = User::query()->cache()->where('id', 11)->first();
static::assertNull($null);
static::assertIsArray($this->app->make('cache')->store()->get($hash));
static::assertEmpty($this->app->make('cache')->store()->get($hash));
static::assertTrue($this->app->make('cache')->store()->has($hash));
}
public function test_cached_base_query_does_not_stores_empty_array_and_null_results(): void
{
$hash = 'cache-query|6SHtUJfPv2GbKc4ikp7cLQ';
$null = $this->app->make('db')->table('users')->cache(Cache::for(30)->exceptEmpty())->where('id', 11)->first();
static::assertNull($null);
static::assertFalse($this->app->make('cache')->store()->has($hash));
}
public function test_cached_eloquent_query_does_not_store_empty_array_and_null_results(): void
{
$hash = 'cache-query|6SHtUJfPv2GbKc4ikp7cLQ';
$null = User::query()->cache(Cache::for(30)->exceptEmpty())->where('id', 11)->first();
static::assertNull($null);
static::assertFalse($this->app->make('cache')->store()->has($hash));
}
public function test_cached_base_query_doesnt_intercepts_manually_cached_null_values(): void
{
$this->app->make('db')->table('users')->insert([
'email' => $this->faker->freeEmail,
'name' => $this->faker->name,
'password' => 'password',
'email_verified_at' => today(),
]);
$hash = 'cache-query|k7FVGieZVUzWvOK44zPFeg';
$this->app->make('cache')->store()->put($hash, null);
$notNull = $this->app->make('db')->table('users')->cache()->where('id', 11)->first();
static::assertEquals('11', $notNull->id);
}
public function test_cached_eloquent_query_doesnt_intercepts_manually_cached_null_values(): void
{
User::query()->insert([
'email' => $this->faker->freeEmail,
'name' => $this->faker->name,
'password' => 'password',
'email_verified_at' => today(),
]);
$hash = 'cache-query|k7FVGieZVUzWvOK44zPFeg';
$this->app->make('cache')->store()->put($hash, null);
$notNull = User::query()->cache()->where('id', 11)->first();
static::assertSame(11, $notNull->id);
}
public function test_cached_base_query_hash_differs_when_columns_are_different(): void
{
$this->app->make('db')->table('users')->cache()->where('id', 1)->first(['name']);
$this->app->make('db')->table('users')->where('id', 1)->delete();
$second = $this->app->make('db')->table('users')->cache()->where('id', 1)->first(['email']);
static::assertNull($second);
}
public function test_cached_eloquent_query_hash_differs_when_columns_are_different(): void
{
User::query()->cache()->where('id', 1)->first(['name']);
User::query()->where('id', 1)->delete();
$second = User::query()->cache()->where('id', 1)->first(['email']);
static::assertNull($second);
}
public function test_cached_base_query_hash_is_commutative(): void
{
$this->app->make('config')->set('cache-query.commutative', true);
$first = $this->app->make('db')->table('users')->cache()->whereNotNull('name')->where('id', 1)->first();
$this->app->make('db')->table('users')->where('id', 1)->delete();
$second = $this->app->make('db')->table('users')->cache()->where('id', 1)->whereNotNull('name')->first();
static::assertEquals($first, $second);
}
public function test_cached_base_query_works_as_before_last_method_with_different_columns(): void
{
$this->app->make('db')->table('users')->where('id', 1)->cache()->first(['name']);
$this->app->make('db')->table('users')->where('id', 1)->delete();
$second = $this->app->make('db')->table('users')->where('id', 1)->cache()->first(['email']);
static::assertNull($second);
}
public function test_cached_eloquent_query_works_as_before_last_method_with_different_columns(): void
{
User::query()->where('id', 1)->cache()->first(['name']);
User::query()->where('id', 1)->delete();
$second = User::query()->where('id', 1)->cache()->first(['email']);
static::assertNull($second);
}
public function test_cached_base_query_hash_differs_when_pagination_changes(): void
{
$first = $this->app->make('db')->table('users')->cache()->paginate(perPage: 1, page: 1);
$second = $this->app->make('db')->table('users')->cache()->paginate(perPage: 1, page: 2);
static::assertSame(1, $first->firstItem());
static::assertSame(2, $second->firstItem());
}
public function test_cached_eloquent_query_hash_differs_when_pagination_changes(): void
{
$first = User::query()->cache()->paginate(perPage: 1, page: 1);
$second = User::query()->cache()->paginate(perPage: 1, page: 2);
static::assertSame(1, $first->firstItem());
static::assertSame(2, $second->firstItem());
}
public function test_cached_base_query_hash_differs_when_pagination_changes_through_instance(): void
{
$this->instance('request', Request::create('/', 'GET', ['page' => 1]));
$first = $this->app->make('db')->table('users')->cache()->paginate(perPage: 1);
$this->instance('request', Request::create('/', 'GET', ['page' => 2]));
$second = $this->app->make('db')->table('users')->cache()->paginate(perPage: 1);
static::assertSame(1, $first->firstItem());
static::assertSame(2, $second->firstItem());
}
public function test_cached_eloquent_query_hash_differs_when_pagination_changes_through_instance(): void
{
$this->instance('request', Request::create('/', 'GET', ['page' => 1]));
$first = User::query()->cache()->paginate(perPage: 1);
$this->instance('request', Request::create('/', 'GET', ['page' => 2]));
$second = User::query()->cache()->paginate(perPage: 1);
static::assertSame(1, $first->firstItem());
static::assertSame(2, $second->firstItem());
}
public function test_caches_pagination_with_group_by(): void
{
User::query()->delete();
foreach (['first@bogus.com', 'second@bogus.com'] as $email) {
User::forceCreate([
'email' => $email,
'name' => 'foo',
'password' => 'password',
'email_verified_at' => today(),
]);
}
User::query()->cache()->groupBy('name')->paginate(perPage: 1, page: 1);
User::query()->cache()->groupBy('name')->paginate(perPage: 1, page: 2);
User::query()->delete();
$this->assertDatabaseEmpty('users');
$first = User::query()->cache()->groupBy('name')->paginate(perPage: 1, page: 1);
$second = User::query()->cache()->groupBy('name')->paginate(perPage: 1, page: 2);
static::assertCount(1, $first->items());
static::assertSame(1, $first->total());
static::assertSame(1, $first->firstItem());
static::assertEmpty($second);
}
public function test_uses_custom_time_to_live(): void
{
$hash = 'cache-query|30250dGAv64n2ySOIxuL+g';
$seconds = 120;
$now = now()->addMinute();
$interval = $now->diffAsCarbonInterval(now());
$repository = $this->mock(Repository::class);
$repository->expects('getMultiple')->with([$hash, ''])->times(4)->andReturn(['' => null, $hash => null]);
$repository->allows('getStore')->never();
$repository->expects('put')->with($hash, Mockery::type('array'), null);
$repository->expects('put')->with($hash, Mockery::type('array'), $seconds);
$repository->expects('put')->with($hash, Mockery::type('array'), $now);
$repository->expects('put')->with($hash, Mockery::type('array'), $interval);
$this->mock('cache')->allows('store')->with(null)->andReturn($repository);
$this->app->make('db')->table('users')->cache(null)->first();
$this->app->make('db')->table('users')->cache($seconds)->first();
$this->app->make('db')->table('users')->cache($now)->first();
$this->app->make('db')->table('users')->cache($interval)->first();
}
public function test_saves_user_key_with_real_computed_keys_list(): void
{
$this->travelTo(now());
$this->app->make('db')->table('users')->cache(Cache::for(60)->as('foo'))->first();
static::assertTrue($this->app->make('cache')->has('cache-query|30250dGAv64n2ySOIxuL+g'));
static::assertSame(
['list' => ['cache-query|30250dGAv64n2ySOIxuL+g'], 'expires_at' => now()->addMinute()->getTimestamp()],
$this->app->make('cache')->get('cache-query|foo')
);
}
public function test_first_query_takes_precedence_over_second_query_with_different_key(): void
{
$this->app->make('db')->table('users')->cache(Cache::for(60)->as('foo'))->first();
$this->app->make('db')->table('users')->cache(Cache::for(60)->as('bar'))->first();
static::assertTrue($this->app->make('cache')->has('cache-query|foo'));
static::assertFalse($this->app->make('cache')->has('cache-query|bar'));
}
public function test_largest_ttl_key_takes_precedence(): void
{
$this->travelTo(now()->startOfSecond());
$this->app->make('db')->table('users')->where('id', 1)->cache(Cache::for(120)->as('foo'))->first();
$this->app->make('db')->table('users')->where('id', 1)->cache(Cache::for(30)->as('foo'))->first();
$this->app->make('db')->table('users')->where('id', 2)->cache(Cache::for(now()->addSeconds(120))->as('bar'))->first();
$this->app->make('db')->table('users')->where('id', 2)->cache(Cache::for(now()->addSeconds(30))->as('bar'))->first();
$this->app->make('db')->table('users')->where('id', 4)->cache(Cache::for(null)->as('quz'))->first();
$this->app->make('db')->table('users')->where('id', 4)->cache(Cache::for(30)->as('quz'))->first();
$this->travelTo(now()->addMinute());
static::assertTrue($this->app->make('cache')->has('cache-query|foo'));
static::assertTrue($this->app->make('cache')->has('cache-query|bar'));
static::assertTrue($this->app->make('cache')->has('cache-query|quz'));
$this->travelTo(now()->addMinute()->subSecond());
static::assertTrue($this->app->make('cache')->has('cache-query|foo'));
static::assertTrue($this->app->make('cache')->has('cache-query|bar'));
static::assertTrue($this->app->make('cache')->has('cache-query|quz'));
$this->travelTo(now()->addSeconds(2));
static::assertFalse($this->app->make('cache')->has('cache-query|foo'));
static::assertFalse($this->app->make('cache')->has('cache-query|bar'));
static::assertTrue($this->app->make('cache')->has('cache-query|quz'));
}
public function test_regenerates_cache_using_regen_when(): void
{
$this->app->make('db')->table('users')->where('id', 1)->cache()->first();
$this->app->make('db')->table('users')->where('id', 1)->update(['name' => 'test']);
$result = $this->app->make('db')
->table('users')
->where('id', 1)
->cache(Cache::for(60)->regenWhen(false))
->first();
static::assertNotSame('test', $result->name);
$result = $this->app->make('db')
->table('users')
->where('id', 1)
->cache(Cache::for(60)->regenWhen(true))
->first();
static::assertSame('test', $result->name);
}
public function test_regenerates_cache_using_regen_unless(): void
{
$this->app->make('db')->table('users')->where('id', 1)->cache()->first();
$this->app->make('db')->table('users')->where('id', 1)->update(['name' => 'test']);
$result = $this->app->make('db')
->table('users')
->where('id', 1)
->cache(Cache::for(60)->regenUnless(true))
->first();
static::assertNotSame('test', $result->name);
$result = $this->app->make('db')
->table('users')
->where('id', 1)
->cache(Cache::for(60)->regenUnless(false))
->first();
static::assertSame('test', $result->name);
}
public function test_uses_db_flexible_caching_when_using_ttl_as_array_of_values(): void
{
if (! method_exists(CacheRepository::class, 'flexible')) {
$this->markTestSkipped('Cannot test flexible caching if repository does not implements it.');
}
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
$repository = $this->mock(CacheRepository::class);
$repository->expects('put')->never();
$repository->expects('flexible')->with($hash, [5, 300], Mockery::type('\Closure'), null)->once();
$repository->expects('getMultiple')->never();
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
$this->app->make('db')->table('users')->where('id', 1)->cache([5, 300])->first();
}
public function test_doesnt_check_if_the_flexible_array_is_well_formed(): void
{
if (! method_exists(CacheRepository::class, 'flexible')) {
$this->markTestSkipped('Cannot test flexible caching if repository does not implements it.');
}
$malformed = [];
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
$repository = $this->mock(CacheRepository::class);
$repository->expects('put')->never();
$repository->expects('flexible')->with($hash, $malformed, Mockery::type('\Closure'), null)->once();
$repository->expects('getMultiple')->never();
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
$this->app->make('db')->table('users')->where('id', 1)->cache($malformed)->first();
}
public function test_uses_date_interval_with_user_key(): void
{
$this->freezeSecond();
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
$interval = CarbonInterval::make(60, 'seconds');
$repository = $this->mock(Repository::class);
$repository->expects('flexible')->never();
$repository->expects('put')->with($hash, Mockery::type('array'), $interval)->once();
$repository->expects('put')->with(
'cache-query|some-key',
['list' => [$hash], 'expires_at' => now()->add($interval)->getTimestamp()],
$interval
)->once();
$repository->expects('getMultiple')
->with([$hash, 'cache-query|some-key'])
->times(1)
->andReturn(['cache-query|some-key' => null, $hash => null]);
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
$this->app->make('db')
->table('users')
->where('id', 1)
->cache(fn ($cache) => $cache->ttl($interval)->as('some-key'))->first();
}
public function test_uses_custom_store(): void
{
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
$repository = $this->mock(Repository::class);
$repository->expects('flexible')->never();
$repository->expects('put')->with($hash, Mockery::type('array'), 60)->once();
$repository->expects('getMultiple')->with([$hash, ''])->times(1)->andReturn(['' => null, $hash => null]);
$this->mock('cache')->expects('store')->with('test-store')->andReturn($repository);
$this->app->make('db')
->table('users')
->where('id', 1)
->cache(fn ($cache) => $cache->store('test-store'))->first();
}
public function test_uses_eloquent_flexible_caching_when_using_ttl_as_array_of_values(): void
{
if (! method_exists(CacheRepository::class, 'flexible')) {
$this->markTestSkipped('Cannot test flexible caching if repository does not implements it.');
}
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
$repository = $this->mock(CacheRepository::class);
$repository->expects('put')->never();
$repository->expects('flexible')->with($hash, [5, 300], Mockery::type('\Closure'), null)->once();
$repository->expects('getMultiple')->never();
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
User::where('id', 1)->cache([5, 300])->first();
}
public function test_uses_eloquent_flexible_caching_with_lock_arguments(): void
{
if (! method_exists(CacheRepository::class, 'flexible')) {
$this->markTestSkipped('Cannot test flexible caching if repository does not implements it.');
}
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
$repository = $this->mock(CacheRepository::class);
$repository->expects('put')->never();
$repository->expects('flexible')->with($hash, [5, 300], Mockery::type('\Closure'), ['test', 10])->once();
$repository->expects('getMultiple')->never();
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
User::where('id', 1)->cache(Cache::flexible(5, 300, ['test', 10]))->first();
}
public function test_flexible_cache_uses_user_key(): void
{
$cached = User::query()->cache(Cache::for([5, 300])->as('foo'))->with('posts', function ($posts) {
$posts->whereKey(2);
})->whereKey(1)->first();
User::query()->whereKey(1)->delete();
Post::query()->whereKey(2)->delete();
$renewed = User::query()->cache(Cache::for([5, 300])->as('foo'))->with('posts', function ($posts) {
$posts->whereKey(2);
})->whereKey(1)->first();
static::assertTrue($cached->is($renewed));
static::assertCount(1, $renewed->posts);
}
public function test_doesnt_uses_flexible_caching_if_repository_is_not_flexible(): void
{
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
$repository = $this->mock(Repository::class);
$repository->expects('flexible')->never();
$repository->expects('put')->with($hash, Mockery::type('array'), [5, 300])->once();
$repository->expects('getMultiple')->with([$hash, ''])->times(1)->andReturn(['' => null, $hash => null]);
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
$this->app->make('db')->table('users')->where('id', 1)->cache([5, 300])->first();
}
public function test_different_queries_with_same_key_add_to_same_list(): void
{
$this->app->make('db')->table('users')->cache(Cache::for(null)->as('foo'))->where('id', 1)->first();
$this->app->make('db')->table('users')->cache(Cache::for(null)->as('foo'))->where('id', 2)->first();
static::assertTrue($this->app->make('cache')->has('cache-query|fj8Xyz4K1Zh0tdAamPbG1A'));
static::assertTrue($this->app->make('cache')->has('cache-query|u7YzPIzZNGNu7Dkr/kx4Iw'));
static::assertSame(
[
'list' => ['cache-query|fj8Xyz4K1Zh0tdAamPbG1A', 'cache-query|u7YzPIzZNGNu7Dkr/kx4Iw'],
'expires_at' => 'never',
],
$this->app->make('cache')->get('cache-query|foo')
);
}
public function test_cached_eloquent_query_is_aware_of_eager_loaded_list(): void
{
$cached = User::query()->cache()->with('posts')->whereKey(1)->first();
static::assertNotEquals($cached, User::query()->with('posts.comments')->cache()->whereKey(1)->first());
static::assertTrue(User::query()->with('posts')->cache()->whereKey(1)->first()->relationLoaded('posts'));
$cached = User::query()->cache()->with('posts')->whereKey(1)->first();
static::assertEquals($cached, User::query()->with('posts')->whereKey(1)->first());
static::assertTrue($cached->relationLoaded('posts'));
}
public function test_caches_eager_loaded_query(): void
{
$cached = User::query()->cache()->with('posts', function ($posts) {
$posts->whereKey(2);
})->whereKey(1)->first();
User::query()->whereKey(1)->delete();
Post::query()->whereKey(2)->delete();
$renewed = User::query()->cache()->with('posts', function ($posts) {
$posts->whereKey(2);
})->whereKey(1)->first();
static::assertTrue($cached->is($renewed));
static::assertCount(1, $renewed->posts);
}
public function test_caches_eager_loaded_query_with_user_key(): void
{
$cached = User::query()->cache(Cache::for(60)->as('foo'))->with('posts', function ($posts) {
$posts->whereKey(2);
})->whereKey(1)->first();
User::query()->whereKey(1)->delete();
Post::query()->whereKey(2)->delete();
$renewed = User::query()->cache(Cache::for(60)->as('foo'))->with('posts', function ($posts) {
$posts->whereKey(2);
})->whereKey(1)->first();
static::assertTrue($cached->is($renewed));
static::assertCount(1, $renewed->posts);
}
public function test_cached_eager_loaded_query_with_user_key_saves_computed_query_keys_list(): void
{
User::query()->cache(Cache::for('ever')->as('foo'))->with('posts', function ($posts) {
$posts->whereKey(2);
})->whereKey(1)->first();
static::assertSame(
[
'list' => ['cache-query|O18ompNwDpTOa5rNZczCSw', 'cache-query|O18ompNwDpTOa5rNZczCSw.NF0RBjEJ/bDl95d8ryoKeg'],
'expires_at' => 'never',
],
$this->app->make('cache')->get('cache-query|foo')
);
}
public function test_overrides_cached_eager_load_query_with_parent_user_keys(): void
{
User::query()->cache(Cache::for('ever')->as('foo'))->with('posts', function ($posts) {
$posts->whereKey(2)->cache(Cache::for('ever')->as('bar'));
})->whereKey(1)->first();
static::assertSame(
[
'list' => ['cache-query|O18ompNwDpTOa5rNZczCSw', 'cache-query|O18ompNwDpTOa5rNZczCSw.NF0RBjEJ/bDl95d8ryoKeg'],
'expires_at' => 'never',
],
$this->app->make('cache')->get('cache-query|foo')
);
static::assertFalse($this->app->make('cache')->has('cache-query|bar'));
static::assertFalse($this->app->make('cache')->has('cache-query|foo.bar'));
}
public function test_caches_deep_eager_loaded_query(): void
{
$this->app->make('db')->table('comments')->insert(['likes' => 1, 'body' => 'test', 'post_id' => 2]);
$cached = User::query()->cache()->with('posts', function ($posts) {
$posts->whereKey(2)->with('comments');
})->whereKey(1)->first();
User::query()->whereKey(1)->delete();
Post::query()->whereKey(2)->delete();
Comment::query()->whereKey(1)->delete();
$renewed = User::query()->cache()->with('posts', function ($posts) {
$posts->whereKey(2)->with('comments');
})->whereKey(1)->first();
static::assertTrue($cached->is($renewed));
static::assertCount(1, $renewed->posts);
static::assertCount(1, $renewed->posts->first()->comments);
}
public function test_cached_deep_eager_loaded_query_with_user_key_saves_computed_query_keys_list(): void
{
User::query()->cache(Cache::for('ever')->as('foo'))->with('posts', function ($posts) {
$posts->whereKey(2)->with('comments');
})->whereKey(1)->first();
static::assertSame(
[
'list' => [
'cache-query|O18ompNwDpTOa5rNZczCSw',
'cache-query|O18ompNwDpTOa5rNZczCSw.NF0RBjEJ/bDl95d8ryoKeg',
'cache-query|O18ompNwDpTOa5rNZczCSw.NF0RBjEJ/bDl95d8ryoKeg.ULZsLi343YS0xbuO0VteEA',
],
'expires_at' => 'never',
],
$this->app->make('cache')->get('cache-query|foo')
);
}
public function test_doesnt_caches_eager_loaded_query(): void
{
$this->app->make('db')->table('comments')->insert(['likes' => 1, 'body' => 'test', 'post_id' => 2]);
$cached = User::query()->cache(Cache::for(60)->exceptNested())->with('posts', function ($posts) {
$posts->whereKey(2)->with('comments');
})->whereKey(1)->first();
User::query()->whereKey(1)->delete();
Post::query()->whereKey(2)->delete();
Comment::query()->whereKey(1)->delete();
$renewed = User::query()->cache()->with('posts', function ($posts) {
$posts->whereKey(2)->with('comments');
})->whereKey(1)->first();
static::assertTrue($cached->is($renewed));
static::assertEmpty($renewed->posts);
}
public function test_caches_above_one_level_deep_eager_load_relation_query(): void
{
User::query()->with('posts', function ($posts) {
$posts->whereKey(2)->cache(Cache::for('ever')->as('foo'))->with('comments');
})->whereKey(1)->first();
static::assertSame(
[
'list' => [
'cache-query|NF0RBjEJ/bDl95d8ryoKeg',
'cache-query|NF0RBjEJ/bDl95d8ryoKeg.ULZsLi343YS0xbuO0VteEA',
],
'expires_at' => 'never',
],
$this->app->make('cache')->get('cache-query|foo')
);
}
public function test_calling_to_sql_does_not_cache_result(): void
{
$repository = $this->mock(Repository::class);
$repository->expects('has')->never();
$repository->expects('put')->never();
$this->mock('cache')->expects('store')->twice()->with(null)->andReturn($repository);
static::assertIsString($this->app->make('db')->table('users')->cache()->toSql());
static::assertIsString(User::query()->cache()->toSql());
}
public function test_calling_non_executing_method_doesnt_caches_the_result(): void
{
$repository = $this->mock(Repository::class);
$repository->expects('has')->never();
$repository->expects('put')->never();
$this->mock('cache')->expects('store')->twice()->with(null)->andReturn($repository);
static::assertIsArray($this->app->make('db')->table('users')->cache()->getBindings());
static::assertIsArray(User::query()->cache()->getBindings());
}
public function test_calling_non_returning_builder_method_does_not_cache_result(): void
{
$repository = $this->mock(Repository::class);
$repository->expects('has')->never();
$repository->expects('put')->never();
$this->mock('cache')->expects('store')->once()->with(null)->andReturn($repository);
static::assertIsArray(User::query()->cache()->with('pages')->getEagerLoads());
}
public function test_no_exception_when_caching_eloquent_query_twice(): void
{
$builder = User::query()->cache();
$proxy = $builder->getConnection();
static::assertInstanceOf(Proxy::class, $proxy);
static::assertInstanceOf(ConnectionInterface::class, $proxy->connection);
$sameConnection = $builder->cache()->getConnection();
static::assertSame($proxy->connection, $sameConnection->connection);
}
public function test_pass_through_methods_to_wrapped_connection(): void
{
$connection = $this->app->make('db')->table('users')->cache()->getConnection();
$connection->setDatabaseName('foo');
static::assertSame('foo', $connection->getDatabaseName());
$query = $this->app->make('db')->table('users')->cache()->where('id', 1);
static::assertSame([], $query->getConnection()->getQueryLog());
}
public function test_pass_through_properties_set_and_get(): void
{
$this->app->make('db')->table('users')->cache()->getConnection()->foo = ['foo'];
static::assertSame(['foo'], $this->app->make('db')->table('users')->cache()->getConnection()->foo);
}
public function test_select_one_uses_cache(): void
{
$results = $this->app->make('db')->table('users')->cache()->getConnection()->selectOne(
'select id from users where id > ?', [1]
);
$this->app->make('db')->table('users')->delete();
$retrieved = $this->app->make('db')->table('users')->cache()->getConnection()->selectOne(
'select id from users where id > ?', [1]
);
static::assertSame($results, $retrieved);
}
public function test_sets_custom_query_hasher(): void
{
Proxy::$queryHasher = function (
ConnectionInterface $connection,
string $query,
array $bindings
) use (&$args) {
static::assertSame(':memory:', $connection->getDatabaseName());
static::assertSame('select * from "users" where "users"."id" = ? limit 1', $query);
static::assertSame([0 => 1], $bindings);
return 'test_hash';
};
User::query()->cache()->whereKey(1)->first();
static::assertTrue($this->app->make('cache')->has('cache-query|test_hash'));
}
public function test_base_query_uses_cache_callback(): void
{
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
$repository = $this->mock(Repository::class);
$repository->expects('flexible')->never();
$repository->expects('put')->with($hash, Mockery::type('array'), [5, 300])->once();
$repository->expects('getMultiple')->with([$hash, ''])->times(1)->andReturn(['' => null, $hash => null]);
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
$this->app->make('db')->table('users')->where('id', 1)->cache(function ($cache) {
static::assertInstanceOf(Cache::class, $cache);
$cache->ttl([5, 300]);
})->first();
}
}
class User extends Authenticatable
{
protected $table = 'users';
public function posts()
{
return $this->hasMany(Post::class);
}
public function latestPost()
{
return $this->hasOne(Post::class)->ofMany();
}
public function comments()
{
return $this->hasManyThrough(Comment::class, Post::class);
}
}
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
}
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);
}
}