From 397211a8a40e6cc8fa0c2e5c47a62b4c40a15149 Mon Sep 17 00:00:00 2001 From: mpyw Date: Thu, 5 Oct 2023 17:30:42 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20Add=20`Builder::firstOrC?= =?UTF-8?q?reate()`=20snapshot=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Database/DatabaseEloquentBuilderTest.php | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 4233bc5b6704..2fa61c027a32 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -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');