From 5f1542ef4930f761492f1fdd40b738f994856f1f Mon Sep 17 00:00:00 2001 From: Duilio Palacios Date: Mon, 31 Oct 2016 21:12:05 +0000 Subject: [PATCH] Add test for HasOne withDefault --- tests/Database/DatabaseEloquentHasOneTest.php | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/tests/Database/DatabaseEloquentHasOneTest.php b/tests/Database/DatabaseEloquentHasOneTest.php index 9a40bb1c0d56..9c447c58caf5 100755 --- a/tests/Database/DatabaseEloquentHasOneTest.php +++ b/tests/Database/DatabaseEloquentHasOneTest.php @@ -6,11 +6,32 @@ class DatabaseEloquentHasOneTest extends PHPUnit_Framework_TestCase { + protected $builder; + + protected $related; + + protected $parent; + public function tearDown() { m::close(); } + public function testHasOneWithDefault() + { + $relation = $this->getRelation()->withDefault(); + + $this->builder->shouldReceive('first')->once()->andReturnNull(); + + $newModel = m::mock('Illuminate\Database\Eloquent\Model'); + + $newModel->shouldReceive('setAttribute')->once()->with('foreign_key', 1)->andReturn($newModel); + + $this->related->shouldReceive('newInstance')->once()->andReturn($newModel); + + $this->assertInstanceOf('Illuminate\Database\Eloquent\Model', $relation->getResults()); + } + public function testSaveMethodSetsForeignKeyOnModel() { $relation = $this->getRelation(); @@ -113,18 +134,18 @@ public function testRelationCountQueryCanBeBuilt() protected function getRelation() { - $builder = m::mock('Illuminate\Database\Eloquent\Builder'); - $builder->shouldReceive('whereNotNull')->with('table.foreign_key'); - $builder->shouldReceive('where')->with('table.foreign_key', '=', 1); - $related = m::mock('Illuminate\Database\Eloquent\Model'); - $builder->shouldReceive('getModel')->andReturn($related); - $parent = m::mock('Illuminate\Database\Eloquent\Model'); - $parent->shouldReceive('getAttribute')->with('id')->andReturn(1); - $parent->shouldReceive('getCreatedAtColumn')->andReturn('created_at'); - $parent->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at'); - $parent->shouldReceive('newQueryWithoutScopes')->andReturn($builder); - - return new HasOne($builder, $parent, 'table.foreign_key', 'id'); + $this->builder = m::mock('Illuminate\Database\Eloquent\Builder'); + $this->builder->shouldReceive('whereNotNull')->with('table.foreign_key'); + $this->builder->shouldReceive('where')->with('table.foreign_key', '=', 1); + $this->related = m::mock('Illuminate\Database\Eloquent\Model'); + $this->builder->shouldReceive('getModel')->andReturn($this->related); + $this->parent = m::mock('Illuminate\Database\Eloquent\Model'); + $this->parent->shouldReceive('getAttribute')->with('id')->andReturn(1); + $this->parent->shouldReceive('getCreatedAtColumn')->andReturn('created_at'); + $this->parent->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at'); + $this->parent->shouldReceive('newQueryWithoutScopes')->andReturn($this->builder); + + return new HasOne($this->builder, $this->parent, 'table.foreign_key', 'id'); } }