Skip to content

Commit

Permalink
Dry out some tests that expect new or created models with specific at…
Browse files Browse the repository at this point in the history
…tributes

See laravel#15943
  • Loading branch information
colindecarlo committed Oct 16, 2016
1 parent 98a44c7 commit e820cb7
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ public function tearDown()
public function testCreateMethodProperlyCreatesNewModel()
{
$relation = $this->getRelation();
$created = $this->getMockBuilder('Illuminate\Database\Eloquent\Model')->setMethods(['save', /*'getKey',*/ 'setAttribute'])->getMock();
$created->expects($this->once())->method('save')->will($this->returnValue(true));
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['name' => 'taylor'])->andReturn($created);
$created->expects($this->once())->method('setAttribute')->with('foreign_key', 1);
$created = $this->expectCreatedModel($relation, ['name' => 'taylor']);

$this->assertEquals($created, $relation->create(['name' => 'taylor']));
}
Expand Down Expand Up @@ -56,10 +53,9 @@ public function testFirstOrNewMethodReturnsNewModelWithForeignKeySet()
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo'])->andReturn($model = m::mock('StdClass'));
$model->shouldReceive('setAttribute')->once()->with('foreign_key', 1);
$model = $this->expectNewModel($relation, ['foo']);

$this->assertInstanceOf(StdClass::class, $relation->firstOrNew(['foo']));
$this->assertEquals($model, $relation->firstOrNew(['foo']));
}

public function testFirstOrCreateMethodFindsFirstModel()
Expand All @@ -79,11 +75,9 @@ public function testFirstOrCreateMethodCreatesNewModelWithForeignKeySet()
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo'])->andReturn($model = m::mock('StdClass'));
$model->shouldReceive('save')->once()->andReturn(true);
$model->shouldReceive('setAttribute')->once()->with('foreign_key', 1);
$model = $this->expectCreatedModel($relation, ['foo']);

$this->assertInstanceOf(StdClass::class, $relation->firstOrCreate(['foo']));
$this->assertEquals($model, $relation->firstOrCreate(['foo']));
}

public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
Expand Down Expand Up @@ -187,8 +181,8 @@ public function testCreateManyCreatesARelatedModelForEachRecord()
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('newCollection')->once()->andReturn(new Collection);

$taylor = $this->expectNewlyCreatedModel($relation, ['name' => 'taylor']);
$colin = $this->expectNewlyCreatedModel($relation, ['name' => 'colin']);
$taylor = $this->expectCreatedModel($relation, ['name' => 'taylor']);
$colin = $this->expectCreatedModel($relation, ['name' => 'colin']);

$instances = $relation->createMany($records);
$this->assertInstanceOf(Collection::class, $instances);
Expand All @@ -211,12 +205,19 @@ protected function getRelation()
return new HasMany($builder, $parent, 'table.foreign_key', 'id');
}

protected function expectNewlyCreatedModel($relation, $attributes)
protected function expectNewModel($relation, $attributes = null)
{
$model = $this->getMockBuilder('Illuminate\Database\Eloquent\Model')->setMethods(['setAttribute', 'save'])->getMock();
$relation->getRelated()->shouldReceive('newInstance')->with($attributes)->andReturn($model);
$model->expects($this->once())->method('setAttribute')->with('foreign_key', 1);

return $model;
}

protected function expectCreatedModel($relation, $attributes)
{
$model = $this->expectNewModel($relation, $attributes);
$model->expects($this->once())->method('save');
$relation->getRelated()->shouldReceive('newInstance')->once()->with($attributes)->andReturn($model);

return $model;
}
Expand Down

0 comments on commit e820cb7

Please sign in to comment.