diff --git a/tests/unit/plugins/database/HasOneModelTest.php b/tests/unit/plugins/database/HasOneModelTest.php index 02251902b..7a5e30157 100644 --- a/tests/unit/plugins/database/HasOneModelTest.php +++ b/tests/unit/plugins/database/HasOneModelTest.php @@ -30,6 +30,10 @@ class HasOneModelTest extends PluginTestCase $this->assertEquals($author->id, $phone1->author_id); $this->assertEquals('0404040404', $author->phone->number); + // Double check + $phone1 = Phone::find($phone1->id); + $this->assertEquals($author->id, $phone1->author_id); + // Set by primary key $phoneId = $phone2->id; $author->phone = $phoneId; @@ -38,6 +42,10 @@ class HasOneModelTest extends PluginTestCase $this->assertEquals($author->id, $phone2->author_id); $this->assertEquals('0505050505', $author->phone->number); + // Ensure relationship is "stolen" from first model + $phone1 = Phone::find($phone1->id); + $this->assertNotEquals($author->id, $phone1->author_id); + // Nullify $author->phone = null; $author->save(); @@ -51,6 +59,25 @@ class HasOneModelTest extends PluginTestCase $this->assertEquals($author->id, $phone3->author_id); } + public function testSetRelationValueTwice() + { + Model::unguard(); + $author = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']); + $phone = Phone::create(['number' => '0505050505']); + Model::reguard(); + + $phoneId = $phone->id; + $author->phone = $phoneId; + $author->save(); + + $author->phone = $phoneId; + $author->save(); + + $phone = Phone::find($phoneId); + $this->assertEquals($author->id, $phone->author_id); + $this->assertEquals('0505050505', $author->phone->number); + } + public function testGetRelationValue() { Model::unguard(); diff --git a/tests/unit/plugins/database/MorphOneModelTest.php b/tests/unit/plugins/database/MorphOneModelTest.php index d8cb43ca3..4051e0b0c 100644 --- a/tests/unit/plugins/database/MorphOneModelTest.php +++ b/tests/unit/plugins/database/MorphOneModelTest.php @@ -59,6 +59,11 @@ class MorphOneModelTest extends PluginTestCase $this->assertEquals(get_class($post), $meta1->taggable_type); $this->assertEquals('Question', $post->meta->meta_title); + // Double check + $meta1 = Meta::find($meta1->id); + $this->assertEquals($post->id, $meta1->taggable_id); + $this->assertEquals(get_class($post), $meta1->taggable_type); + // Set by primary key $metaId = $meta2->id; $author->meta = $metaId; @@ -82,6 +87,33 @@ class MorphOneModelTest extends PluginTestCase $this->assertEquals($author->id, $meta3->taggable_id); } + public function testSetRelationValueTwice() + { + Model::unguard(); + $author = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']); + $meta = Meta::create([ + 'meta_title' => 'Question', + 'meta_description' => 'Industry', + 'meta_keywords' => 'major', + 'canonical_url' => 'http://google.com/search/jobs', + 'redirect_url' => 'http://google.com', + 'robot_index' => 'index', + 'robot_follow' => 'follow', + ]); + Model::reguard(); + + $metaId = $meta->id; + $author->meta = $metaId; + $author->save(); + + $author->meta = $metaId; + $author->save(); + + $meta = Meta::find($metaId); + $this->assertEquals($author->id, $meta->taggable_id); + $this->assertEquals(get_class($author), $meta->taggable_type); + } + public function testGetRelationValue() { Model::unguard();