@@ -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}
0 commit comments