Skip to content

Commit

Permalink
test: 💍 Add HasMany::firstOrCreate() snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Oct 5, 2023
1 parent 9dad517 commit e5735c4
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,117 @@ public function testCreateOrFirstMethodRetrievesExistingRecord()
});
}

public function testFirstOrCreateMethodCreatesNewRecord()
{
Carbon::setTestNow('2023-01-01 00:00:00');

Model::unguarded(function () {
$model = new EloquentHasManyModelStubWithRelation();
$model->id = '123';
$this->mockConnectionForModel($model, 'SQLite');
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');

$model->getConnection()
->expects('select')
->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', ['123', 'foo'], true)
->andReturn([]);

$model->getConnection()->expects('insert')->with(
'insert into "child_table" ("attr", "val", "parent_id", "updated_at", "created_at") values (?, ?, ?, ?, ?)',
['foo', 'bar', '123', '2023-01-01 00:00:00', '2023-01-01 00:00:00'],
)->andReturnTrue();

$result = $model->child()->firstOrCreate(['attr' => 'foo'], ['val' => 'bar']);
$this->assertEquals([
'parent_id' => '123',
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
], $result->toArray());
});
}

public function testFirstOrCreateMethodRetrievesExistingRecord()
{
Carbon::setTestNow('2023-01-01 00:00:00');

Model::unguarded(function () {
$model = new EloquentHasManyModelStubWithRelation();
$model->id = '123';
$this->mockConnectionForModel($model, 'SQLite');
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');

$model->getConnection()
->expects('select')
->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', ['123', 'foo'], true)
->andReturn([[
'parent_id' => '123',
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
]]);

$result = $model->child()->firstOrCreate(['attr' => 'foo'], ['val' => 'bar']);
$this->assertEquals([
'parent_id' => '123',
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
], $result->toArray());
});
}

public function testFirstOrCreateMethodRetrievesRecordCreatedJustNow()
{
Carbon::setTestNow('2023-01-01 00:00:00');

Model::unguarded(function () {
$model = new EloquentHasManyModelStubWithRelation();
$model->id = '123';
$this->mockConnectionForModel($model, 'SQLite');
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');

$model->getConnection()
->expects('select')
->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) limit 1', ['123', 'foo'], true)
->andReturn([]);

$sql = 'insert into "child_table" ("attr", "val", "parent_id", "updated_at", "created_at") values (?, ?, ?, ?, ?)';
$bindings = ['foo', 'bar', '123', '2023-01-01 00:00:00', '2023-01-01 00:00:00'];

$model->getConnection()
->expects('insert')
->with($sql, $bindings)
->andThrow(new UniqueConstraintViolationException('sqlite', $sql, $bindings, new Exception()));

$model->getConnection()
->expects('select')
->with('select * from "child_table" where "child_table"."parent_id" = ? and "child_table"."parent_id" is not null and ("attr" = ?) and ("attr" = ?) limit 1', ['123', 'foo', 'foo'], false)
->andReturn([[
'parent_id' => '123',
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01 00:00:00',
'updated_at' => '2023-01-01 00:00:00',
]]);

$result = $model->child()->firstOrCreate(['attr' => 'foo'], ['val' => 'bar']);
$this->assertEquals([
'parent_id' => '123',
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01T00:00:00.000000Z',
'updated_at' => '2023-01-01T00:00:00.000000Z',
], $result->toArray());
});
}

protected function getRelation()
{
$builder = m::mock(Builder::class);
Expand Down

0 comments on commit e5735c4

Please sign in to comment.