Skip to content

Commit d94ca2c

Browse files
Merge pull request #45 from Laragear/fix/callback
Fixes cache callback using its return instead of Cache instance.
2 parents fbb3810 + e180986 commit d94ca2c

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/CacheQueryServiceProvider.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,16 @@ protected function macro(): Closure
8787
$this->connection = $this->connection->connection;
8888
}
8989

90+
$cache = new Cache();
91+
92+
match (true) {
93+
$ttl instanceof Closure => $ttl($cache),
94+
! $ttl instanceof Cache => $cache->ttl($ttl),
95+
default => $cache = $ttl
96+
};
97+
9098
// Normalize the TTL argument to a Cache instance.
91-
$this->connection = Proxy::crateNewInstance($this->connection, match (true) {
92-
$ttl instanceof Closure => $ttl(new Cache),
93-
! $ttl instanceof Cache => (new Cache)->ttl($ttl),
94-
default => $ttl
95-
});
99+
$this->connection = Proxy::crateNewInstance($this->connection, $cache);
96100

97101
return $this;
98102
};

tests/ProxyTest.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,6 @@ protected function defineDatabaseMigrations(): void
7171
});
7272
}
7373

74-
public function test_passes_through_method_calls(): void
75-
{
76-
$query = $this->app->make('db')->table('users')->cache()->where('id', 1);
77-
78-
static::assertSame([], $query->getConnection()->getQueryLog());
79-
}
80-
8174
public function test_caches_base_query_into_default_store(): void
8275
{
8376
$get = $this->app->make('db')->table('users')->cache()->where('id', 1)->get();
@@ -874,6 +867,10 @@ public function test_pass_through_methods_to_wrapped_connection(): void
874867
$connection->setDatabaseName('foo');
875868

876869
static::assertSame('foo', $connection->getDatabaseName());
870+
871+
$query = $this->app->make('db')->table('users')->cache()->where('id', 1);
872+
873+
static::assertSame([], $query->getConnection()->getQueryLog());
877874
}
878875

879876
public function test_pass_through_properties_set_and_get(): void
@@ -916,6 +913,24 @@ public function test_sets_custom_query_hasher(): void
916913

917914
static::assertTrue($this->app->make('cache')->has('cache-query|test_hash'));
918915
}
916+
917+
public function test_base_query_uses_cache_callback(): void
918+
{
919+
$hash = 'cache-query|fj8Xyz4K1Zh0tdAamPbG1A';
920+
921+
$repository = $this->mock(Repository::class);
922+
$repository->expects('flexible')->never();
923+
$repository->expects('put')->with($hash, Mockery::type('array'), [5, 300])->once();
924+
$repository->expects('getMultiple')->with([$hash, ''])->times(1)->andReturn(['' => null, $hash => null]);
925+
926+
$this->mock('cache')->expects('store')->with(null)->andReturn($repository);
927+
928+
$this->app->make('db')->table('users')->where('id', 1)->cache(function ($cache) {
929+
static::assertInstanceOf(Cache::class, $cache);
930+
931+
$cache->ttl([5, 300]);
932+
})->first();
933+
}
919934
}
920935

921936
class User extends Authenticatable

0 commit comments

Comments
 (0)