Skip to content

Commit

Permalink
test: 💍 Add HasMany::createOrFirst() snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Oct 5, 2023
1 parent 12da4d1 commit beab92f
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Support\Carbon;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;
Expand Down Expand Up @@ -344,6 +345,74 @@ public function testCreateManyCreatesARelatedModelForEachRecord()
$this->assertEquals($colin, $instances[1]);
}

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

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

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

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

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

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

$sql = 'insert into "child_table" ("attr", "val", "foreign_key", "updated_at", "created_at") values (?, ?, ?, ?, ?)';
$bindings = ['foo', 'bar', '123', '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 "child_table" where "child_table"."foreign_key" = ? and "child_table"."foreign_key" is not null and ("attr" = ?) limit 1', ['123', 'foo'], false)
->andReturn([[
'foreign_key' => '123',
'attr' => 'foo',
'val' => 'bar',
'created_at' => '2023-01-01 00:00:00',
'updated_at' => '2023-01-01 00:00:00',
]]);

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

protected function getRelation()
{
$builder = m::mock(Builder::class);
Expand Down

0 comments on commit beab92f

Please sign in to comment.