Skip to content

Commit

Permalink
Add test for HasOne withDefault
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence committed Oct 31, 2016
1 parent 8e06cc0 commit 5f1542e
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}
}

Expand Down

0 comments on commit 5f1542e

Please sign in to comment.