Skip to content

Commit beab92f

Browse files
committed
test: 💍 Add HasMany::createOrFirst() snapshot tests
1 parent 12da4d1 commit beab92f

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tests/Database/DatabaseEloquentHasManyTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Database\Eloquent\Relations\HasMany;
1212
use Illuminate\Database\Query\Builder as BaseBuilder;
1313
use Illuminate\Database\UniqueConstraintViolationException;
14+
use Illuminate\Support\Carbon;
1415
use Mockery as m;
1516
use PHPUnit\Framework\TestCase;
1617
use stdClass;
@@ -344,6 +345,74 @@ public function testCreateManyCreatesARelatedModelForEachRecord()
344345
$this->assertEquals($colin, $instances[1]);
345346
}
346347

348+
public function testCreateOrFirstMethodCreatesNewRecord()
349+
{
350+
Carbon::setTestNow('2023-01-01 00:00:00');
351+
352+
Model::unguarded(function () {
353+
$model = new EloquentHasManyModelStubWithRelation();
354+
$model->id = '123';
355+
$this->mockConnectionForModel($model, 'SQLite');
356+
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
357+
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');
358+
359+
$model->getConnection()->expects('insert')->with(
360+
'insert into "child_table" ("attr", "val", "foreign_key", "updated_at", "created_at") values (?, ?, ?, ?, ?)',
361+
['foo', 'bar', '123', '2023-01-01 00:00:00', '2023-01-01 00:00:00'],
362+
)->andReturnTrue();
363+
364+
$result = $model->child()->createOrFirst(['attr' => 'foo'], ['val' => 'bar']);
365+
$this->assertEquals([
366+
'foreign_key' => '123',
367+
'attr' => 'foo',
368+
'val' => 'bar',
369+
'created_at' => '2023-01-01T00:00:00.000000Z',
370+
'updated_at' => '2023-01-01T00:00:00.000000Z',
371+
], $result->toArray());
372+
});
373+
}
374+
375+
public function testCreateOrFirstMethodRetrievesExistingRecord()
376+
{
377+
Carbon::setTestNow('2023-01-01 00:00:00');
378+
379+
Model::unguarded(function () {
380+
$model = new EloquentHasManyModelStubWithRelation();
381+
$model->id = '123';
382+
$this->mockConnectionForModel($model, 'SQLite');
383+
$model->getConnection()->shouldReceive('transactionLevel')->andReturn(0);
384+
$model->getConnection()->shouldReceive('getName')->andReturn('sqlite');
385+
386+
$sql = 'insert into "child_table" ("attr", "val", "foreign_key", "updated_at", "created_at") values (?, ?, ?, ?, ?)';
387+
$bindings = ['foo', 'bar', '123', '2023-01-01 00:00:00', '2023-01-01 00:00:00'];
388+
389+
$model->getConnection()
390+
->expects('insert')
391+
->with($sql, $bindings)
392+
->andThrow(new UniqueConstraintViolationException('sqlite', $sql, $bindings, new Exception()));
393+
394+
$model->getConnection()
395+
->expects('select')
396+
->with('select * from "child_table" where "child_table"."foreign_key" = ? and "child_table"."foreign_key" is not null and ("attr" = ?) limit 1', ['123', 'foo'], false)
397+
->andReturn([[
398+
'foreign_key' => '123',
399+
'attr' => 'foo',
400+
'val' => 'bar',
401+
'created_at' => '2023-01-01 00:00:00',
402+
'updated_at' => '2023-01-01 00:00:00',
403+
]]);
404+
405+
$result = $model->child()->createOrFirst(['attr' => 'foo'], ['val' => 'bar']);
406+
$this->assertEquals([
407+
'foreign_key' => '123',
408+
'attr' => 'foo',
409+
'val' => 'bar',
410+
'created_at' => '2023-01-01T00:00:00.000000Z',
411+
'updated_at' => '2023-01-01T00:00:00.000000Z',
412+
], $result->toArray());
413+
});
414+
}
415+
347416
protected function getRelation()
348417
{
349418
$builder = m::mock(Builder::class);

0 commit comments

Comments
 (0)