From 182e4e14aa294b578e8e43e3c191405ed2d647b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justas=20Lavi=C5=A1ius?= Date: Fri, 1 Feb 2019 14:56:18 +0200 Subject: [PATCH 1/4] MorphTo: instantiate related class appropriate to given parent instance --- .../Database/Eloquent/Relations/MorphTo.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 8d2dc2365aff..93bd9a426de0 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -177,6 +177,17 @@ protected function matchToMorphParents($type, Collection $results) } } + /** + * 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->relation}()->getRelated()->newInstance(); + } + /** * Associate the model instance to the given parent. * From ea6ca4919c848625b589915abca3e00c54b36ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justas=20Lavi=C5=A1ius?= Date: Mon, 4 Feb 2019 12:33:42 +0200 Subject: [PATCH 2/4] Update MorphTo tests to account for new withDefault() behavior MorphTo::withDefault() now always creates a fresh model instance, so we can't know the exact output in advance for use in assertSame(). Instead, use assertEquals() to check that two instances of a model have the same attributes. --- .../Database/DatabaseEloquentMorphToTest.php | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/Database/DatabaseEloquentMorphToTest.php b/tests/Database/DatabaseEloquentMorphToTest.php index 95591a253ea8..337f5314e1bb 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,13 @@ public function testMorphToWithArrayDefault() $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 testAssociateMethodSetsForeignKeyAndTypeOnModel() @@ -165,4 +165,10 @@ 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(); + } } From d21c82bf0dc579f894d7f39a2005fb37a23f72ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justas=20Lavi=C5=A1ius?= Date: Mon, 4 Feb 2019 12:43:06 +0200 Subject: [PATCH 3/4] Add test for MorphTo withDefault behavior when morph type is specified --- .../Database/DatabaseEloquentMorphToTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/Database/DatabaseEloquentMorphToTest.php b/tests/Database/DatabaseEloquentMorphToTest.php index 337f5314e1bb..8da80c2e7684 100644 --- a/tests/Database/DatabaseEloquentMorphToTest.php +++ b/tests/Database/DatabaseEloquentMorphToTest.php @@ -90,6 +90,20 @@ public function testMorphToWithArrayDefault() $this->assertSame('taylor', $result->username); } + public function testMorphToWithSpecifiedClassDefault() + { + $parent = new EloquentMorphToModelStub; + $parent->relation_type = EloquentMorphToRelatedStub::class; + + $relation = $parent->relation()->withDefault(); + + $newModel = new EloquentMorphToRelatedStub; + + $result = $relation->getResults(); + + $this->assertEquals($newModel, $result); + } + public function testAssociateMethodSetsForeignKeyAndTypeOnModel() { $parent = m::mock(Model::class); @@ -172,3 +186,8 @@ public function relation() { return $this->morphTo(); } } + +class EloquentMorphToRelatedStub extends Model +{ + public $table = 'eloquent_morph_to_related_stubs'; +} From f6c83dfcc9468ee081a4a64edc0806be5edf5e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Justas=20Lavi=C5=A1ius?= Date: Mon, 4 Feb 2019 13:43:59 +0200 Subject: [PATCH 4/4] style fix --- tests/Database/DatabaseEloquentMorphToTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentMorphToTest.php b/tests/Database/DatabaseEloquentMorphToTest.php index 8da80c2e7684..2569a8d74da7 100644 --- a/tests/Database/DatabaseEloquentMorphToTest.php +++ b/tests/Database/DatabaseEloquentMorphToTest.php @@ -182,7 +182,8 @@ class EloquentMorphToModelStub extends Model public $table = 'eloquent_morph_to_model_stubs'; - public function relation() { + public function relation() + { return $this->morphTo(); } }