Skip to content

Commit d837930

Browse files
authored
Merge pull request #10 from hamidrezarj/datatable_test
wrote tests for different search functions and datatypes
2 parents c814b59 + 13cebcc commit d837930

19 files changed

+896
-87
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ It contains various search logics for different datatypes (numeric, text, date):
1414
5. Equals
1515
6. NotEquals
1616

17-
You can add custom your own search logic and datatype with ease :)
17+
You can add your own search logic and datatype with ease :)
1818

1919
## Installation
2020

@@ -77,7 +77,7 @@ Please review [our security policy](../../security/policy) on how to report secu
7777

7878
## Credits
7979

80-
- [Hamidreza Ranjbarpour](https://github.com/hamidrezaRanjbarpour)
80+
- [Hamidreza Ranjbarpour](https://github.com/hamidrezarj)
8181
- [All Contributors](../../contributors)
8282

8383
## License

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"laravel",
77
"laravel-datatable"
88
],
9-
"homepage": "https://github.com/hamidrrj/laravel-datatable",
9+
"homepage": "https://github.com/hamidrezarj/laravel-datatable",
1010
"license": "MIT",
1111
"authors": [
1212
{
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.1",
19+
"php": "^8.1|^8.0",
2020
"spatie/laravel-package-tools": "^1.14.0",
2121
"illuminate/contracts": "^10.0"
2222
},
@@ -40,8 +40,7 @@
4040
},
4141
"autoload-dev": {
4242
"psr-4": {
43-
"HamidRrj\\LaravelDatatable\\Tests\\": "tests/",
44-
"Workbench\\App\\": "workbench/app/"
43+
"HamidRrj\\LaravelDatatable\\Tests\\": "tests/"
4544
}
4645
},
4746
"scripts": {

database/factories/PostFactory.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace HamidRrj\LaravelDatatable\Database\Factories;
4+
5+
use HamidRrj\LaravelDatatable\Tests\Models\Post;
6+
use HamidRrj\LaravelDatatable\Tests\Models\User;
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
9+
10+
class PostFactory extends Factory
11+
{
12+
protected $model = Post::class;
13+
14+
public function definition()
15+
{
16+
return [
17+
'title' => $this->faker->name(),
18+
'description' => $this->faker->text(),
19+
'user_id' => User::factory(),
20+
];
21+
22+
}
23+
}
24+

database/factories/UserFactory.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use HamidRrj\LaravelDatatable\Tests\Models\User;
66
use Illuminate\Database\Eloquent\Factories\Factory;
77

8+
89
class UserFactory extends Factory
910
{
1011
protected $model = User::class;
@@ -15,16 +16,9 @@ public function definition()
1516
'name' => $this->faker->name(),
1617
'username' => $this->faker->userName(),
1718
'email' => $this->faker->email,
18-
// 'position' => $this->faker->randomElement(['boss']),
19-
// 'city_id' => City::factory(),
20-
// 'province_id' => function (array $attributes) {
21-
// return City::find($attributes['city_id'])->province_id;
22-
// },
23-
// 'city_name' => function (array $attributes) {
24-
// return City::find($attributes['city_id'])->name;
25-
// },
26-
19+
'age' => $this->faker->numberBetween(1, 100),
2720
];
2821

2922
}
3023
}
24+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('posts', function (Blueprint $table) {
12+
$table->id();
13+
$table->foreignId('user_id')
14+
->constrained();
15+
$table->string('title');
16+
$table->text('description');
17+
$table->timestamps();
18+
});
19+
}
20+
};

database/migrations/create_users_table.php.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ return new class extends Migration
1111
Schema::create('users', function (Blueprint $table) {
1212
$table->id();
1313
$table->string('name');
14+
$table->integer('age');
1415
$table->string('username')->unique();
1516
$table->string('email')->unique();
1617
$table->timestamps();

src/DatatableService.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,30 @@ public function __construct(
2626
public function setAllowedFilters(array $allowedFilters): DatatableService
2727
{
2828
$this->allowedFilters = $allowedFilters;
29-
3029
return $this;
3130
}
3231

3332
public function setAllowedRelations(array $allowedRelations): DatatableService
3433
{
3534
$this->allowedRelations = $allowedRelations;
36-
3735
return $this;
3836
}
3937

4038
public function setAllowedSortings(array $allowedSortings): DatatableService
4139
{
4240
$this->allowedSortings = $allowedSortings;
43-
4441
return $this;
4542
}
4643

4744
public function setAllowedSelects(array $allowedSelects): DatatableService
4845
{
4946
$this->allowedSelects = $allowedSelects;
50-
5147
return $this;
5248
}
5349

5450
/**
5551
* Handle 'getData' operations
52+
* @return array
5653
*/
5754
public function getData(): array
5855
{

src/Facades/Datatable.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,28 @@ class Datatable
1313
{
1414
/**
1515
* Extracts data from request, passes to datatable service and prepares data for response.
16-
*
16+
* @param Model|Builder $mixed
17+
* @param array $requestParameters
18+
* @param array $allowedFilters
19+
* @param array $allowedRelations
20+
* @param array $allowedSortings
21+
* @param array $allowedSelects
22+
* @return array
1723
* @throws InvalidParameterInterface if input parameters are invalid.
1824
*/
1925
public function run(
2026
Model|Builder $mixed,
21-
array $requestParameters,
22-
array $allowedFilters = [],
23-
array $allowedRelations = [],
24-
array $allowedSortings = [],
25-
array $allowedSelects = []
26-
): array {
27+
array $requestParameters,
28+
array $allowedFilters = [],
29+
array $allowedRelations = [],
30+
array $allowedSortings = [],
31+
array $allowedSelects = []
32+
): array
33+
{
2734

2835
$filters = json_decode($requestParameters['filters']);
2936
$sorting = json_decode($requestParameters['sorting']);
30-
$rels = array_key_exists('rels', $requestParameters) ? $requestParameters['rels'] : [];
37+
$rels = array_key_exists('rels', $requestParameters) ? $requestParameters['rels'] : array();
3138

3239
$dataTableInput = new DataTableInput(
3340
$requestParameters['start'],

src/Filter/SearchFunctions/FilterGreaterThan.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class FilterGreaterThan extends SearchFilter
99
{
1010
public function apply(): Builder
1111
{
12-
$value = ($this->filter->getDatatype() == DataType::NUMERIC) ?
12+
$value = ($this->filter->getDatatype() == DataType::NUMERIC->value) ?
1313
(float) $this->filter->getValue() : $this->filter->getValue();
1414

1515
return $this->query->where($this->filter->getId(), '>', $value);

src/Filter/SearchFunctions/FilterGreaterThanOrEqual.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class FilterGreaterThanOrEqual extends SearchFilter
99
{
1010
public function apply(): Builder
1111
{
12-
$value = ($this->filter->getDatatype() == DataType::NUMERIC) ?
12+
$value = ($this->filter->getDatatype() == DataType::NUMERIC->value) ?
1313
(float) $this->filter->getValue() : $this->filter->getValue();
1414

1515
return $this->query->where($this->filter->getId(), '>=', $value);

0 commit comments

Comments
 (0)