Skip to content

Commit

Permalink
Refactor setUseCache method
Browse files Browse the repository at this point in the history
  • Loading branch information
Thavarshan committed Apr 14, 2024
1 parent 507ae00 commit a3f3fe4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 28 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,21 @@ This request will return all posts with the status `active` and associated with

### Caching

In your filter class, you can control caching by using the `setUseCache` method. Set the `$useCache` property to `true` to enable caching, or `false` to disable it. You can also customise the duration of the cache by modifying the `$cacheExpiration` property.`
In your filter class, you can control caching by using the `enableCaching` static method. Set the `$useCache` static property to `true` to enable caching, or `false` to disable it. You can also customise the duration of the cache by modifying the `$cacheExpiration` property.`

```php
// AppServiceProvider.php

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
Filter::enableCaching(true); // Control caching
}
```

```php
namespace App\Filters;
Expand All @@ -251,8 +265,7 @@ class PostFilter extends Filter
$filter = new PostFilter(request(), cache());

// Control caching
$filter->setUseCache(true);
$filter->cacheExpiration = 1440; // Cache duration in minutes
$filter->setCacheExpiration(1440); // Cache duration in minutes
```

## Testing
Expand Down
16 changes: 7 additions & 9 deletions src/Filterable/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ abstract class Filter implements FilterInterface
*
* @var bool
*/
protected bool $useCache = true;
protected static bool $useCache = true;

