Skip to content

Commit

Permalink
Add withDefault() support to MorphTo relationships (#24061)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed May 2, 2018
1 parent cc22d50 commit 43e4bc3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function buildDictionary(Collection $models)
*/
public function getResults()
{
return $this->ownerKey ? $this->query->first() : null;
return $this->ownerKey ? parent::getResults() : null;
}

/**
Expand Down
63 changes: 56 additions & 7 deletions tests/Database/DatabaseEloquentMorphToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

class DatabaseEloquentMorphToTest extends TestCase
{
protected $builder;

protected $related;

public function tearDown()
{
m::close();
Expand Down Expand Up @@ -39,6 +43,51 @@ public function testLookupDictionaryIsProperlyConstructed()
], $dictionary);
}

public function testMorphToWithDefault()
{
$relation = $this->getRelation()->withDefault();

$this->builder->shouldReceive('first')->once()->andReturnNull();

$newModel = new EloquentMorphToModelStub();

$this->related->shouldReceive('newInstance')->once()->andReturn($newModel);

$this->assertSame($newModel, $relation->getResults());
}

public function testMorphToWithDynamicDefault()
{
$relation = $this->getRelation()->withDefault(function ($newModel) {
$newModel->username = 'taylor';
});

$this->builder->shouldReceive('first')->once()->andReturnNull();

$newModel = new EloquentMorphToModelStub();

$this->related->shouldReceive('newInstance')->once()->andReturn($newModel);

$this->assertSame($newModel, $relation->getResults());

$this->assertSame('taylor', $newModel->username);
}

public function testMorphToWithArrayDefault()
{
$relation = $this->getRelation()->withDefault(['username' => 'taylor']);

$this->builder->shouldReceive('first')->once()->andReturnNull();

$newModel = new EloquentMorphToModelStub();

$this->related->shouldReceive('newInstance')->once()->andReturn($newModel);

$this->assertSame($newModel, $relation->getResults());

$this->assertSame('taylor', $newModel->username);
}

public function testAssociateMethodSetsForeignKeyAndTypeOnModel()
{
$parent = m::mock('Illuminate\Database\Eloquent\Model');
Expand Down Expand Up @@ -99,14 +148,14 @@ protected function getRelationAssociate($parent)

public function getRelation($parent = null, $builder = null)
{
$builder = $builder ?: m::mock('Illuminate\Database\Eloquent\Builder');
$builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
$related = m::mock('Illuminate\Database\Eloquent\Model');
$related->shouldReceive('getKeyName')->andReturn('id');
$related->shouldReceive('getTable')->andReturn('relation');
$builder->shouldReceive('getModel')->andReturn($related);
$this->builder = $builder ?: m::mock('Illuminate\Database\Eloquent\Builder');
$this->builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
$this->related = m::mock('Illuminate\Database\Eloquent\Model');
$this->related->shouldReceive('getKeyName')->andReturn('id');
$this->related->shouldReceive('getTable')->andReturn('relation');
$this->builder->shouldReceive('getModel')->andReturn($this->related);
$parent = $parent ?: new EloquentMorphToModelStub;
$morphTo = m::mock('Illuminate\Database\Eloquent\Relations\MorphTo[createModelByType]', [$builder, $parent, 'foreign_key', 'id', 'morph_type', 'relation']);
$morphTo = m::mock('Illuminate\Database\Eloquent\Relations\MorphTo[createModelByType]', [$this->builder, $parent, 'foreign_key', 'id', 'morph_type', 'relation']);

return $morphTo;
}
Expand Down

0 comments on commit 43e4bc3

Please sign in to comment.