Skip to content

Commit e4efdc3

Browse files
committed
test: 💍 Add Builder::firstOrCreate() snapshot tests
1 parent f7c4ffe commit e4efdc3

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,107 @@ public function testCreateOrFirstMethodRetrievesExistingRecord()
21982198
});
21992199
}
22002200

2201+
public function testFirstOrCreateMethodRetrievesExistingRecord()
2202+
{
2203+
Carbon::setTestNow('2023-01-01 00:00:00');
2204+
2205+
Model::unguarded(function () {
2206+
$model = new EloquentBuilderTestStubStringPrimaryKey();
2207+
$this->mockConnectionForModel($model, 'SQLite');
2208+
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
2209+
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');
2210+
2211+
$sql = 'insert into "foo_table" ("attr", "val", "updated_at", "created_at") values (?, ?, ?, ?)';
2212+
$bindings = ['foo', 'bar', '2023-01-01 00:00:00', '2023-01-01 00:00:00'];
2213+
$model->getConnection()
2214+
->expects('insert')
2215+
->with($sql, $bindings)
2216+
->andThrow(new UniqueConstraintViolationException('sqlite', $sql, $bindings, new Exception()));
2217+
$model->getConnection()
2218+
->expects('select')
2219+
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], false)
2220+
->andReturn([
2221+
'attr' => 'foo',
2222+
'val' => 'bar',
2223+
'created_at' => '2023-01-01 00:00:00',
2224+
'updated_at' => '2023-01-01 00:00:00',
2225+
]);
2226+
2227+
$model->newQuery()->createOrFirst(['attr' => 'foo'], ['val' => 'bar']);
2228+
});
2229+
}
2230+
2231+
public function testFirstOrCreateMethodCreatesNewRecord()
2232+
{
2233+
Carbon::setTestNow('2023-01-01 00:00:00');
2234+
2235+
Model::unguarded(function () {
2236+
$model = new EloquentBuilderTestStubStringPrimaryKey();
2237+
$this->mockConnectionForModel($model, 'SQLite');
2238+
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
2239+
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');
2240+
2241+
$model->getConnection()
2242+
->expects('select')
2243+
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], true)
2244+
->andReturn([]);
2245+
$model->getConnection()->expects('insert')->with(
2246+
'insert into "foo_table" ("attr", "val", "updated_at", "created_at") values (?, ?, ?, ?)',
2247+
['foo', 'bar', '2023-01-01 00:00:00', '2023-01-01 00:00:00'],
2248+
)->andReturnTrue();
2249+
2250+
$result = $model->newQuery()->firstOrCreate(['attr' => 'foo'], ['val' => 'bar']);
2251+
$this->assertEquals([
2252+
'attr' => 'foo',
2253+
'val' => 'bar',
2254+
'created_at' => '2023-01-01T00:00:00.000000Z',
2255+
'updated_at' => '2023-01-01T00:00:00.000000Z',
2256+
], $result->toArray());
2257+
});
2258+
}
2259+
2260+
public function testFirstOrCreateMethodRetrievesRecordCreatedJustNow()
2261+
{
2262+
Carbon::setTestNow('2023-01-01 00:00:00');
2263+
2264+
Model::unguarded(function () {
2265+
$model = new EloquentBuilderTestStubStringPrimaryKey();
2266+
$this->mockConnectionForModel($model, 'SQLite');
2267+
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
2268+
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');
2269+
2270+
$model->getConnection()
2271+
->expects('select')
2272+
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], true)
2273+
->andReturn([]);
2274+
2275+
$sql = 'insert into "foo_table" ("attr", "val", "updated_at", "created_at") values (?, ?, ?, ?)';
2276+
$bindings = ['foo', 'bar', '2023-01-01 00:00:00', '2023-01-01 00:00:00'];
2277+
$model->getConnection()
2278+
->expects('insert')
2279+
->with($sql, $bindings)
2280+
->andThrow(new UniqueConstraintViolationException('sqlite', $sql, $bindings, new Exception()));
2281+
$model->getConnection()
2282+
->expects('select')
2283+
// FIXME: duplicate conditions
2284+
->with('select * from "foo_table" where ("attr" = ?) and ("attr" = ?) limit 1', ['foo', 'foo'], false)
2285+
->andReturn([[
2286+
'attr' => 'foo',
2287+
'val' => 'bar',
2288+
'created_at' => '2023-01-01 00:00:00',
2289+
'updated_at' => '2023-01-01 00:00:00',
2290+
]]);
2291+
2292+
$result = $model->newQuery()->firstOrCreate(['attr' => 'foo'], ['val' => 'bar']);
2293+
$this->assertEquals([
2294+
'attr' => 'foo',
2295+
'val' => 'bar',
2296+
'created_at' => '2023-01-01T00:00:00.000000Z',
2297+
'updated_at' => '2023-01-01T00:00:00.000000Z',
2298+
], $result->toArray());
2299+
});
2300+
}
2301+
22012302
public function testUpsert()
22022303
{
22032304
Carbon::setTestNow($now = '2017-10-10 10:10:10');

0 commit comments

Comments
 (0)