diff --git a/tests/Database/DatabaseEloquentHasManyTest.php b/tests/Database/DatabaseEloquentHasManyTest.php index 53f3b2e268be..3f7574d0151b 100755 --- a/tests/Database/DatabaseEloquentHasManyTest.php +++ b/tests/Database/DatabaseEloquentHasManyTest.php @@ -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'])); } @@ -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() @@ -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() @@ -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); @@ -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; }