Skip to content

Commit

Permalink
Refactor filter method in Filterable trait to use scopeFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Thavarshan committed Apr 10, 2024
1 parent c54f910 commit fd9362a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class PostController extends Controller
{
public function index(Request $request, PostFilter $filter)
{
$query = Post::query()->filter($filter);
$query = Post::filter($filter);

$posts = $request->has('paginate')
? $query->paginate($request->query('per_page', 20))
Expand All @@ -145,7 +145,7 @@ class PostController extends Controller
{
$filter->forUser($request->user());

$query = Post::query()->filter($filter);
$query = Post::filter($filter);

$posts = $request->has('paginate')
? $query->paginate($request->query('per_page', 20))
Expand Down Expand Up @@ -207,7 +207,7 @@ class PostFilterTest extends TestCase
$inactivePost = Post::factory()->create(['status' => 'inactive']);

$filter = new PostFilter(new Request(['status' => 'active']));
$filteredPosts = $filter->apply(Post::query())->get();
$filteredPosts = Post::filter($filter)->get();

$this->assertTrue($filteredPosts->contains($activePost));
$this->assertFalse($filteredPosts->contains($inactivePost));
Expand Down
15 changes: 12 additions & 3 deletions src/Filterable/Interfaces/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Filterable\Interfaces;

use Exception;
use Illuminate\Database\Eloquent\Builder;

/**
Expand All @@ -14,9 +15,17 @@ interface Filterable
/**
* Apply all relevant space filters.
*
* @param \Filterable\Interfaces\Filter $filters
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Filterable\Interfaces\Filter $filters
* @param array|null $options
*
* @return \Illuminate\Database\Eloquent\Builder
* @return \Illuminate\Database\Eloquent\Builder $query
*
* @throws Exception
*/
public function filter(Filter $filters): Builder;
public function scopeFilter(
Builder $query,
Filter $filters,
?array $options = []
): Builder;
}
14 changes: 9 additions & 5 deletions src/Filterable/Traits/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ trait Filterable
/**
* Apply all relevant space filters.
*
* @param \Filterable\Interfaces\Filter $filters
* @param array|null $options
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Filterable\Interfaces\Filter $filters
* @param array|null $options
*
* @return \Illuminate\Database\Eloquent\Builder $query
*
* @throws Exception
*/
public function filter(Filter $filters, ?array $options = []): Builder
{
return $filters->apply($this->newModelQuery(), $options);
public function scopeFilter(
Builder $query,
Filter $filters,
?array $options = []
): Builder {
return $filters->apply($query, $options);
}
}
36 changes: 33 additions & 3 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ protected function setUp(): void
'name' => 'John Doe',
'email' => '[email protected]',
]);

MockFilterable::factory()->create([
'name' => 'Jane Doe',
'email' => '[email protected]',
]);
}

/**
Expand Down Expand Up @@ -59,10 +64,11 @@ public function testAppliesFiltersDynamicallyBasedOnRequest(): void
$model->newQuery()->where('name', 'LIKE', '%John Doe%')->toSql(),
$results->toSql()
);
$this->assertCount(1, $results->get());
$this->assertEquals('John Doe', $results->first()->name);
}

public function testAppliesFiltersDynamicallyBasedOnRequestWithCustomMethodNames(): void
public function testAppliesFiltersManuallyThroughModel(): void
{
$request = Request::create('/?name=' . urlencode('John Doe'), 'GET');
$model = new MockFilterable();
Expand All @@ -71,6 +77,29 @@ public function testAppliesFiltersDynamicallyBasedOnRequestWithCustomMethodNames
$cache = m::mock(Repository::class);
$cache->shouldNotReceive('remember')->andReturn($builder);

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

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

$this->assertEquals(
$model->newQuery()->where('name', 'LIKE', '%John Doe%')->toSql(),
$results->toSql()
);
$this->assertCount(1, $results->get());
$this->assertEquals('John Doe', $results->first()->name);
}

public function testAppliesFiltersDynamicallyBasedOnRequestWithCustomMethodNames(): void
{
$request = Request::create('/?name=' . urlencode('Jane Doe'), 'GET');
$model = new MockFilterable();
$builder = $model->newQuery();

$cache = m::mock(Repository::class);
$cache->shouldNotReceive('remember')->andReturn($builder);

// Assuming 'name' filter translates to a method call
$filter = new class ($request, $cache) extends Filter {
protected array $filterMethodMap = [
Expand All @@ -88,10 +117,11 @@ public function filterByName($name)
$results = $filter->apply($builder);

$this->assertEquals(
$model->newQuery()->where('name', 'LIKE', '%John Doe%')->toSql(),
$model->newQuery()->where('name', 'LIKE', '%Jane Doe%')->toSql(),
$results->toSql()
);
$this->assertEquals('John Doe', $results->first()->name);
$this->assertCount(1, $results->get());
$this->assertEquals('Jane Doe', $results->first()->name);
}

public function testHandlesCachingCorrectly(): void
Expand Down

0 comments on commit fd9362a

Please sign in to comment.