Skip to content

Commit

Permalink
test: 💍 Add Builder::firstOrCreate() snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Oct 5, 2023
1 parent 495db28 commit ebc9ba1
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,110 @@ public function testCreateOrFirstMethodRetrievesExistingRecord()
});
}

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

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

$model->getConnection()
->expects('select')
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], true)
->andReturn([[
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01 00:00:00',
'updated_at' => '2023-01-01 00:00:00',
]]);

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

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

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

$model->getConnection()
->expects('select')
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], true)
->andReturn([]);

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

$result = $model->newQuery()->firstOrCreate(['attr' => 'foo'], ['val' => 'bar']);
$this->assertEquals([
'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 EloquentBuilderTestStubStringPrimaryKey();
$this->mockConnectionForModel($model, 'SQLite');
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');

$model->getConnection()
->expects('select')
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], true)
->andReturn([]);

$sql = 'insert into "foo_table" ("attr", "val", "updated_at", "created_at") values (?, ?, ?, ?)';
$bindings = ['foo', 'bar', '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')
// FIXME: duplicate conditions
->with('select * from "foo_table" where ("attr" = ?) and ("attr" = ?) limit 1', ['foo', 'foo'], false)
->andReturn([[
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01 00:00:00',
'updated_at' => '2023-01-01 00:00:00',
]]);

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

public function testUpsert()
{
Carbon::setTestNow($now = '2017-10-10 10:10:10');
Expand Down

0 comments on commit ebc9ba1

Please sign in to comment.