diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index c027f94e818f..46e7d8c7cc3e 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -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; } /** diff --git a/tests/Database/DatabaseEloquentMorphToTest.php b/tests/Database/DatabaseEloquentMorphToTest.php index 9cfdb2283a30..1747b426d215 100644 --- a/tests/Database/DatabaseEloquentMorphToTest.php +++ b/tests/Database/DatabaseEloquentMorphToTest.php @@ -8,6 +8,10 @@ class DatabaseEloquentMorphToTest extends TestCase { + protected $builder; + + protected $related; + public function tearDown() { m::close(); @@ -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'); @@ -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; }