Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Aug 28, 2023
1 parent 892c9ba commit 5e1d32f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));
$relation->getRelated()->shouldReceive('newInstance')->never();
$model->shouldReceive('fill')->once()->with(['bar']);

$model->wasRecentlyCreated = false;
$model->shouldReceive('fill')->once()->with(['bar'])->andReturn($model);
$model->shouldReceive('save')->once();

$this->assertInstanceOf(stdClass::class, $relation->updateOrCreate(['foo'], ['bar']));
Expand All @@ -236,11 +238,15 @@ public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
public function testUpdateOrCreateMethodCreatesNewModelWithForeignKeySet()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
return $scope();
});
$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(Model::class));
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo', 'bar'])->andReturn($model = m::mock(Model::class));

$model->wasRecentlyCreated = true;
$model->shouldReceive('save')->once()->andReturn(true);
$model->shouldReceive('fill')->once()->with(['bar']);
$model->shouldReceive('setAttribute')->once()->with('foreign_key', 1);

$this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
Expand Down
12 changes: 9 additions & 3 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,10 @@ public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
$relation->getRelated()->shouldReceive('newInstance')->never();

$model->wasRecentlyCreated = false;
$model->shouldReceive('setAttribute')->never();
$model->shouldReceive('fill')->once()->with(['bar']);
$model->shouldReceive('fill')->once()->with(['bar'])->andReturn($model);
$model->shouldReceive('save')->once();

$this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
Expand All @@ -312,13 +314,17 @@ public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
public function testUpdateOrCreateMethodCreatesNewMorphModel()
{
$relation = $this->getOneRelation();
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
return $scope();
});
$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(Model::class));
$relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo', 'bar'])->andReturn($model = m::mock(Model::class));

$model->wasRecentlyCreated = true;
$model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
$model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
$model->shouldReceive('save')->once()->andReturn(true);
$model->shouldReceive('fill')->once()->with(['bar']);

$this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
}
Expand Down

0 comments on commit 5e1d32f

Please sign in to comment.