packages/sql-semantics-postgres/tests/Unit/Lowering/Routine/OptionRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
12use SqlSemantics\Platform\PostgreSql\Lowering\Routine\OptionRule;
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\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Statement\Name\ObjectKind;
19use SqlSemantics\Platform\PostgreSql\Statement\Routine\Option\RoutineAttribute;
20
21#[CoversClass(OptionRule::class)]
22#[Small]
23final class OptionRuleTest extends TestCase
24{
25    public function testOptionsLowersAnEmptyList(): void
26    {
27        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
28        $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS int RETURN 1');
29        self::assertSame([], (new OptionRule($lowering))->options($tree->find('opt_createfunc_opt_list')[0]));
30    }
31
32    public function testOptionLowersWindowAndLanguage(): void
33    {
34        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
35        $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS int WINDOW LANGUAGE c AS $$x$$');
36        $rule = new OptionRule($lowering);
37        self::assertSame(RoutineAttribute::Window, $rule->option($tree->find('createfunc_opt_item')[0]));
38        self::assertSame('language', $rule->option($tree->find('createfunc_opt_item')[1])->setting());
39    }
40
41    public function testCommonLowersExternalSecurityAsSecurity(): void
42    {
43        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
44        $tree = (new PostgreSqlParser('pg-17.2'))->parse('ALTER FUNCTION f() EXTERNAL SECURITY DEFINER COST 5');
45        $rule = new OptionRule($lowering);
46        self::assertSame(RoutineAttribute::SecurityDefiner, $rule->common($tree->find('common_func_opt_item')[0]));
47        self::assertSame('cost', $rule->common($tree->find('common_func_opt_item')[1])->setting());
48    }
49
50    public function testDefinitionLowersALinkSymbol(): void
51    {
52        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
53        $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS int AS $$lib$$, $$sym$$ LANGUAGE c');
54        $definition = (new OptionRule($lowering))->definition($tree->find('func_as')[0], new Name('c'));
55        self::assertSame(['sym', 'c'], [$definition->symbol?->value, $definition->source->language?->value]);
56    }
57
58    public function testTransformsLowersEachType(): void
59    {
60        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
61        $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS int TRANSFORM FOR TYPE int, FOR TYPE text AS $$x$$');
62        self::assertCount(2, (new OptionRule($lowering))->transforms($tree->find('transform_type_list')[0]));
63    }
64
65    public function testAlterLowersTheKind(): void
66    {
67        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
68        $tree = (new PostgreSqlParser('pg-17.2'))->parse('ALTER PROCEDURE p() SECURITY INVOKER RESTRICT');
69        self::assertSame(ObjectKind::Procedure, (new OptionRule($lowering))->alter($tree->find('AlterFunctionStmt')[0])->kind);
70    }
71}
72