|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tpetry\PostgresqlEnhanced\Tests\Eloquent; |
| 6 | + |
| 7 | +use Carbon\Carbon; |
| 8 | +use Composer\Semver\Comparator; |
| 9 | +use Illuminate\Database\Eloquent\Model; |
| 10 | +use Illuminate\Database\Query\Builder; |
| 11 | +use Tpetry\PostgresqlEnhanced\Tests\TestCase; |
| 12 | + |
| 13 | +class BuilderUpsertPartialTest extends TestCase |
| 14 | +{ |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + |
| 19 | + Carbon::setTestNow(); // only compatible way of freezing time in Laravel 6 |
| 20 | + $this->getConnection()->unprepared(' |
| 21 | + CREATE TABLE example ( |
| 22 | + id bigint NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY, |
| 23 | + str text NOT NULL, |
| 24 | + val int NOT NULL, |
| 25 | + created_at timestamptz, |
| 26 | + updated_at timestamptz, |
| 27 | + deleted_at timestamptz |
| 28 | + ); |
| 29 | + CREATE UNIQUE INDEX example_partial ON example (str) WHERE deleted_at IS NULL; |
| 30 | + '); |
| 31 | + } |
| 32 | + |
| 33 | + public function testUpsertPartialWhereQuery(): void |
| 34 | + { |
| 35 | + if (Comparator::lessThan($this->app->version(), '8.10.0')) { |
| 36 | + $this->markTestSkipped('Upsert() has been added in a later Laravel version.'); |
| 37 | + } |
| 38 | + |
| 39 | + $queries = $this->withQueryLog(function (): void { |
| 40 | + $result = (new ExamplePartial()) |
| 41 | + ->newQuery() |
| 42 | + ->upsertPartial([['str' => 'JKLkmraa', 'val' => 849351]], ['str'], ['val'], fn (Builder $query) => $query->whereNull('deleted_at')); |
| 43 | + |
| 44 | + $this->assertEquals(1, $result); |
| 45 | + }); |
| 46 | + $this->assertEquals(['insert into "example" ("str", "val") values (?, ?) on conflict ("str") where "deleted_at" is null do update set "val" = "excluded"."val"'], array_column($queries, 'query')); |
| 47 | + } |
| 48 | + |
| 49 | + public function testUpsertPartialWhereString(): void |
| 50 | + { |
| 51 | + if (Comparator::lessThan($this->app->version(), '8.10.0')) { |
| 52 | + $this->markTestSkipped('Upsert() has been added in a later Laravel version.'); |
| 53 | + } |
| 54 | + |
| 55 | + $queries = $this->withQueryLog(function (): void { |
| 56 | + $result = (new ExamplePartial()) |
| 57 | + ->newQuery() |
| 58 | + ->upsertPartial([['str' => 'kIhqPWDC', 'val' => 623169]], ['str'], ['val'], 'deleted_at IS NULL'); |
| 59 | + |
| 60 | + $this->assertEquals(1, $result); |
| 61 | + }); |
| 62 | + $this->assertEquals(['insert into "example" ("str", "val") values (?, ?) on conflict ("str") where deleted_at IS NULL do update set "val" = "excluded"."val"'], array_column($queries, 'query')); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class ExamplePartial extends Model |
| 67 | +{ |
| 68 | + public $dateFormat = 'Y-m-d H:i:sO'; |
| 69 | + public $guarded = []; |
| 70 | + public $table = 'example'; |
| 71 | + public $timestamps = false; |
| 72 | +} |
0 commit comments