-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor filter method in Filterable trait to use scopeFilter
- Loading branch information
1 parent
c54f910
commit fd9362a
Showing
4 changed files
with
57 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,11 @@ protected function setUp(): void | |
'name' => 'John Doe', | ||
'email' => '[email protected]', | ||
]); | ||
|
||
MockFilterable::factory()->create([ | ||
'name' => 'Jane Doe', | ||
'email' => '[email protected]', | ||
]); | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
|
@@ -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 = [ | ||
|
@@ -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 | ||
|