From 811d8e7886ac8ec6f6dd9a32f60f1b70c3bbfe59 Mon Sep 17 00:00:00 2001 From: zhuzhichao Date: Tue, 10 Nov 2020 19:39:36 +0800 Subject: [PATCH] Update unit testing. --- src/ConditionsBuilder.php | 2 +- src/ServiceProvider.php | 8 ++-- ...28_000002_create_testbench_users_table.php | 1 - tests/TestCase.php | 5 ++- .../ConditionsBuilder/SimpleLikeQueryTest.php | 44 +++++++++++++++++++ .../Unit/ConditionsBuilder/WhereQueryTest.php | 35 +++++++++++++++ tests/Utils/Models/Company.php | 9 ++++ tests/Utils/Models/User.php | 9 ++++ 8 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 tests/Unit/ConditionsBuilder/SimpleLikeQueryTest.php create mode 100644 tests/Unit/ConditionsBuilder/WhereQueryTest.php diff --git a/src/ConditionsBuilder.php b/src/ConditionsBuilder.php index 7cbf336..9e0ad6b 100644 --- a/src/ConditionsBuilder.php +++ b/src/ConditionsBuilder.php @@ -238,7 +238,7 @@ private function convertOperator($operator): string 'le' => '<=', ]; - return $operatorMap[$operator] ?? '='; + return $operatorMap[$operator] ?? $operator ?? '='; } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 7b5ea0a..23eb94f 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -20,12 +20,12 @@ public function boot() */ public function register() { - Builder::macro('advanced', function(Builder $builder, $conditions) { - return (new ConditionsBuilder($builder))->attach($conditions); + Builder::macro('advanced', function($conditions = []) { + return (new ConditionsBuilder($this))->attach($conditions); }); - Request::macro('conditions', function(Request $request, $wheres) { - return (new ConditionsGenerator($request, $wheres))->getRequestConditions(); + Request::macro('conditions', function($wheres = []) { + return (new ConditionsGenerator($this, $wheres))->getRequestConditions(); }); } } diff --git a/tests/Migrations/2018_02_28_000002_create_testbench_users_table.php b/tests/Migrations/2018_02_28_000002_create_testbench_users_table.php index 826bf7a..3183522 100644 --- a/tests/Migrations/2018_02_28_000002_create_testbench_users_table.php +++ b/tests/Migrations/2018_02_28_000002_create_testbench_users_table.php @@ -14,7 +14,6 @@ public function up(): void Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('company_id')->nullable(); - $table->unsignedInteger('team_id')->nullable(); $table->string('name')->nullable(); $table->string('email')->nullable(); $table->string('password')->nullable(); diff --git a/tests/TestCase.php b/tests/TestCase.php index e0c8450..d5a16a1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,6 +3,7 @@ namespace Tests; use Orchestra\Testbench\TestCase as BaseTestCase; +use Zhuzhichao\LaravelAdvancedSearch\ServiceProvider; abstract class TestCase extends BaseTestCase { @@ -14,7 +15,9 @@ abstract class TestCase extends BaseTestCase */ protected function getPackageProviders($app) { - return []; + return [ + ServiceProvider::class + ]; } /** diff --git a/tests/Unit/ConditionsBuilder/SimpleLikeQueryTest.php b/tests/Unit/ConditionsBuilder/SimpleLikeQueryTest.php new file mode 100644 index 0000000..6a316ca --- /dev/null +++ b/tests/Unit/ConditionsBuilder/SimpleLikeQueryTest.php @@ -0,0 +1,44 @@ +make([ + 'name' => 'this name is testing for search, first name', + ])->save(); + Company::factory()->make([ + 'name' => 'this name is testing for search, double first name', + ])->save(); + Company::factory()->make([ + 'name' => 'this name is testing for search, second name', + ])->save(); + + User::factory()->make([ + + ]); + } + + public function test_without_scope() + { + self::assertEquals(2, Company::advanced(['wheres' => [ + 'name.like' => '%first%', + ]])->count()); + } + + public function test_with_cope() + { + + } +} diff --git a/tests/Unit/ConditionsBuilder/WhereQueryTest.php b/tests/Unit/ConditionsBuilder/WhereQueryTest.php new file mode 100644 index 0000000..d59d734 --- /dev/null +++ b/tests/Unit/ConditionsBuilder/WhereQueryTest.php @@ -0,0 +1,35 @@ +count(10)->create(); + User::factory()->make([ + 'name' => 'zhuzhichao', + 'email' => 'me@zhuzhichao.com', + ])->save(); + } + + public function test_model_without_search_keyword_method() + { + self::assertEquals(Company::count(), Company::advanced([ + 'keyword' => 'Chou' + ])->count()); + } + + public function test_model_with_search_keyword_method() + { + self::assertEquals(1, User::advanced([ + 'keyword' => 'zhichao' + ])->count()); + } +} diff --git a/tests/Utils/Models/Company.php b/tests/Utils/Models/Company.php index b92dbf0..b62b5a0 100644 --- a/tests/Utils/Models/Company.php +++ b/tests/Utils/Models/Company.php @@ -2,11 +2,15 @@ namespace Tests\Utils\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Tests\Factories\CompanyFactory; class Company extends Model { + use HasFactory; + protected $guarded = []; public function users(): HasMany @@ -20,4 +24,9 @@ public function scopeName($query, $value) return $query; } + + protected static function newFactory() + { + return new CompanyFactory; + } } diff --git a/tests/Utils/Models/User.php b/tests/Utils/Models/User.php index 48917b5..be74c9b 100644 --- a/tests/Utils/Models/User.php +++ b/tests/Utils/Models/User.php @@ -3,13 +3,17 @@ namespace Tests\Utils\Models; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Foundation\Auth\User as Authenticatable; +use Tests\Factories\CompanyFactory; +use Tests\Factories\UserFactory; class User extends Authenticatable { use SoftDeletes; + use HasFactory; /** * @var mixed[] @@ -50,4 +54,9 @@ public function scopeSearchKeyword($q, $key) return $q; } + + protected static function newFactory() + { + return new UserFactory; + } }