From 23da8330e73ea92efe9a0da388dec10a09fa1920 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 3 Feb 2016 22:09:21 +1100 Subject: [PATCH] Test for detaching belongs to many after delete --- .../database/BelongsToManyModelTest.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/unit/plugins/database/BelongsToManyModelTest.php b/tests/unit/plugins/database/BelongsToManyModelTest.php index 9cff30ca8..2c08255ed 100644 --- a/tests/unit/plugins/database/BelongsToManyModelTest.php +++ b/tests/unit/plugins/database/BelongsToManyModelTest.php @@ -29,8 +29,31 @@ class BelongsToManyModelTest extends PluginTestCase $author->roles()->add($role1); // TODO: Should this occur as part of add() above? + // It probably should simply refresh the relation $author->roles->push($role1); $this->assertTrue($author->roles->contains($role1)); } + + public function testDetachAfterDelete() + { + // Needed for other "delete" events + include_once base_path().'/tests/fixtures/plugins/database/tester/models/User.php'; + include_once base_path().'/tests/fixtures/plugins/database/tester/models/EventLog.php'; + + Model::unguard(); + $author = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']); + $role1 = Role::create(['name' => "Designer", 'description' => "Quality"]); + $role2 = Role::create(['name' => "Programmer", 'description' => "Speed"]); + $role3 = Role::create(['name' => "Manager", 'description' => "Budget"]); + Model::reguard(); + + $author->roles()->add($role1); + $author->roles()->add($role2); + $author->roles()->add($role3); + $this->assertEquals(3, Db::table('database_tester_authors_roles')->where('author_id', $author->id)->count()); + + $author->delete(); + $this->assertEquals(0, Db::table('database_tester_authors_roles')->where('author_id', $author->id)->count()); + } }