/**
* Expiration minutes for the cache.
Expand Down Expand Up @@ -197,7 +197,7 @@ protected function applyForUserFilter(): void
*/
protected function applyFilterables(): void
{
if (! $this->shouldUseCache()) {
if (! self::shouldUseCache()) {
$this->applyFiltersToQuery();

return;
Expand Down Expand Up @@ -467,13 +467,11 @@ public function setBuilder(Builder $builder): self
*
* @param bool $useCache
*
* @return self
* @return void
*/
public function setUseCache(bool $useCache): self
public static function enableCaching(bool $useCache): void
{
$this->useCache = $useCache;

return $this;
self::$useCache = $useCache;
}

/**
Expand Down Expand Up @@ -519,8 +517,8 @@ public function setCacheHandler(Cache $cache): self
*
* @return bool
*/
public function shouldUseCache(): bool
public static function shouldUseCache(): bool
{
return $this->useCache;
return self::$useCache;
}
}
8 changes: 4 additions & 4 deletions src/Filterable/Interfaces/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* This class is used to apply filters to an Eloquent query builder instance.
* It provides methods to set and apply filters, cache the results, and filter results for a specific authenticated user.
*
* @package Filterable
* @package Filterable\Interfaces
*
* @property \Illuminate\Database\Eloquent\Builder $builder The Eloquent builder instance.
* @property \Illuminate\Http\Request $request The current HTTP request.
Expand Down Expand Up @@ -174,9 +174,9 @@ public function setBuilder(Builder $builder): self;
*
* @param bool $useCache
*
* @return self
* @return void
*/
public function setUseCache(bool $useCache): self;
public static function enableCaching(bool $useCache): void;

/**
* Clear the cache.
Expand Down Expand Up @@ -206,5 +206,5 @@ public function setCacheHandler(Cache $cache): self;
*
* @return bool
*/
public function shouldUseCache(): bool;
public static function shouldUseCache(): bool;
}
5 changes: 5 additions & 0 deletions src/Filterable/Interfaces/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use Closure;
use Illuminate\Database\Eloquent\Builder;

/**
* Interface Filterable
*
* @package Filterable\Interfaces
*/
interface Handler
{
/**
Expand Down
33 changes: 21 additions & 12 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ protected function tearDown(): void
{
m::close();

Filter::enableCaching(false);

parent::tearDown();
}

public function testAppliesFiltersDynamicallyBasedOnRequest(): void
{
Filter::enableCaching(false);

$request = Request::create('/?name=' . urlencode('John Doe'), 'GET');
$model = new MockFilterable();
$builder = $model->newQuery();
Expand All @@ -56,7 +60,6 @@ public function testAppliesFiltersDynamicallyBasedOnRequest(): void

// Assuming 'name' filter translates to a method call
$filter = new MockFilter($request, $cache);
$filter->setUseCache(false);

$results = $filter->apply($builder);

Expand All @@ -70,6 +73,8 @@ public function testAppliesFiltersDynamicallyBasedOnRequest(): void

public function testAppliesFiltersManuallyThroughModel(): void
{
Filter::enableCaching(false);

$request = Request::create('/?name=' . urlencode('John Doe'), 'GET');
$model = new MockFilterable();
$builder = $model->newQuery();
Expand All @@ -79,7 +84,6 @@ public function testAppliesFiltersManuallyThroughModel(): void

// Assuming 'name' filter translates to a method call
$filter = new MockFilter($request, $cache);
$filter->setUseCache(false);

$results = $model->filter($filter);

Expand All @@ -93,6 +97,8 @@ public function testAppliesFiltersManuallyThroughModel(): void

public function testAppliesFiltersDynamicallyBasedOnRequestWithCustomMethodNames(): void
{
Filter::enableCaching(false);

$request = Request::create('/?name=' . urlencode('Jane Doe'), 'GET');
$model = new MockFilterable();
$builder = $model->newQuery();
Expand All @@ -112,8 +118,6 @@ public function filterByName($name)
}
};

$filter->setUseCache(false);

$results = $filter->apply($builder);

$this->assertEquals(
Expand All @@ -126,6 +130,8 @@ public function filterByName($name)

public function testHandlesCachingCorrectly(): void
{
Filter::enableCaching(true);

$request = new Request();
$cache = m::spy(Repository::class);
$model = new MockFilterable();
Expand All @@ -140,8 +146,6 @@ public function testHandlesCachingCorrectly(): void
public function __construct($request, $cache)
{
parent::__construct($request, $cache);

$this->useCache = true; // Ensure caching is enabled
}

protected function testFilter($value)
Expand All @@ -160,6 +164,8 @@ protected function testFilter($value)

public function testHandlesCachingCorrectlyWhenDisabled(): void
{
Filter::enableCaching(false);

$request = new Request();
$cache = m::spy(Repository::class);
$model = new MockFilterable();
Expand All @@ -177,8 +183,6 @@ protected function testFilter($value)
}
};

$filter->setUseCache(false);

$results = $filter->apply($builder);

$this->assertSame($builder, $results);
Expand All @@ -187,6 +191,8 @@ protected function testFilter($value)

public function testHandlesCachingCorrectlyWhenForced(): void
{
Filter::enableCaching(true);

$request = new Request();
$cache = m::spy(Repository::class);
$model = new MockFilterable();
Expand All @@ -204,8 +210,6 @@ protected function testFilter($value)
}
};

$filter->setUseCache(true);

$results = $filter->apply($builder);

$this->assertSame($builder, $results);
Expand All @@ -214,6 +218,8 @@ protected function testFilter($value)

public function testHandlesCachingCorrectlyWhenForcedWithCustomTtl(): void
{
Filter::enableCaching(true);

$request = new Request();
$cache = m::spy(Repository::class);
$model = new MockFilterable();
Expand All @@ -233,7 +239,6 @@ protected function testFilter($value)
}
};

$filter->setUseCache(true);
$filter->setCacheExpiration(60);

$results = $filter->apply($builder);
Expand All @@ -244,6 +249,8 @@ protected function testFilter($value)

public function testClearsCacheCorrectly(): void
{
Filter::enableCaching(true);

$request = new Request();
$cache = m::mock(Repository::class);
$cache->shouldReceive('forget')->once();
Expand All @@ -262,6 +269,8 @@ public function testClearsCacheCorrectly(): void

public function testAppliesPreFiltersCorrectly(): void
{
Filter::enableCaching(true);

$request = new Request();
$cache = m::mock(Repository::class);
$model = new MockFilterable();
Expand All @@ -277,7 +286,7 @@ public function applyPreFilters(): void
}
};

// $filter->setUseCache(false);
// $filter->enableCaching(false);

$filter->apply($builder);

Expand Down

0 comments on commit a3f3fe4

Please sign in to comment.