Skip to content

Commit

Permalink
MorphTo: instantiate related class appropriate to given parent instance
Browse files Browse the repository at this point in the history
Also update tests to account for new behavior
  • Loading branch information
bucaneer committed Feb 8, 2019
1 parent 31cb80a commit 5e0bc23
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
44 changes: 35 additions & 9 deletions tests/Database/DatabaseEloquentMorphToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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';
}

0 comments on commit 5e0bc23

Please sign in to comment.