Skip to content

Commit 1eb9b1a

Browse files
committed
test: 💍 Add Builder::updateOrCreate() snapshot tests
1 parent ebc9ba1 commit 1eb9b1a

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,6 +2304,130 @@ public function testFirstOrCreateMethodRetrievesRecordCreatedJustNow()
23042304
});
23052305
}
23062306

2307+
public function testUpdateOrCreateMethodUpdatesExistingRecord()
2308+
{
2309+
Carbon::setTestNow('2023-01-01 00:00:00');
2310+
2311+
Model::unguarded(function () {
2312+
$model = new EloquentBuilderTestStubStringPrimaryKey();
2313+
$this->mockConnectionForModel($model, 'SQLite');
2314+
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
2315+
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');
2316+
2317+
$model->getConnection()
2318+
->expects('select')
2319+
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], true)
2320+
->andReturn([[
2321+
'id' => '123',
2322+
'attr' => 'foo',
2323+
'val' => 'bar',
2324+
'created_at' => '2023-01-01 00:00:00',
2325+
'updated_at' => '2023-01-01 00:00:00',
2326+
]]);
2327+
2328+
$model->getConnection()
2329+
->expects('update')
2330+
->with(
2331+
'update "foo_table" set "val" = ?, "updated_at" = ? where "id" = ?',
2332+
['baz', '2023-01-01 00:00:00', '123'],
2333+
)
2334+
->andReturn(1);
2335+
2336+
$result = $model->newQuery()->updateOrCreate(['attr' => 'foo'], ['val' => 'baz']);
2337+
$this->assertEquals([
2338+
'id' => '123',
2339+
'attr' => 'foo',
2340+
'val' => 'baz',
2341+
'created_at' => '2023-01-01T00:00:00.000000Z',
2342+
'updated_at' => '2023-01-01T00:00:00.000000Z',
2343+
], $result->toArray());
2344+
});
2345+
}
2346+
2347+
public function testUpdateOrCreateMethodCreatesNewRecord()
2348+
{
2349+
Carbon::setTestNow('2023-01-01 00:00:00');
2350+
2351+
Model::unguarded(function () {
2352+
$model = new EloquentBuilderTestStubStringPrimaryKey();
2353+
$this->mockConnectionForModel($model, 'SQLite');
2354+
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
2355+
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');
2356+
2357+
$model->getConnection()
2358+
->expects('select')
2359+
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], true)
2360+
->andReturn([]);
2361+
2362+
$model->getConnection()->expects('insert')->with(
2363+
'insert into "foo_table" ("attr", "val", "updated_at", "created_at") values (?, ?, ?, ?)',
2364+
['foo', 'bar', '2023-01-01 00:00:00', '2023-01-01 00:00:00'],
2365+
)->andReturnTrue();
2366+
2367+
$result = $model->newQuery()->updateOrCreate(['attr' => 'foo'], ['val' => 'bar']);
2368+
$this->assertEquals([
2369+
'attr' => 'foo',
2370+
'val' => 'bar',
2371+
'created_at' => '2023-01-01T00:00:00.000000Z',
2372+
'updated_at' => '2023-01-01T00:00:00.000000Z',
2373+
], $result->toArray());
2374+
});
2375+
}
2376+
2377+
public function testUpdateOrCreateMethodUpdatesRecordCreatedJustNow()
2378+
{
2379+
Carbon::setTestNow('2023-01-01 00:00:00');
2380+
2381+
Model::unguarded(function () {
2382+
$model = new EloquentBuilderTestStubStringPrimaryKey();
2383+
$this->mockConnectionForModel($model, 'SQLite');
2384+
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
2385+
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');
2386+
2387+
$model->getConnection()
2388+
->expects('select')
2389+
->with('select * from "foo_table" where ("attr" = ?) limit 1', ['foo'], true)
2390+
->andReturn([]);
2391+
2392+
$sql = 'insert into "foo_table" ("attr", "val", "updated_at", "created_at") values (?, ?, ?, ?)';
2393+
$bindings = ['foo', 'baz', '2023-01-01 00:00:00', '2023-01-01 00:00:00'];
2394+
2395+
$model->getConnection()
2396+
->expects('insert')
2397+
->with($sql, $bindings)
2398+
->andThrow(new UniqueConstraintViolationException('sqlite', $sql, $bindings, new Exception()));
2399+
2400+
$model->getConnection()
2401+
->expects('select')
2402+
// FIXME: duplicate conditions
2403+
->with('select * from "foo_table" where ("attr" = ?) and ("attr" = ?) limit 1', ['foo', 'foo'], false)
2404+
->andReturn([[
2405+
'id' => '123',
2406+
'attr' => 'foo',
2407+
'val' => 'bar',
2408+
'created_at' => '2023-01-01 00:00:00',
2409+
'updated_at' => '2023-01-01 00:00:00',
2410+
]]);
2411+
2412+
$model->getConnection()
2413+
->expects('update')
2414+
->with(
2415+
'update "foo_table" set "val" = ?, "updated_at" = ? where "id" = ?',
2416+
['baz', '2023-01-01 00:00:00', '123'],
2417+
)
2418+
->andReturn(1);
2419+
2420+
$result = $model->newQuery()->updateOrCreate(['attr' => 'foo'], ['val' => 'baz']);
2421+
$this->assertEquals([
2422+
'id' => '123',
2423+
'attr' => 'foo',
2424+
'val' => 'baz',
2425+
'created_at' => '2023-01-01T00:00:00.000000Z',
2426+
'updated_at' => '2023-01-01T00:00:00.000000Z',
2427+
], $result->toArray());
2428+
});
2429+
}
2430+
23072431
public function testUpsert()
23082432
{
23092433
Carbon::setTestNow($now = '2017-10-10 10:10:10');

0 commit comments

Comments
 (0)