|
11 | 11 | use Illuminate\Database\Eloquent\Relations\HasMany;
|
12 | 12 | use Illuminate\Database\Query\Builder as BaseBuilder;
|
13 | 13 | use Illuminate\Database\UniqueConstraintViolationException;
|
| 14 | +use Illuminate\Support\Carbon; |
14 | 15 | use Mockery as m;
|
15 | 16 | use PHPUnit\Framework\TestCase;
|
16 | 17 | use stdClass;
|
@@ -344,6 +345,74 @@ public function testCreateManyCreatesARelatedModelForEachRecord()
|
344 | 345 | $this->assertEquals($colin, $instances[1]);
|
345 | 346 | }
|
346 | 347 |
|
| 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 | + |
347 | 416 | protected function getRelation()
|
348 | 417 | {
|
349 | 418 | $builder = m::mock(Builder::class);
|
|
0 commit comments