From 9df2b191ad7b730be1b44c4bf43a9873e178fbfa Mon Sep 17 00:00:00 2001 From: Hamidrrj Date: Fri, 19 Jul 2024 21:17:21 +0330 Subject: [PATCH 1/6] tested filter with various FNs and Datatypes --- README.md | 4 +- database/factories/UserFactory.php | 2 +- .../migrations/create_users_table.php.stub | 1 + tests/DatatableTest.php | 692 +++++++++++++++++- 4 files changed, 684 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0cf7f47..02e7002 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ It contains various search logics for different datatypes (numeric, text, date): 5. Equals 6. NotEquals -You can add custom your own search logic and datatype with ease :) +You can add your own search logic and datatype with ease :) ## Installation @@ -77,7 +77,7 @@ Please review [our security policy](../../security/policy) on how to report secu ## Credits -- [Hamidreza Ranjbarpour](https://github.com/hamidrezaRanjbarpour) +- [Hamidreza Ranjbarpour](https://github.com/hamidrezarj) - [All Contributors](../../contributors) ## License diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 63f9d74..20e812d 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -16,7 +16,7 @@ public function definition() 'name' => $this->faker->name(), 'username' => $this->faker->userName(), 'email' => $this->faker->email, -// 'position' => $this->faker->randomElement(['boss']), + 'age' => $this->faker->numberBetween(1, 100), // 'city_id' => City::factory(), // 'province_id' => function (array $attributes) { // return City::find($attributes['city_id'])->province_id; diff --git a/database/migrations/create_users_table.php.stub b/database/migrations/create_users_table.php.stub index 87017f4..8c3eab0 100644 --- a/database/migrations/create_users_table.php.stub +++ b/database/migrations/create_users_table.php.stub @@ -11,6 +11,7 @@ return new class extends Migration Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); + $table->integer('age'); $table->string('username')->unique(); $table->string('email')->unique(); $table->timestamps(); diff --git a/tests/DatatableTest.php b/tests/DatatableTest.php index 395b067..9eeb65b 100644 --- a/tests/DatatableTest.php +++ b/tests/DatatableTest.php @@ -141,35 +141,703 @@ it('can get correct data with providing filter fn:`contains` and datatype:`text`', function () { - $users = User::factory()->count(15)->create(); + $expectedUsers = User::factory() + ->count(4) + ->create([ + 'name' => 'First User' + ]); + + User::factory() + ->count(6) + ->create([ + 'name' => 'Non match user' + ]); $query = User::query(); $requestParameters = [ - 'start' => 5, - 'size' => 8, - 'filters' => json_encode([]), + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'name', + 'value' => 'first', + 'fn' => 'contains', + 'datatype' => 'text' + ] + ]), 'sorting' => json_encode([]) ]; + $allowedFilters = array('name'); + $data = DatatableFacade::run( $query, - $requestParameters + $requestParameters, + $allowedFilters ); - $expected = array_slice($users->toArray(), 5, 8); + $expected = $expectedUsers->toArray(); expect($data['data']) ->toEqual($expected); expect($data['meta']['totalRowCount']) - ->toBe(15); + ->toBe(4); + +}); + +it('can get correct data with providing filter fn:`contains` and datatype:`numeric`', function () { + + $expectedUsers = User::factory() + ->count(5) + ->state(new \Illuminate\Database\Eloquent\Factories\Sequence( + ['age' => 3], + ['age' => 13], + ['age' => 53], + )) + ->create(); + + User::factory() + ->count(6) + ->state(new \Illuminate\Database\Eloquent\Factories\Sequence( + ['age' => 12], + ['age' => 98], + ['age' => 42], + )) + ->create(); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'age', + 'value' => '3', + 'fn' => 'contains', + 'datatype' => 'numeric' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('age'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(5); + +}); + +it('can get correct data with providing filter fn:`equals` and datatype:`text`', function () { + + $expectedUsers = User::factory() + ->count(4) + ->create([ + 'name' => 'First User' + ]); + + User::factory() + ->count(6) + ->create([ + 'name' => 'Non match user' + ]); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'name', + 'value' => 'first user', + 'fn' => 'equals', + 'datatype' => 'text' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('name'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(4); + +}); + +/* FN: equals */ +it('can get correct data with providing filter fn:`equals` and datatype:`numeric`', function () { + + $expectedUsers = User::factory() + ->count(3) + ->create([ + 'age' => 13 + ]); + + User::factory() + ->count(6) + ->create([ + 'age' => 42 + ]); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'age', + 'value' => '13', + 'fn' => 'equals', + 'datatype' => 'numeric' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('age'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(3); + +}); + +it('can get correct data with providing filter fn:`equals` and datatype:`date`', function () { + + $expectedUsers = User::factory() + ->count(5) + ->create([ + 'created_at' => '2024-05-23 12:00:00' + ]); + + + User::factory() + ->count(6) + ->create(); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'created_at', + 'value' => '2024-05-23 12:00:00', + 'fn' => 'equals', + 'datatype' => 'date' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('created_at'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(5); + +}); + +/* FN: notEquals */ +it('can get correct data with providing filter fn:`notEquals` and datatype:`text`', function () { + + User::factory() + ->count(4) + ->create([ + 'name' => 'First User' + ]); + + $expectedUsers = User::factory() + ->count(6) + ->create([ + 'name' => 'Non match user' + ]); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'name', + 'value' => 'First User', + 'fn' => 'notEquals', + 'datatype' => 'text' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('name'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(6); + +}); + +it('can get correct data with providing filter fn:`notEquals` and datatype:`numeric`', function () { + + User::factory() + ->count(3) + ->create([ + 'age' => 13 + ]); + + $expectedUsers = User::factory() + ->count(6) + ->create([ + 'age' => 42 + ]); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'age', + 'value' => '13', + 'fn' => 'notEquals', + 'datatype' => 'numeric' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('age'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(6); + +}); + +it('can get correct data with providing filter fn:`notEquals` and datatype:`date`', function () { + + User::factory() + ->count(5) + ->create([ + 'created_at' => '2024-05-23 12:00:00' + ]); + + + $expectedUsers = User::factory() + ->count(6) + ->create(); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'created_at', + 'value' => '2024-05-23 12:00:00', + 'fn' => 'notEquals', + 'datatype' => 'date' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('created_at'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(6); + +}); + +/* FN: between */ +it('can get correct data with providing filter fn:`between` and datatype:`numeric`', function () { + + User::factory() + ->count(3) + ->create([ + 'age' => 13 + ]); + + $expectedUsers = User::factory() + ->count(6) + ->create([ + 'age' => 42 + ]); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'age', + 'value' => ['30', '50'], + 'fn' => 'between', + 'datatype' => 'numeric' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('age'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(6); + +}); + +it('can get correct data with providing filter fn:`between` and datatype:`date`', function () { + + $expectedUsers = User::factory() + ->count(5) + ->create([ + 'created_at' => '2024-05-23 12:00:00' + ]); + + + User::factory() + ->count(6) + ->create(); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'created_at', + 'value' => ['2024-05-23 10:30:00', '2024-05-23 15:00:00'], + 'fn' => 'between', + 'datatype' => 'date' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('created_at'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(5); + +}); + +/* FN: greaterThan */ +it('can get correct data with providing filter fn:`greaterThan` and datatype:`numeric`', function () { + + User::factory() + ->count(3) + ->create([ + 'age' => 13 + ]); + + $expectedUsers = User::factory() + ->count(6) + ->create([ + 'age' => 42 + ]); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'age', + 'value' => '25', + 'fn' => 'greaterThan', + 'datatype' => 'numeric' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('age'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(6); + +}); + +it('can get correct data with providing filter fn:`greaterThan` and datatype:`date`', function () { + + User::factory() + ->count(5) + ->create([ + 'created_at' => '2024-05-23 12:00:00' + ]); + + + $expectedUsers = User::factory() + ->count(6) + ->create(); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'created_at', + 'value' => '2024-05-23 23:59:59', + 'fn' => 'greaterThan', + 'datatype' => 'date' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('created_at'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(6); + +}); + +/* FN: lessThan */ +it('can get correct data with providing filter fn:`lessThan` and datatype:`numeric`', function () { + + $expectedUsers = User::factory() + ->count(3) + ->create([ + 'age' => 13 + ]); + + User::factory() + ->count(6) + ->create([ + 'age' => 42 + ]); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'age', + 'value' => '25', + 'fn' => 'lessThan', + 'datatype' => 'numeric' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('age'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(3); + +}); + +/** Sorting */ +it('throws exception when trying to sort a field which is not allowed to be sorted', function () { + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([ + [ + 'id' => 'name', + 'desc' => true, + ] + ]) + ]; + + $data = DatatableFacade::run( + $query, + $requestParameters, + ); + +})->throws(\HamidRrj\LaravelDatatable\Exceptions\InvalidSortingException::class, "sorting field `name` is not allowed."); + +it('can get data with descending sort on id', function (){ + + $users = User::factory() + ->count(6) + ->create(); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([ + [ + 'id' => 'id', + 'desc' => true, + ] + ]) + ]; + + $allowedSortings = array('id'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + allowedSortings: $allowedSortings + ); + +// $expected = $users->sortBy('id', descending: true)->toArray(); + $expected = $users->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(3); }); +// contains: text, numeric DONE :D +// equals: text, numeric, date :D +// not equals: text, numeric, date :D +// between: numeric, date & text :D +// GT AND LT: numeric , date :D -// contains: text, numeric -// equals: text, numeric, date -// not equals: text, numeric, date -// between: numeric, date & text -// GT AND LT: numeric , date +//sorting +// filter with rel +// sorting with rel From 02bf2f704be2909494c5c29c1ee80120479fa58a Mon Sep 17 00:00:00 2001 From: Hamidreza Ranjbarpour Date: Sun, 21 Jul 2024 17:39:44 +0330 Subject: [PATCH 2/6] fixed bug in sorting test --- tests/DatatableTest.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/DatatableTest.php b/tests/DatatableTest.php index 9eeb65b..32cafee 100644 --- a/tests/DatatableTest.php +++ b/tests/DatatableTest.php @@ -822,14 +822,19 @@ allowedSortings: $allowedSortings ); -// $expected = $users->sortBy('id', descending: true)->toArray(); $expected = $users->toArray(); + array_multisort( array_column($expected, "id"), SORT_DESC, $expected); - expect($data['data']) - ->toEqual($expected); + expect($data) + ->toEqual([ + 'data' => $expected, + 'meta' => [ + 'totalRowCount' => 6 + ] + ]); expect($data['meta']['totalRowCount']) - ->toBe(3); + ->toBe(6); }); // contains: text, numeric DONE :D From 9cac7d31e52b228daff34bbd8fb1353ea99ea5e6 Mon Sep 17 00:00:00 2001 From: Hamidreza Ranjbarpour Date: Sun, 21 Jul 2024 17:45:12 +0330 Subject: [PATCH 3/6] test for ascending sort --- tests/DatatableTest.php | 53 +++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/tests/DatatableTest.php b/tests/DatatableTest.php index 32cafee..95e0441 100644 --- a/tests/DatatableTest.php +++ b/tests/DatatableTest.php @@ -794,7 +794,7 @@ })->throws(\HamidRrj\LaravelDatatable\Exceptions\InvalidSortingException::class, "sorting field `name` is not allowed."); -it('can get data with descending sort on id', function (){ +it('can get data with descending sort on age', function (){ $users = User::factory() ->count(6) @@ -808,13 +808,13 @@ 'filters' => json_encode([]), 'sorting' => json_encode([ [ - 'id' => 'id', + 'id' => 'age', 'desc' => true, ] ]) ]; - $allowedSortings = array('id'); + $allowedSortings = array('age'); $data = DatatableFacade::run( $query, @@ -823,15 +823,48 @@ ); $expected = $users->toArray(); - array_multisort( array_column($expected, "id"), SORT_DESC, $expected); + array_multisort( array_column($expected, "age"), SORT_DESC, $expected); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(6); +}); - expect($data) - ->toEqual([ - 'data' => $expected, - 'meta' => [ - 'totalRowCount' => 6 +it('can get data with ascending sort on age', function (){ + + $users = User::factory() + ->count(6) + ->create(); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([]), + 'sorting' => json_encode([ + [ + 'id' => 'age', + 'desc' => false, ] - ]); + ]) + ]; + + $allowedSortings = array('age'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + allowedSortings: $allowedSortings + ); + + $expected = $users->toArray(); + array_multisort( array_column($expected, "age"), SORT_ASC, $expected); + + expect($data['data']) + ->toEqual($expected); expect($data['meta']['totalRowCount']) ->toBe(6); From a1bd61b5dd45f439645c55acbd8dad83f7f2bc81 Mon Sep 17 00:00:00 2001 From: Hamidreza Ranjbarpour Date: Tue, 23 Jul 2024 17:33:51 +0330 Subject: [PATCH 4/6] added test for model's relation filter --- database/factories/PostFactory.php | 24 +++++++ database/factories/UserFactory.php | 8 --- .../migrations/create_posts_table.php.stub | 20 ++++++ tests/DatatableTest.php | 64 +++++++++++++++++-- tests/Models/Post.php | 18 ++++++ tests/Models/User.php | 6 +- tests/TestCase.php | 3 + .../Providers/WorkbenchServiceProvider.php | 25 -------- 8 files changed, 129 insertions(+), 39 deletions(-) create mode 100644 database/factories/PostFactory.php create mode 100644 database/migrations/create_posts_table.php.stub create mode 100644 tests/Models/Post.php delete mode 100644 workbench/app/Providers/WorkbenchServiceProvider.php diff --git a/database/factories/PostFactory.php b/database/factories/PostFactory.php new file mode 100644 index 0000000..4874085 --- /dev/null +++ b/database/factories/PostFactory.php @@ -0,0 +1,24 @@ + $this->faker->name(), + 'description' => $this->faker->text(), + 'user_id' => User::factory(), + ]; + + } +} + diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 20e812d..5c69d2a 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -17,14 +17,6 @@ public function definition() 'username' => $this->faker->userName(), 'email' => $this->faker->email, 'age' => $this->faker->numberBetween(1, 100), -// 'city_id' => City::factory(), -// 'province_id' => function (array $attributes) { -// return City::find($attributes['city_id'])->province_id; -// }, -// 'city_name' => function (array $attributes) { -// return City::find($attributes['city_id'])->name; -// }, - ]; } diff --git a/database/migrations/create_posts_table.php.stub b/database/migrations/create_posts_table.php.stub new file mode 100644 index 0000000..fc562a2 --- /dev/null +++ b/database/migrations/create_posts_table.php.stub @@ -0,0 +1,20 @@ +id(); + $table->foreignId('user_id') + ->constrained(); + $table->string('title'); + $table->text('description'); + $table->timestamps(); + }); + } +}; diff --git a/tests/DatatableTest.php b/tests/DatatableTest.php index 95e0441..f348eeb 100644 --- a/tests/DatatableTest.php +++ b/tests/DatatableTest.php @@ -1,6 +1,7 @@ throws(\HamidRrj\LaravelDatatable\Exceptions\InvalidSortingException::class, "sorting field `name` is not allowed."); -it('can get data with descending sort on age', function (){ +it('can get data with descending sort on age', function () { $users = User::factory() ->count(6) @@ -823,7 +824,7 @@ ); $expected = $users->toArray(); - array_multisort( array_column($expected, "age"), SORT_DESC, $expected); + array_multisort(array_column($expected, "age"), SORT_DESC, $expected); expect($data['data']) ->toEqual($expected); @@ -832,7 +833,7 @@ ->toBe(6); }); -it('can get data with ascending sort on age', function (){ +it('can get data with ascending sort on age', function () { $users = User::factory() ->count(6) @@ -861,7 +862,7 @@ ); $expected = $users->toArray(); - array_multisort( array_column($expected, "age"), SORT_ASC, $expected); + array_multisort(array_column($expected, "age"), SORT_ASC, $expected); expect($data['data']) ->toEqual($expected); @@ -870,6 +871,59 @@ ->toBe(6); }); +it("can get correct data with providing filter for model's relation with fn:`contains` and datatype:`text`", function () { + + $expectedUsers = User::factory() + ->count(3) + ->has(Post::factory() + ->state(new \Illuminate\Database\Eloquent\Factories\Sequence( + ['title' => 'Wow! My post got 10k impressions'], + )) + ) + ->create(); + + User::factory() + ->count(4) + ->has(Post::factory() + ->state(new \Illuminate\Database\Eloquent\Factories\Sequence( + ['title' => 'Lorem ipsum doler sit emet.'], + )) + ) + ->create(); + + $query = User::query(); + + $requestParameters = [ + 'start' => 0, + 'size' => 10, + 'filters' => json_encode([ + [ + 'id' => 'posts.title', + 'value' => 'my post', + 'fn' => 'contains', + 'datatype' => 'text' + ] + ]), + 'sorting' => json_encode([]) + ]; + + $allowedFilters = array('posts.title'); + + $data = DatatableFacade::run( + $query, + $requestParameters, + $allowedFilters + ); + + $expected = $expectedUsers->toArray(); + + expect($data['data']) + ->toEqual($expected); + + expect($data['meta']['totalRowCount']) + ->toBe(3); +}); + // contains: text, numeric DONE :D // equals: text, numeric, date :D // not equals: text, numeric, date :D @@ -878,4 +932,4 @@ //sorting // filter with rel -// sorting with rel +// include relations test :) diff --git a/tests/Models/Post.php b/tests/Models/Post.php new file mode 100644 index 0000000..2a7f24e --- /dev/null +++ b/tests/Models/Post.php @@ -0,0 +1,18 @@ +belongsTo(User::class); + } + +} diff --git a/tests/Models/User.php b/tests/Models/User.php index fbf37d5..ccc0bd3 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -4,11 +4,15 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use HasFactory, Notifiable; + + public function posts(): HasMany + { + return $this->hasMany(Post::class); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 091b310..808e475 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -31,5 +31,8 @@ public function getEnvironmentSetUp($app) $migration = include __DIR__.'/../database/migrations/create_users_table.php.stub'; $migration->up(); + $migration = include __DIR__.'/../database/migrations/create_posts_table.php.stub'; + $migration->up(); + } } diff --git a/workbench/app/Providers/WorkbenchServiceProvider.php b/workbench/app/Providers/WorkbenchServiceProvider.php deleted file mode 100644 index 001d06d..0000000 --- a/workbench/app/Providers/WorkbenchServiceProvider.php +++ /dev/null @@ -1,25 +0,0 @@ - Date: Tue, 23 Jul 2024 17:34:13 +0330 Subject: [PATCH 5/6] updated composer --- composer.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 453dbd7..959cfa2 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "laravel", "laravel-datatable" ], - "homepage": "https://github.com/hamidrrj/laravel-datatable", + "homepage": "https://github.com/hamidrezarj/laravel-datatable", "license": "MIT", "authors": [ { @@ -16,7 +16,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.1|^8.0", "spatie/laravel-package-tools": "^1.14.0", "illuminate/contracts": "^10.0" }, @@ -40,8 +40,7 @@ }, "autoload-dev": { "psr-4": { - "HamidRrj\\LaravelDatatable\\Tests\\": "tests/", - "Workbench\\App\\": "workbench/app/" + "HamidRrj\\LaravelDatatable\\Tests\\": "tests/" } }, "scripts": { From 13cebcc2a11c9fed47c24d49ba4041d8d9055979 Mon Sep 17 00:00:00 2001 From: Hamidreza Ranjbarpour Date: Mon, 5 Aug 2024 10:58:57 +0330 Subject: [PATCH 6/6] fixed some type check issues --- src/Filter/SearchFunctions/FilterGreaterThan.php | 2 +- src/Filter/SearchFunctions/FilterGreaterThanOrEqual.php | 2 +- src/Filter/SearchFunctions/FilterLessThan.php | 2 +- src/Filter/SearchFunctions/FilterLessThanOrEqual.php | 2 +- src/Validators/FilterValidator.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Filter/SearchFunctions/FilterGreaterThan.php b/src/Filter/SearchFunctions/FilterGreaterThan.php index d4c8d33..ad2b25c 100644 --- a/src/Filter/SearchFunctions/FilterGreaterThan.php +++ b/src/Filter/SearchFunctions/FilterGreaterThan.php @@ -9,7 +9,7 @@ class FilterGreaterThan extends SearchFilter { public function apply(): Builder { - $value = ($this->filter->getDatatype() == DataType::NUMERIC) ? + $value = ($this->filter->getDatatype() == DataType::NUMERIC->value) ? (float) $this->filter->getValue() : $this->filter->getValue(); return $this->query->where($this->filter->getId(), '>', $value); diff --git a/src/Filter/SearchFunctions/FilterGreaterThanOrEqual.php b/src/Filter/SearchFunctions/FilterGreaterThanOrEqual.php index ef20e2a..7e41043 100644 --- a/src/Filter/SearchFunctions/FilterGreaterThanOrEqual.php +++ b/src/Filter/SearchFunctions/FilterGreaterThanOrEqual.php @@ -9,7 +9,7 @@ class FilterGreaterThanOrEqual extends SearchFilter { public function apply(): Builder { - $value = ($this->filter->getDatatype() == DataType::NUMERIC) ? + $value = ($this->filter->getDatatype() == DataType::NUMERIC->value) ? (float) $this->filter->getValue() : $this->filter->getValue(); return $this->query->where($this->filter->getId(), '>=', $value); diff --git a/src/Filter/SearchFunctions/FilterLessThan.php b/src/Filter/SearchFunctions/FilterLessThan.php index df226bb..6d77170 100644 --- a/src/Filter/SearchFunctions/FilterLessThan.php +++ b/src/Filter/SearchFunctions/FilterLessThan.php @@ -9,7 +9,7 @@ class FilterLessThan extends SearchFilter { public function apply(): Builder { - $value = ($this->filter->getDatatype() == DataType::NUMERIC) ? + $value = ($this->filter->getDatatype() == DataType::NUMERIC->value) ? (float) $this->filter->getValue() : $this->filter->getValue(); return $this->query->where($this->filter->getId(), '<', $value); diff --git a/src/Filter/SearchFunctions/FilterLessThanOrEqual.php b/src/Filter/SearchFunctions/FilterLessThanOrEqual.php index 6dac492..322b6e3 100644 --- a/src/Filter/SearchFunctions/FilterLessThanOrEqual.php +++ b/src/Filter/SearchFunctions/FilterLessThanOrEqual.php @@ -9,7 +9,7 @@ class FilterLessThanOrEqual extends SearchFilter { public function apply(): Builder { - $value = ($this->filter->getDatatype() == DataType::NUMERIC) ? + $value = ($this->filter->getDatatype() == DataType::NUMERIC->value) ? (float) $this->filter->getValue() : $this->filter->getValue(); return $this->query->where($this->filter->getId(), '<=', $value); diff --git a/src/Validators/FilterValidator.php b/src/Validators/FilterValidator.php index d8342da..552c13c 100644 --- a/src/Validators/FilterValidator.php +++ b/src/Validators/FilterValidator.php @@ -55,7 +55,7 @@ protected function isValidSearchFunction(Filter $filter): bool { $searchFunction = $filter->getFn(); - return isset($searchFunction) && in_array($searchFunction, SearchType::values()); + return $searchFunction && in_array($searchFunction, SearchType::values()); } protected function isValidDataType(Filter $filter): int