packages/sql-semantics-sqlite/tests/Unit/Statement/Mutation/InsertIntoTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Mutation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
12use SqlSemantics\Platform\Sqlite\Semantics;
13use SqlSemantics\Platform\Sqlite\Statement\Mutation\ConflictResolution;
14use SqlSemantics\Platform\Sqlite\Statement\Mutation\InsertInto;
15use SqlSemantics\Platform\Sqlite\Statement\Mutation\InsertRows;
16use SqlSemantics\Platform\Sqlite\Statement\Mutation\InsertSelect;
17use SqlSemantics\Platform\Sqlite\Statement\Mutation\MutationTarget;
18use SqlSemantics\Platform\Sqlite\Statement\Relation\IndexChoice;
19
20#[CoversClass(InsertInto::class)]
21#[Medium]
22final class InsertIntoTest extends TestCase
23{
24    public function testRenderWritesTheVerbTheResolutionTheTargetAndTheColumns(): void
25    {
26        $semantics = new Semantics();
27        $replace = $semantics->analyze('replace into t (a, b) values (1, 2)');
28        $insert = $semantics->analyze('insert or abort into main.t as x (a) values (1)');
29        $with = $semantics->analyze('with w as (select 1 as q) insert into t select q from w');
30
31        self::assertSame('REPLACE INTO t (a, b) VALUES (1, 2)', $replace->toString());
32        self::assertInstanceOf(InsertRows::class, $replace->statement);
33        self::assertTrue($replace->statement->into->replace);
34        self::assertNull($replace->statement->into->resolution);
35        self::assertSame(['a', 'b'], array_map(static fn (Name $name): string => $name->value, $replace->statement->into->columns));
36        self::assertSame('INSERT OR ABORT INTO main.t AS x (a) VALUES (1)', $insert->toString());
37        self::assertInstanceOf(InsertRows::class, $insert->statement);
38        self::assertFalse($insert->statement->into->replace);
39        self::assertSame(ConflictResolution::Abort, $insert->statement->into->resolution);
40        self::assertSame('x', $insert->statement->into->target->alias?->value);
41        self::assertSame('WITH w AS (SELECT 1 AS q) INSERT INTO t SELECT q FROM w', $with->toString());
42        self::assertInstanceOf(InsertSelect::class, $with->statement);
43        self::assertNotNull($with->statement->into->with);
44        self::assertSame([], $with->statement->into->columns);
45    }
46
47    public function testRejectsReplaceWithAConflictResolution(): void
48    {
49        $this->expectExceptionMessage('REPLACE takes no conflict resolution.');
50
51        new InsertInto(new MutationTarget(new QualifiedName(new Name('t'))), [], true, ConflictResolution::Ignore);
52    }
53
54    public function testRejectsATargetWithAnIndexChoice(): void
55    {
56        $this->expectExceptionMessage('The table of an INSERT takes no index choice.');
57
58        new InsertInto(new MutationTarget(new QualifiedName(new Name('t')), null, new IndexChoice()));
59    }
60}
61