-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
- Loading branch information
Showing
15 changed files
with
689 additions
and
45 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
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 |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php | ||
|
||
namespace Laravie\QueryFilter\Tests\Feature; | ||
|
||
use Illuminate\Database\Query\Expression; | ||
use Illuminate\Support\Facades\DB; | ||
use Laravie\QueryFilter\Searchable; | ||
use Laravie\QueryFilter\Tests\Models\User; | ||
use Laravie\QueryFilter\Tests\TestCase; | ||
|
||
class EloquentSearchableTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_build_search_query() | ||
{ | ||
$stub = new Searchable( | ||
'hello', ['name'] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users" where (("users"."name" like ? or "users"."name" like ? or "users"."name" like ? or "users"."name" like ?))', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
['hello', 'hello%', '%hello', '%hello%'], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_ignores_build_search_query_when_columns_is_not_provided() | ||
{ | ||
$stub = new Searchable( | ||
'hello', [] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users"', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
[], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_ignores_build_search_query_when_columns_is_invalid() | ||
{ | ||
$stub = new Searchable( | ||
'hello', [''] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users"', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
[], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_ignores_build_search_query_when_keyword_is_empty() | ||
{ | ||
$stub = new Searchable( | ||
'', ['name'] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users"', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
[], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_build_search_query_with_expression_value() | ||
{ | ||
$stub = new Searchable( | ||
'hello', [new Expression('users.name')] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users" where (("users"."name" like ? or "users"."name" like ? or "users"."name" like ? or "users"."name" like ?))', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
['hello', 'hello%', '%hello', '%hello%'], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_build_search_query_with_json_selector() | ||
{ | ||
$stub = new Searchable( | ||
'hello', ['address->postcode'] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users" where ((lower(address->\'$.postcode\') like ? or lower(address->\'$.postcode\') like ? or lower(address->\'$.postcode\') like ? or lower(address->\'$.postcode\') like ?))', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
['hello', 'hello%', '%hello', '%hello%'], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_cant_build_search_query_with_invalid_column_name() | ||
{ | ||
$stub = new Searchable( | ||
'hello', ['email->"%27))%23injectedSQL'] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users"', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
[], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_build_search_query_with_relation_field() | ||
{ | ||
$stub = new Searchable( | ||
'hello', ['name', 'posts.title'] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users" where (("users"."name" like ? or "users"."name" like ? or "users"."name" like ? or "users"."name" like ?) or exists (select * from "posts" where "users"."id" = "posts"."user_id" and ("posts"."title" like ? or "posts"."title" like ? or "posts"."title" like ? or "posts"."title" like ?)))', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
['hello', 'hello%', '%hello', '%hello%', 'hello', 'hello%', '%hello', '%hello%'], | ||
$query->getBindings() | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace Laravie\QueryFilter\Tests\Feature; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Laravie\QueryFilter\Taxonomy; | ||
use Laravie\QueryFilter\Tests\Models\User; | ||
use Laravie\QueryFilter\Tests\TestCase; | ||
|
||
class EloquentTaxonomyTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_build_match_query() | ||
{ | ||
$stub = new Taxonomy( | ||
'name:hello email:crynobone@gmail.com email:crynobone@orchestraplatform.com is:active', [ | ||
'name:*' => static function ($query, $value) { | ||
return $query->where('name', '=', $value); | ||
}, | ||
'email:[]' => static function ($query, $value) { | ||
return $query->whereIn('email', $value); | ||
}, | ||
'is:active' => static function ($query) { | ||
return $query->whereNotNull('deleted_at'); | ||
}, | ||
], ['name'] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users" where "name" = ? and "email" in (?, ?) and "deleted_at" is not null', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
['hello', 'crynobone@gmail.com', 'crynobone@orchestraplatform.com'], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_cant_build_match_query_given_empty_taxanomy() | ||
{ | ||
$stub = new Taxonomy( | ||
'name: email: email: is:', [ | ||
'name:*' => static function ($query, $value) { | ||
return $query->where('name', '=', $value); | ||
}, | ||
'email:[]' => static function ($query, $value) { | ||
return $query->whereIn('email', $value); | ||
}, | ||
'is:active' => static function ($query) { | ||
return $query->whereNotNull('deleted_at'); | ||
}, | ||
], ['name'] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users"', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
[], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_build_match_query_with_basic_search() | ||
{ | ||
$stub = new Taxonomy( | ||
'hello', [], ['name'] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users" where (("users"."name" like ? or "users"."name" like ? or "users"."name" like ? or "users"."name" like ?))', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
['hello', 'hello%', '%hello', '%hello%'], | ||
$query->getBindings() | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_build_match_query_with_basic_search_with_related_field() | ||
{ | ||
$stub = new Taxonomy( | ||
'hello', [], ['name', 'posts.title'] | ||
); | ||
|
||
$query = User::query(); | ||
$stub->apply($query); | ||
|
||
$this->assertSame( | ||
'select * from "users" where (("users"."name" like ? or "users"."name" like ? or "users"."name" like ? or "users"."name" like ?) or exists (select * from "posts" where "users"."id" = "posts"."user_id" and ("posts"."title" like ? or "posts"."title" like ? or "posts"."title" like ? or "posts"."title" like ?)))', | ||
$query->toSql() | ||
); | ||
|
||
$this->assertSame( | ||
['hello', 'hello%', '%hello', '%hello%', 'hello', 'hello%', '%hello', '%hello%'], | ||
$query->getBindings() | ||
); | ||
} | ||
} |
Oops, something went wrong.