Skip to content

Commit 12e0cc2

Browse files
committed
Add failing grandchild test
1 parent e60a655 commit 12e0cc2

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

tests/CascadeSoftDeletesIntegrationTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ public static function setupBeforeClass()
2121
$manager->setAsGlobal();
2222
$manager->bootEloquent();
2323

24+
$manager->schema()->create('authors', function ($table) {
25+
$table->increments('id');
26+
$table->string('name');
27+
$table->timestamps();
28+
$table->softDeletes();
29+
});
30+
2431
$manager->schema()->create('posts', function ($table) {
2532
$table->increments('id');
33+
$table->integer('author_id')->unsigned()->nullable();
2634
$table->string('title');
2735
$table->string('body');
2836
$table->timestamps();
@@ -170,6 +178,54 @@ public function it_handles_soft_deletes_inherited_from_a_parent_model()
170178
$this->assertCount(0, Tests\Entities\Comment::where('post_id', $post->id)->get());
171179
}
172180

181+
/** @test */
182+
public function it_handles_grandchildren()
183+
{
184+
$author = Tests\Entities\Author::create([
185+
'name' => 'Testing grandchildren are deleted',
186+
]);
187+
188+
$this->attachPostsAndCommentsToAuthor($author);
189+
190+
$author->delete();
191+
192+
$this->assertNull(Tests\Entities\Author::find($author->id));
193+
$this->assertCount(1, Tests\Entities\Author::withTrashed()->where('id', $author->id)->get());
194+
$this->assertCount(0, Tests\Entities\Post::where('author_id', $author->id)->get());
195+
196+
$deletedPosts = Tests\Entities\Post::withTrashed()->where('author_id', $author->id)->get();
197+
$this->assertCount(2, $deletedPosts);
198+
199+
foreach ($deletedPosts as $deletedPost) {
200+
$this->assertCount(0, Tests\Entities\Comment::where('post_id', $deletedPost->id)->get());
201+
}
202+
}
203+
204+
/**
205+
* Attach some dummy posts (w/ comments) to the given author.
206+
*
207+
* @return void
208+
*/
209+
private function attachPostsAndCommentsToAuthor($author)
210+
{
211+
$author->posts()->saveMany([
212+
$this->attachCommentsToPost(
213+
Tests\Entities\Post::create([
214+
'title' => 'First post',
215+
'body' => 'This is the first test post',
216+
])
217+
),
218+
$this->attachCommentsToPost(
219+
Tests\Entities\Post::create([
220+
'title' => 'Second post',
221+
'body' => 'This is the second test post',
222+
])
223+
),
224+
]);
225+
226+
return $author;
227+
}
228+
173229
/**
174230
* Attach some dummy comments to the given post.
175231
*
@@ -182,6 +238,8 @@ private function attachCommentsToPost($post)
182238
new Tests\Entities\Comment(['body' => 'This is the second test comment']),
183239
new Tests\Entities\Comment(['body' => 'This is the third test comment']),
184240
]);
241+
242+
return $post;
185243
}
186244

187245
}

tests/Entities/Author.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tests\Entities;
4+
5+
use Iatstuti\Database\Support\CascadeSoftDeletes;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\SoftDeletes;
8+
9+
class Author extends Model
10+
{
11+
use SoftDeletes, CascadeSoftDeletes;
12+
13+
public $dates = ['deleted_at'];
14+
15+
protected $cascadeDeletes = ['posts'];
16+
17+
protected $fillable = ['name'];
18+
19+
public function posts()
20+
{
21+
return $this->hasMany('Tests\Entities\Post');
22+
}
23+
}

0 commit comments

Comments
 (0)