From 5e0bc230b150194cfc4aa44becc272a31b9b7655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justas=20Lavi=C5=A1ius?= Date: Fri, 8 Feb 2019 16:58:19 +0200 Subject: [PATCH] MorphTo: instantiate related class appropriate to given parent instance Also update tests to account for new behavior --- .../Database/Eloquent/Relations/MorphTo.php | 11 +++++ .../Database/DatabaseEloquentMorphToTest.php | 44 +++++++++++++++---- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 1386558c320d..6c08ef3e04c0 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -222,6 +222,17 @@ public function touch() } } + /** + * Make a new related instance for the given model. + * + * @param \Illuminate\Database\Eloquent\Model $parent + * @return \Illuminate\Database\Eloquent\Model + */ + protected function newRelatedInstanceFor(Model $parent) + { + return $parent->{$this->getRelationName()}()->getRelated()->newInstance(); + } + /** * Get the foreign key "type" name. * diff --git a/tests/Database/DatabaseEloquentMorphToTest.php b/tests/Database/DatabaseEloquentMorphToTest.php index 95591a253ea8..2569a8d74da7 100644 --- a/tests/Database/DatabaseEloquentMorphToTest.php +++ b/tests/Database/DatabaseEloquentMorphToTest.php @@ -53,9 +53,7 @@ public function testMorphToWithDefault() $newModel = new EloquentMorphToModelStub; - $this->related->shouldReceive('newInstance')->once()->andReturn($newModel); - - $this->assertSame($newModel, $relation->getResults()); + $this->assertEquals($newModel, $relation->getResults()); } public function testMorphToWithDynamicDefault() @@ -67,12 +65,13 @@ public function testMorphToWithDynamicDefault() $this->builder->shouldReceive('first')->once()->andReturnNull(); $newModel = new EloquentMorphToModelStub; + $newModel->username = 'taylor'; - $this->related->shouldReceive('newInstance')->once()->andReturn($newModel); + $result = $relation->getResults(); - $this->assertSame($newModel, $relation->getResults()); + $this->assertEquals($newModel, $result); - $this->assertSame('taylor', $newModel->username); + $this->assertSame('taylor', $result->username); } public function testMorphToWithArrayDefault() @@ -82,12 +81,27 @@ public function testMorphToWithArrayDefault() $this->builder->shouldReceive('first')->once()->andReturnNull(); $newModel = new EloquentMorphToModelStub; + $newModel->username = 'taylor'; + + $result = $relation->getResults(); + + $this->assertEquals($newModel, $result); + + $this->assertSame('taylor', $result->username); + } + + public function testMorphToWithSpecifiedClassDefault() + { + $parent = new EloquentMorphToModelStub; + $parent->relation_type = EloquentMorphToRelatedStub::class; + + $relation = $parent->relation()->withDefault(); - $this->related->shouldReceive('newInstance')->once()->andReturn($newModel); + $newModel = new EloquentMorphToRelatedStub; - $this->assertSame($newModel, $relation->getResults()); + $result = $relation->getResults(); - $this->assertSame('taylor', $newModel->username); + $this->assertEquals($newModel, $result); } public function testAssociateMethodSetsForeignKeyAndTypeOnModel() @@ -165,4 +179,16 @@ public function getRelation($parent = null, $builder = null) class EloquentMorphToModelStub extends Model { public $foreign_key = 'foreign.value'; + + public $table = 'eloquent_morph_to_model_stubs'; + + public function relation() + { + return $this->morphTo(); + } +} + +class EloquentMorphToRelatedStub extends Model +{ + public $table = 'eloquent_morph_to_related_stubs'; }