Skip to content

Commit

Permalink
test: 💍 Add BelongsToMany::updateOrCreate snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Oct 6, 2023
1 parent 4cefa9c commit fab4ee3
Showing 1 changed file with 113 additions and 7 deletions.
120 changes: 113 additions & 7 deletions tests/Database/DatabaseEloquentBelongsToManyCreateOrFirstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,24 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore
{
$relation = Mockery::mock(BelongsToMany::class)->makePartial();
$relation->__construct(...func_get_args());
$instance = new BelongsToManyCreateOrFirstTestRelatedModel([
'id' => 456,
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
'pivot' => [
'source_id' => 123,
'related_id' => 456,
],
]);
$instance->exists = true;
$instance->wasRecentlyCreated = false;
$instance->syncOriginal();
$relation
->expects('createOrFirst')
->with(['attr' => 'foo'], ['val' => 'bar'], [], true)
->andReturn(new BelongsToManyCreateOrFirstTestRelatedModel([
'id' => 456,
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
]));
->andReturn($instance);

return $relation;
}
Expand Down Expand Up @@ -333,6 +341,104 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
'pivot' => [
'source_id' => 123,
'related_id' => 456,
],
], $result->toArray());
}

public function testUpdateOrCreateMethodCreatesNewRelated(): void
{
$source = new class() extends BelongsToManyCreateOrFirstTestSourceModel
{
protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null): BelongsToMany
{
$relation = Mockery::mock(BelongsToMany::class)->makePartial();
$relation->__construct(...func_get_args());
$instance = new BelongsToManyCreateOrFirstTestRelatedModel([
'id' => 456,
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
]);
$instance->exists = true;
$instance->wasRecentlyCreated = true;
$instance->syncOriginal();
$relation
->expects('firstOrCreate')
->with(['attr' => 'foo'], ['val' => 'baz'], [], true)
->andReturn($instance);

return $relation;
}
};
$source->id = 123;
$this->mockConnectionForModels(
[$source, new BelongsToManyCreateOrFirstTestRelatedModel()],
'SQLite',
);

$result = $source->related()->updateOrCreate(['attr' => 'foo'], ['val' => 'baz']);
$this->assertEquals([
'id' => 456,
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
], $result->toArray());
}

public function testUpdateOrCreateMethodUpdatesExistingRelated(): void
{
$source = new class() extends BelongsToManyCreateOrFirstTestSourceModel
{
protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null): BelongsToMany
{
$relation = Mockery::mock(BelongsToMany::class)->makePartial();
$relation->__construct(...func_get_args());
$instance = new BelongsToManyCreateOrFirstTestRelatedModel([
'id' => 456,
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
]);
$instance->exists = true;
$instance->wasRecentlyCreated = false;
$instance->syncOriginal();
$relation
->expects('firstOrCreate')
->with(['attr' => 'foo'], ['val' => 'baz'], [], true)
->andReturn($instance);

return $relation;
}
};
$source->id = 123;
$this->mockConnectionForModels(
[$source, new BelongsToManyCreateOrFirstTestRelatedModel()],
'SQLite',
);
$source->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
$source->getConnection()->shouldReceive('getName')->andReturn('sqlite');

$source->getConnection()
->expects('update')
->with(
'update "related_table" set "val" = ?, "updated_at" = ? where "id" = ?',
['baz', '2023-01-01 00:00:00', 456],
)
->andReturn(1);

$result = $source->related()->updateOrCreate(['attr' => 'foo'], ['val' => 'baz']);
$this->assertEquals([
'id' => 456,
'attr' => 'foo',
'val' => 'baz',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
], $result->toArray());
}

Expand Down

0 comments on commit fab4ee3

Please sign in to comment.