Skip to content

Commit

Permalink
Add Conditionable trait (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanra authored Feb 10, 2022
1 parent a3bed78 commit dcce3c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Searcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;

class Searcher
{
use Conditionable;

/**
* Collection of models to search through.
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Collection;
use ProtoneMedia\LaravelCrossEloquentSearch\OrderByRelevanceException;
use ProtoneMedia\LaravelCrossEloquentSearch\Search;
use ProtoneMedia\LaravelCrossEloquentSearch\Searcher;

class SearchTest extends TestCase
{
Expand Down Expand Up @@ -654,4 +655,24 @@ public function it_returns_data_consistently() {
$this->assertTrue($resultA->first()->is($postA));
$this->assertTrue($resultB->first()->is($postA));
}

/** @test */
public function it_can_conditionally_apply_ordering()
{
Carbon::setTestNow(now());
$postA = Post::create(['title' => 'foo']);

Carbon::setTestNow(now()->subDay());
$postB = Post::create(['title' => 'foo2']);

$results = Search::add(Post::class, 'title')
->when(true, fn (Searcher $searcher) => $searcher->orderByDesc())
->get('foo');

$this->assertInstanceOf(Collection::class, $results);
$this->assertCount(2, $results);

$this->assertTrue($results->first()->is($postA));
$this->assertTrue($results->last()->is($postB));
}
}

0 comments on commit dcce3c1

Please sign in to comment.