packages/sql-semantics-postgres/tests/Unit/Lowering/Invocation/SyntaxRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Invocation\SyntaxRule;
12use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
17use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Cast;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\CastSpelling;
22
23#[CoversClass(SyntaxRule::class)]
24#[Small]
25final class SyntaxRuleTest extends TestCase
26{
27    public function testSubexpressionLowersTheBareKeywordFunctions(): void
28    {
29        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
30        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT CURRENT_ROLE');
31        $node = (new SyntaxRule($lowering))->subexpression($tree->find('func_expr_common_subexpr')[0]);
32        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
33        $out->node($node);
34        self::assertSame('CURRENT_ROLE', (new Lexical())->join($out->pieces()));
35    }
36
37    public function testSpecialBuildsTheFunctionSpellingOfCast(): void
38    {
39        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
40        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT CAST(1 AS int)');
41        $node = (new SyntaxRule($lowering))->subexpression($tree->find('func_expr_common_subexpr')[0]);
42        self::assertInstanceOf(Cast::class, $node);
43        self::assertSame(CastSpelling::Function, $node->spelling);
44    }
45
46    public function testSubexpressionRoutesToTheFamilyRules(): void
47    {
48        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
49        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT XMLCONCAT(NULL)');
50        $node = (new SyntaxRule($lowering))->subexpression($tree->find('func_expr_common_subexpr')[0]);
51        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52        $out->node($node);
53        self::assertSame('XMLCONCAT(NULL)', (new Lexical())->join($out->pieces()));
54    }
55
56    public function testSpecialLowersTheRemainingFunctions(): void
57    {
58        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
59        $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT COLLATION FOR (1), TREAT(1 AS int), NORMALIZE(\'a\', NFC), NULLIF(1, 2), COALESCE(1), GREATEST(1), LEAST(1), MERGE_ACTION()');
60        $node = array_map(static fn ($target) => (new SyntaxRule($lowering))->subexpression($target->find('func_expr_common_subexpr')[0]), $tree->find('target_list')[0]->find('target_el'));
61        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
62        $out->list($node);
63        self::assertSame('COLLATION FOR (1), TREAT(1 AS INT), NORMALIZE(\'a\', NFC), NULLIF(1, 2), COALESCE(1), GREATEST(1), LEAST(1), MERGE_ACTION()', (new Lexical())->join($out->pieces()));
64    }
65}
66