22
33use PHPUnit \Framework \TestCase ;
44use Illuminate \Events \Dispatcher ;
5+ use Iatstuti \Database \Support \CascadeSoftDeleteException ;
56use Illuminate \Container \Container ;
67use Illuminate \Database \Capsule \Manager ;
78
@@ -49,6 +50,17 @@ public static function setupBeforeClass(): void
4950 $ table ->string ('label ' );
5051 $ table ->timestamps ();
5152 });
53+
54+ $ manager ->schema ()->create ('authors__post_types ' , function ($ table ) {
55+
56+ $ table ->increments ('id ' );
57+ $ table ->integer ('author_id ' );
58+ $ table ->integer ('posttype_id ' );
59+ $ table ->timestamps ();
60+
61+ $ table ->foreign ('author_id ' )->references ('id ' )->on ('author ' );
62+ $ table ->foreign ('posttype_id ' )->references ('id ' )->on ('post_types ' );
63+ });
5264 }
5365
5466
@@ -67,6 +79,23 @@ public function it_cascades_deletes_when_deleting_a_parent_model()
6779 $ this ->assertCount (0 , Tests \Entities \Comment::where ('post_id ' , $ post ->id )->get ());
6880 }
6981
82+ /** @test */
83+ public function it_cascades_deletes_entries_from_pivot_table ()
84+ {
85+ $ author = Tests \Entities \Author::create (['name ' => 'ManyToManyTestAuthor ' ]);
86+
87+ $ this ->attachPostTypesToAuthor ($ author );
88+ $ this ->assertCount (2 , $ author ->posttypes );
89+
90+ $ author ->delete ();
91+
92+ $ pivotEntries = Manager::table ('authors__post_types ' )
93+ ->where ('author_id ' , $ author ->id )
94+ ->get ();
95+
96+ $ this ->assertCount (0 , $ pivotEntries );
97+ }
98+
7099 /** @test */
71100 public function it_cascades_deletes_when_force_deleting_a_parent_model ()
72101 {
@@ -88,7 +117,7 @@ public function it_cascades_deletes_when_force_deleting_a_parent_model()
88117 */
89118 public function it_takes_exception_to_models_that_do_not_implement_soft_deletes ()
90119 {
91- $ this ->expectException (LogicException ::class);
120+ $ this ->expectException (CascadeSoftDeleteException ::class);
92121 $ this ->expectExceptionMessage ('Tests\Entities\NonSoftDeletingPost does not implement Illuminate\Database\Eloquent\SoftDeletes ' );
93122
94123 $ post = Tests \Entities \NonSoftDeletingPost::create ([
@@ -106,7 +135,7 @@ public function it_takes_exception_to_models_that_do_not_implement_soft_deletes(
106135 */
107136 public function it_takes_exception_to_models_trying_to_cascade_deletes_on_invalid_relationships ()
108137 {
109- $ this ->expectException (LogicException ::class);
138+ $ this ->expectException (CascadeSoftDeleteException ::class);
110139 $ this ->expectExceptionMessage ('Relationships [invalidRelationship, anotherInvalidRelationship] must exist and return an object of type Illuminate\Database\Eloquent\Relations\Relation ' );
111140
112141 $ post = Tests \Entities \InvalidRelationshipPost::create ([
@@ -131,7 +160,7 @@ public function it_ensures_that_no_deletes_are_performed_if_there_are_invalid_re
131160
132161 try {
133162 $ post ->delete ();
134- } catch (LogicException $ e ) {
163+ } catch (CascadeSoftDeleteException $ e ) {
135164 $ this ->assertNotNull (Tests \Entities \InvalidRelationshipPost::find ($ post ->id ));
136165 $ this ->assertCount (3 , Tests \Entities \Comment::where ('post_id ' , $ post ->id )->get ());
137166 }
@@ -156,10 +185,11 @@ public function it_can_accept_cascade_deletes_as_a_single_string()
156185
157186 /**
158187 * @test
188+
159189 */
160190 public function it_handles_situations_where_the_relationship_method_does_not_exist ()
161191 {
162- $ this ->expectException (LogicException ::class);
192+ $ this ->expectException (CascadeSoftDeleteException ::class);
163193 $ this ->expectExceptionMessage ('Relationship [comments] must exist and return an object of type Illuminate\Database\Eloquent\Relations\Relation ' );
164194
165195 $ post = Tests \Entities \PostWithMissingRelationshipMethod::create ([
@@ -226,6 +256,25 @@ public function it_cascades_a_has_one_relationship()
226256 $ this ->assertCount (0 , Tests \Entities \PostType::where ('id ' , $ type ->id )->get ());
227257 }
228258
259+ /**
260+ * Attach some post types to the given author.
261+ *
262+ * @return void
263+ */
264+ public function attachPostTypesToAuthor ($ author )
265+ {
266+ $ author ->posttypes ()->saveMany ([
267+
268+ Tests \Entities \PostType::create ([
269+ 'label ' => 'First Post Type ' ,
270+ ]),
271+
272+ Tests \Entities \PostType::create ([
273+ 'label ' => 'Second Post Type ' ,
274+ ]),
275+ ]);
276+ }
277+
229278 /**
230279 * Attach some dummy posts (w/ comments) to the given author.
231280 *
0 commit comments