Skip to content

Commit fb95caf

Browse files
Merge pull request #39 from Laragear/fix/array-on-relation
[4.x] Fixes Eloquent Query Cache not accepting array as TTL
2 parents eee9f8e + 33d1c1f commit fb95caf

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Scopes/CacheRelations.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CacheRelations implements Scope
1515
* Creates a new scope instance.
1616
*/
1717
public function __construct(
18-
protected DateTimeInterface|DateInterval|int|null $ttl,
18+
protected DateTimeInterface|DateInterval|int|array|null $ttl,
1919
protected string $key,
2020
protected ?string $store,
2121
protected int $wait,

tests/CacheAwareConnectionProxyTest.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public function test_regenerates_cache_using_ttl_with_negative_number(): void
445445
static::assertSame('test', $result->name);
446446
}
447447

448-
public function test_uses_flexible_caching_when_using_ttl_as_array_of_values(): void
448+
public function test_uses_db_flexible_caching_when_using_ttl_as_array_of_values(): void
449449
{
450450
if (! method_exists(CacheRepository::class, 'flexible')) {
451451
$this->markTestSkipped('Cannot test flexible caching if repository does not implements it.');
@@ -463,6 +463,24 @@ public function test_uses_flexible_caching_when_using_ttl_as_array_of_values():
463463
$this->app->make('db')->table('users')->where('id', 1)->cache([5, 300])->first();
464464
}
465465

466+
public function test_uses_eloquent_flexible_caching_when_using_ttl_as_array_of_values(): void
467+
{
468+
if (! method_exists(CacheRepository::class, 'flexible')) {
469+
$this->markTestSkipped('Cannot test flexible caching if repository does not implements it.');
470+
}
471+
472+
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
473+
474+
$repository = $this->mock(CacheRepository::class);
475+
$repository->expects('put')->never();
476+
$repository->expects('flexible')->with($hash, Mockery::type('array'), [5, 300])->once();
477+
$repository->expects('getMultiple')->with([$hash, ''])->times(1)->andReturn(['' => null, $hash => null]);
478+
479+
$this->mock('cache')->shouldReceive('store')->with(null)->andReturn($repository);
480+
481+
User::where('id', 1)->cache([5, 300])->first();
482+
}
483+
466484
public function test_doesnt_uses_flexible_caching_if_repository_is_not_flexible(): void
467485
{
468486
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';

0 commit comments

Comments
 (0)