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 f7c4ffe commit 397211a
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,106 @@ 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');

$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')
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], false)
->andReturn([
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01 00:00:00',
'updated_at' => '2023-01-01 00:00:00',
]);

$model->newQuery()->createOrFirst(['attr' => 'foo'], ['val' => 'bar']);
});
}

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')
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['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 397211a

Please sign in to comment.