Skip to content

Commit a1bd61b

Browse files
committed
added test for model's relation filter
1 parent 9cac7d3 commit a1bd61b

File tree

8 files changed

+129
-39
lines changed

8 files changed

+129
-39
lines changed

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: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ public function definition()
1717
'username' => $this->faker->userName(),
1818
'email' => $this->faker->email,
1919
'age' => $this->faker->numberBetween(1, 100),
20-
// 'city_id' => City::factory(),
21-
// 'province_id' => function (array $attributes) {
22-
// return City::find($attributes['city_id'])->province_id;
23-
// },
24-
// 'city_name' => function (array $attributes) {
25-
// return City::find($attributes['city_id'])->name;
26-
// },
27-
2820
];
2921

3022
}
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+
};

tests/DatatableTest.php

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use \HamidRrj\LaravelDatatable\Tests\Models\User;
4+
use \HamidRrj\LaravelDatatable\Tests\Models\Post;
45
use \HamidRrj\LaravelDatatable\Facades\DatatableFacade;
56

67
it('can get data with empty filters', function () {
@@ -794,7 +795,7 @@
794795

795796
})->throws(\HamidRrj\LaravelDatatable\Exceptions\InvalidSortingException::class, "sorting field `name` is not allowed.");
796797

797-
it('can get data with descending sort on age', function (){
798+
it('can get data with descending sort on age', function () {
798799

799800
$users = User::factory()
800801
->count(6)
@@ -823,7 +824,7 @@
823824
);
824825

825826
$expected = $users->toArray();
826-
array_multisort( array_column($expected, "age"), SORT_DESC, $expected);
827+
array_multisort(array_column($expected, "age"), SORT_DESC, $expected);
827828

828829
expect($data['data'])
829830
->toEqual($expected);
@@ -832,7 +833,7 @@
832833
->toBe(6);
833834
});
834835

835-
it('can get data with ascending sort on age', function (){
836+
it('can get data with ascending sort on age', function () {
836837

837838
$users = User::factory()
838839
->count(6)
@@ -861,7 +862,7 @@
861862
);
862863

863864
$expected = $users->toArray();
864-
array_multisort( array_column($expected, "age"), SORT_ASC, $expected);
865+
array_multisort(array_column($expected, "age"), SORT_ASC, $expected);
865866

866867
expect($data['data'])
867868
->toEqual($expected);
@@ -870,6 +871,59 @@
870871
->toBe(6);
871872
});
872873

874+
it("can get correct data with providing filter for model's relation with fn:`contains` and datatype:`text`", function () {
875+
876+
$expectedUsers = User::factory()
877+
->count(3)
878+
->has(Post::factory()
879+
->state(new \Illuminate\Database\Eloquent\Factories\Sequence(
880+
['title' => 'Wow! My post got 10k impressions'],
881+
))
882+
)
883+
->create();
884+
885+
User::factory()
886+
->count(4)
887+
->has(Post::factory()
888+
->state(new \Illuminate\Database\Eloquent\Factories\Sequence(
889+
['title' => 'Lorem ipsum doler sit emet.'],
890+
))
891+
)
892+
->create();
893+
894+
$query = User::query();
895+
896+
$requestParameters = [
897+
'start' => 0,
898+
'size' => 10,
899+
'filters' => json_encode([
900+
[
901+
'id' => 'posts.title',
902+
'value' => 'my post',
903+
'fn' => 'contains',
904+
'datatype' => 'text'
905+
]
906+
]),
907+
'sorting' => json_encode([])
908+
];
909+
910+
$allowedFilters = array('posts.title');
911+
912+
$data = DatatableFacade::run(
913+
$query,
914+
$requestParameters,
915+
$allowedFilters
916+
);
917+
918+
$expected = $expectedUsers->toArray();
919+
920+
expect($data['data'])
921+
->toEqual($expected);
922+
923+
expect($data['meta']['totalRowCount'])
924+
->toBe(3);
925+
});
926+
873927
// contains: text, numeric DONE :D
874928
// equals: text, numeric, date :D
875929
// not equals: text, numeric, date :D
@@ -878,4 +932,4 @@
878932

879933
//sorting
880934
// filter with rel
881-
// sorting with rel
935+
// include relations test :)

tests/Models/Post.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace HamidRrj\LaravelDatatable\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
9+
class Post extends Model
10+
{
11+
use HasFactory;
12+
13+
public function user(): BelongsTo
14+
{
15+
return $this->belongsTo(User::class);
16+
}
17+
18+
}

tests/Models/User.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Relations\HasMany;
7-
use Illuminate\Database\Eloquent\Relations\MorphMany;
87
use Illuminate\Foundation\Auth\User as Authenticatable;
98
use Illuminate\Notifications\Notifiable;
109

1110
class User extends Authenticatable
1211
{
1312
use HasFactory, Notifiable;
13+
14+
public function posts(): HasMany
15+
{
16+
return $this->hasMany(Post::class);
17+
}
1418
}

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ public function getEnvironmentSetUp($app)
3131
$migration = include __DIR__.'/../database/migrations/create_users_table.php.stub';
3232
$migration->up();
3333

34+
$migration = include __DIR__.'/../database/migrations/create_posts_table.php.stub';
35+
$migration->up();
36+
3437
}
3538
}

workbench/app/Providers/WorkbenchServiceProvider.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)