Skip to content

Commit

Permalink
add missing test for morphedByMany
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin committed Oct 15, 2023
1 parent e827d0a commit 4e625a3
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/Database/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,47 @@ public function testDeleteOnMorphToManyRelation()
$this->assertEquals(2, Image::count());
}

// tests morphedByMany
public function testDeleteOnMorphedByManyRelation()
{
$image = $this->seeded['images'][0];

$this->assertEquals(2, Image::count());
$this->assertEquals(2, Post::count());

$this->assertEquals(1, $image->posts()->count());

$this->assertEquals(2, DB::table('imageables')->count());

$image->delete();

// verify that pivot record has been removed
$this->assertEquals(1, DB::table('imageables')->count());

// verify image has been deleted
$this->assertEquals(1, Image::count());

// verify post still exists
$this->assertEquals(2, Post::count());

// test with relation "detach" flag set to false (default is true)
Image::extend(function ($model) {
$model->morphedByMany['posts']['detach'] = false;
});

$image = Image::find($this->seeded['images'][1]->id);

$this->assertEquals(1, $image->posts()->count());

$image->delete();

// verify that pivot record has NOT been removed
$this->assertEquals(1, DB::table('imageables')->count());

$this->assertEquals(0, Image::count());
$this->assertEquals(2, Post::count());
}

// tests hasOne
public function testDeleteOnHasOneRelation()
{
Expand Down

0 comments on commit 4e625a3

Please sign in to comment.