Skip to content

Commit

Permalink
Merge pull request #10 from hamidrezarj/datatable_test
Browse files Browse the repository at this point in the history
wrote tests for different search functions and datatypes
  • Loading branch information
hamidrezarj authored Aug 5, 2024
2 parents c814b59 + 13cebcc commit d837930
Show file tree
Hide file tree
Showing 19 changed files with 896 additions and 87 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"laravel",
"laravel-datatable"
],
"homepage": "https://github.com/hamidrrj/laravel-datatable",
"homepage": "https://github.com/hamidrezarj/laravel-datatable",
"license": "MIT",
"authors": [
{
Expand All @@ -16,7 +16,7 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.1|^8.0",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0"
},
Expand All @@ -40,8 +40,7 @@
},
"autoload-dev": {
"psr-4": {
"HamidRrj\\LaravelDatatable\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/"
"HamidRrj\\LaravelDatatable\\Tests\\": "tests/"
}
},
"scripts": {
Expand Down
24 changes: 24 additions & 0 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace HamidRrj\LaravelDatatable\Database\Factories;

use HamidRrj\LaravelDatatable\Tests\Models\Post;
use HamidRrj\LaravelDatatable\Tests\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;


class PostFactory extends Factory
{
protected $model = Post::class;

public function definition()
{
return [
'title' => $this->faker->name(),
'description' => $this->faker->text(),
'user_id' => User::factory(),
];

}
}

12 changes: 3 additions & 9 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use HamidRrj\LaravelDatatable\Tests\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;


class UserFactory extends Factory
{
protected $model = User::class;
Expand All @@ -15,16 +16,9 @@ public function definition()
'name' => $this->faker->name(),
'username' => $this->faker->userName(),
'email' => $this->faker->email,
// 'position' => $this->faker->randomElement(['boss']),
// '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;
// },

'age' => $this->faker->numberBetween(1, 100),
];

}
}

20 changes: 20 additions & 0 deletions database/migrations/create_posts_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')
->constrained();
$table->string('title');
$table->text('description');
$table->timestamps();
});
}
};
1 change: 1 addition & 0 deletions database/migrations/create_users_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions src/DatatableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,30 @@ public function __construct(
public function setAllowedFilters(array $allowedFilters): DatatableService
{
$this->allowedFilters = $allowedFilters;

return $this;
}

public function setAllowedRelations(array $allowedRelations): DatatableService
{
$this->allowedRelations = $allowedRelations;

return $this;
}

public function setAllowedSortings(array $allowedSortings): DatatableService
{
$this->allowedSortings = $allowedSortings;

return $this;
}

public function setAllowedSelects(array $allowedSelects): DatatableService
{
$this->allowedSelects = $allowedSelects;

return $this;
}

/**
* Handle 'getData' operations
* @return array
*/
public function getData(): array
{
Expand Down
23 changes: 15 additions & 8 deletions src/Facades/Datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,28 @@ class Datatable
{
/**
* Extracts data from request, passes to datatable service and prepares data for response.
*
* @param Model|Builder $mixed
* @param array $requestParameters
* @param array $allowedFilters
* @param array $allowedRelations
* @param array $allowedSortings
* @param array $allowedSelects
* @return array
* @throws InvalidParameterInterface if input parameters are invalid.
*/
public function run(

Check failure on line 25 in src/Facades/Datatable.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @throws with type HamidRrj\LaravelDatatable\Exceptions\InvalidParameterInterface is not subtype of Throwable
Model|Builder $mixed,
array $requestParameters,
array $allowedFilters = [],
array $allowedRelations = [],
array $allowedSortings = [],
array $allowedSelects = []
): array {
array $requestParameters,
array $allowedFilters = [],
array $allowedRelations = [],
array $allowedSortings = [],
array $allowedSelects = []
): array
{

$filters = json_decode($requestParameters['filters']);
$sorting = json_decode($requestParameters['sorting']);
$rels = array_key_exists('rels', $requestParameters) ? $requestParameters['rels'] : [];
$rels = array_key_exists('rels', $requestParameters) ? $requestParameters['rels'] : array();

$dataTableInput = new DataTableInput(
$requestParameters['start'],
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/SearchFunctions/FilterGreaterThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/SearchFunctions/FilterGreaterThanOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/SearchFunctions/FilterLessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/SearchFunctions/FilterLessThanOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/Sort/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Sort
private static SortingValidator $sortingValidator;

/**
* @param string $id
* @param bool $desc
* @param array $allowedSortings
* @throws InvalidSortingException
*/
public function __construct(

Check failure on line 17 in src/Sort/Sort.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @throws with type HamidRrj\LaravelDatatable\Sort\InvalidSortingException is not subtype of Throwable
Expand Down
2 changes: 1 addition & 1 deletion src/Validators/FilterValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit d837930

Please sign in to comment.