Skip to content

Commit

Permalink
Merge pull request #45 from Laragear/fix/callback
Browse files Browse the repository at this point in the history
Fixes cache callback using its return instead of Cache instance.
  • Loading branch information
DarkGhostHunter authored Feb 14, 2025
2 parents fbb3810 + e180986 commit d94ca2c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/CacheQueryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ protected function macro(): Closure
$this->connection = $this->connection->connection;
}

$cache = new Cache();

match (true) {
$ttl instanceof Closure => $ttl($cache),
! $ttl instanceof Cache => $cache->ttl($ttl),
default => $cache = $ttl
};

// Normalize the TTL argument to a Cache instance.
$this->connection = Proxy::crateNewInstance($this->connection, match (true) {
$ttl instanceof Closure => $ttl(new Cache),
! $ttl instanceof Cache => (new Cache)->ttl($ttl),
default => $ttl
});
$this->connection = Proxy::crateNewInstance($this->connection, $cache);

return $this;
};
Expand Down
29 changes: 22 additions & 7 deletions tests/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ protected function defineDatabaseMigrations(): void
});
}

public function test_passes_through_method_calls(): void
{
$query = $this->app->make('db')->table('users')->cache()->where('id', 1);

static::assertSame([], $query->getConnection()->getQueryLog());
}

public function test_caches_base_query_into_default_store(): void
{
$get = $this->app->make('db')->table('users')->cache()->where('id', 1)->get();
Expand Down Expand Up @@ -874,6 +867,10 @@ public function test_pass_through_methods_to_wrapped_connection(): void
$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
Expand Down Expand Up @@ -916,6 +913,24 @@ public function test_sets_custom_query_hasher(): void

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
Expand Down

0 comments on commit d94ca2c

Please sign in to comment.