packages/ztd-query-postgres/tests/Unit/Sql/Dml/Insert/InsertClauseParserTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Sql\Dml\Insert;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Sql\Dml\Insert\InsertClauseParser::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\PostgreSqlLexicalMasker::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Lexing\CommentSpan::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Lexing\IdentifierDecoder::class)]
15final class InsertClauseParserTest extends TestCase
16{
17    public function testFindInsertSourceClause(): void
18    {
19        self::assertSame(['keyword' => 'VALUES', 'offset' => 29], (new \ZtdQuery\Platform\Postgres\Sql\Dml\Insert\InsertClauseParser())->findInsertSourceClause("INSERT INTO users (id, name) VALUES (1, 'a,b'), (2, 'c')"));
20    }
21
22    public function testExtractInsertValues(): void
23    {
24        self::assertSame([['1', '\'a,b\''], ['2', '\'c\'']], (new \ZtdQuery\Platform\Postgres\Sql\Dml\Insert\InsertClauseParser())->extractInsertValues("INSERT INTO users (id, name) VALUES (1, 'a,b'), (2, 'c')"));
25    }
26
27    public function testExtractParenthesizedList(): void
28    {
29        self::assertSame(['items' => ['1', 'coalesce(2, 3)', '\'a,b\''], 'end' => 26], (new \ZtdQuery\Platform\Postgres\Sql\Dml\Insert\InsertClauseParser())->extractParenthesizedList("(1, coalesce(2, 3), 'a,b') trailing", 0));
30    }
31
32    public function testHasInsertSelect(): void
33    {
34        self::assertSame(true, (new \ZtdQuery\Platform\Postgres\Sql\Dml\Insert\InsertClauseParser())->hasInsertSelect('INSERT INTO users SELECT id FROM source'));
35    }
36
37    public function testExtractInsertSelectSql(): void
38    {
39        self::assertSame('SELECT id FROM source', (new \ZtdQuery\Platform\Postgres\Sql\Dml\Insert\InsertClauseParser())->extractInsertSelectSql('INSERT INTO users SELECT id FROM source'));
40    }
41    public function testAppendListCharacterTreatsArrayCommasAsNested(): void
42    {
43        $source = new \ZtdQuery\Platform\Postgres\Sql\Dml\Insert\InsertClauseParser();
44        $depth = 0;
45        $items = [];
46        $current = 'a';
47        $source->appendListCharacter('[', 1, $depth, $items, $current);
48        $source->appendListCharacter(',', 1, $depth, $items, $current);
49        $source->appendListCharacter(']', 1, $depth, $items, $current);
50        self::assertSame([], $items);
51        $source->appendListCharacter(',', 1, $depth, $items, $current);
52        self::assertSame(['a[,]'], $items);
53        self::assertSame('', $current);
54        self::assertSame(0, $depth);
55    }
56
57    public function testExtractInsertColumnsPreservesQuotedColumnNames(): void
58    {
59        $parser = new \ZtdQuery\Platform\Postgres\Sql\Dml\Insert\InsertClauseParser();
60        self::assertSame(['id', 'Name'], $parser->extractInsertColumns('INSERT INTO users (id, "Name") VALUES (1, NULL)'));
61        self::assertSame([], $parser->extractInsertColumns('INSERT INTO users DEFAULT VALUES'));
62    }
63}
64