Skip to content

Commit 3c4334a

Browse files
committed
Added tests for relations with soft deletes
1 parent 14d85a4 commit 3c4334a

File tree

5 files changed

+107
-2
lines changed

5 files changed

+107
-2
lines changed

tests/Integration/HasManyRelationTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Yajra\DataTables\Tests\Integration;
44

55
use Yajra\DataTables\DataTables;
6+
use Yajra\DataTables\Tests\Models\Post;
67
use Yajra\DataTables\Tests\TestCase;
78
use Yajra\DataTables\Tests\Models\User;
89
use Illuminate\Foundation\Testing\DatabaseTransactions;
@@ -25,6 +26,37 @@ public function it_returns_all_records_with_the_relation_when_called_without_par
2526
$this->assertCount(20, $response->json()['data']);
2627
}
2728

29+
/** @test */
30+
public function it_returns_all_records_with_deleted_relations_when_called_with_withtrashed_parameter()
31+
{
32+
Post::find(1)->delete();
33+
34+
$response = $this->call('GET', '/relations/hasManyWithTrashed');
35+
$response->assertJson([
36+
'draw' => 0,
37+
'recordsTotal' => 20,
38+
'recordsFiltered' => 20,
39+
]);
40+
41+
$this->assertArrayHasKey('posts', $response->json()['data'][0]);
42+
$this->assertCount(3, $response->json()['data'][0]['posts']);
43+
}
44+
45+
/** @test */
46+
public function it_returns_all_records_with_only_deleted_relations_when_called_with_onlytrashed_parameter()
47+
{
48+
Post::find(1)->delete();
49+
$response = $this->call('GET', '/relations/hasManyOnlyTrashed');
50+
$response->assertJson([
51+
'draw' => 0,
52+
'recordsTotal' => 20,
53+
'recordsFiltered' => 20,
54+
]);
55+
56+
$this->assertArrayHasKey('posts', $response->json()['data'][0]);
57+
$this->assertCount(1, $response->json()['data'][0]['posts']);
58+
}
59+
2860
/** @test */
2961
public function it_can_perform_global_search_on_the_relation()
3062
{
@@ -60,5 +92,17 @@ protected function setUp()
6092
$this->app['router']->get('/relations/hasMany', function (DataTables $datatables) {
6193
return $datatables->eloquent(User::with('posts')->select('users.*'))->toJson();
6294
});
95+
96+
$this->app['router']->get('/relations/hasManyWithTrashed', function (DataTables $datatables) {
97+
return $datatables->eloquent(User::with(['posts' => function($query){
98+
$query->withTrashed();
99+
}])->select('users.*'))->toJson();
100+
});
101+
102+
$this->app['router']->get('/relations/hasManyOnlyTrashed', function (DataTables $datatables) {
103+
return $datatables->eloquent(User::with(['posts' => function($query){
104+
$query->onlyTrashed();
105+
}])->select('users.*'))->toJson();
106+
});
63107
}
64108
}

tests/Integration/HasOneRelationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Yajra\DataTables\Tests\Integration;
44

55
use Yajra\DataTables\DataTables;
6+
use Yajra\DataTables\Tests\Models\Heart;
67
use Yajra\DataTables\Tests\TestCase;
78
use Yajra\DataTables\Tests\Models\User;
89
use Illuminate\Foundation\Testing\DatabaseTransactions;
@@ -25,6 +26,42 @@ public function it_returns_all_records_with_the_relation_when_called_without_par
2526
$this->assertCount(20, $response->json()['data']);
2627
}
2728

29+
/** @test */
30+
public function it_returns_all_records_with_the_deleted_relation_when_called_with_withtrashed_parameter()
31+
{
32+
Heart::find(1)->delete();
33+
34+
$response = $this->call('GET', '/relations/hasOneWithTrashed');
35+
$response->assertJson([
36+
'draw' => 0,
37+
'recordsTotal' => 20,
38+
'recordsFiltered' => 20,
39+
]);
40+
41+
$this->assertArrayHasKey('heart', $response->json()['data'][0]);
42+
$this->assertArrayHasKey('heart', $response->json()['data'][1]);
43+
$this->assertNotEmpty($response->json()['data'][0]['heart']);
44+
$this->assertNotEmpty($response->json()['data'][1]['heart']);
45+
}
46+
47+
/** @test */
48+
public function it_returns_all_records_with_the_only_deleted_relation_when_called_with_onlytrashed_parameter()
49+
{
50+
Heart::find(1)->delete();
51+
52+
$response = $this->call('GET', '/relations/hasOneOnlyTrashed');
53+
$response->assertJson([
54+
'draw' => 0,
55+
'recordsTotal' => 20,
56+
'recordsFiltered' => 20,
57+
]);
58+
59+
$this->assertArrayHasKey('heart', $response->json()['data'][0]);
60+
$this->assertArrayHasKey('heart', $response->json()['data'][1]);
61+
$this->assertNotEmpty($response->json()['data'][0]['heart']);
62+
$this->assertEmpty($response->json()['data'][1]['heart']);
63+
}
64+
2865
/** @test */
2966
public function it_can_perform_global_search_on_the_relation()
3067
{
@@ -86,5 +123,17 @@ protected function setUp()
86123
$this->app['router']->get('/relations/hasOne', function (DataTables $datatables) {
87124
return $datatables->eloquent(User::with('heart')->select('users.*'))->toJson();
88125
});
126+
127+
$this->app['router']->get('/relations/hasOneWithTrashed', function (DataTables $datatables) {
128+
return $datatables->eloquent(User::with(['heart' => function($query){
129+
$query->withTrashed();
130+
}])->select('users.*'))->toJson();
131+
});
132+
133+
$this->app['router']->get('/relations/hasOneOnlyTrashed', function (DataTables $datatables) {
134+
return $datatables->eloquent(User::with(['heart' => function($query){
135+
$query->onlyTrashed();
136+
}])->select('users.*'))->toJson();
137+
});
89138
}
90139
}

tests/Models/Heart.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
namespace Yajra\DataTables\Tests\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
67

78
class Heart extends Model
89
{
9-
protected $guarded = [];
10+
use SoftDeletes;
11+
12+
protected $guarded = [];
13+
14+
protected $dates = ['deleted_at'];
1015

1116
public function user()
1217
{

tests/Models/Post.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
namespace Yajra\DataTables\Tests\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
67

78
class Post extends Model
89
{
9-
protected $guarded = [];
10+
use SoftDeletes;
11+
12+
protected $guarded = [];
13+
14+
protected $dates = ['deleted_at'];
1015

1116
public function user()
1217
{

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function migrateDatabase()
3636
$table->string('title');
3737
$table->unsignedInteger('user_id');
3838
$table->timestamps();
39+
$table->softDeletes();
3940
});
4041
}
4142
if (! $schemaBuilder->hasTable('hearts')) {
@@ -44,6 +45,7 @@ protected function migrateDatabase()
4445
$table->unsignedInteger('user_id');
4546
$table->string('size');
4647
$table->timestamps();
48+
$table->softDeletes();
4749
});
4850
}
4951
if (! $schemaBuilder->hasTable('roles')) {

0 commit comments

Comments
 (0)