packages/sql-semantics-postgres/tests/Unit/Lowering/Routine/FunctionRuleTest.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\FunctionRule;
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\Platform;
17use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
18use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
19use SqlSemantics\Platform\PostgreSql\Statement\Routine\DefaultSpelling;
20use SqlSemantics\Platform\PostgreSql\Statement\Routine\ResultTable;
21
22#[CoversClass(FunctionRule::class)]
23#[Small]
24final class FunctionRuleTest extends TestCase
25{
26 public function testCreateLowersEachForm(): void
27 {
28 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
29 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS TABLE (a int) AS $$x$$ LANGUAGE sql; CREATE OR REPLACE PROCEDURE p() BEGIN ATOMIC END');
30 $rule = new FunctionRule($lowering);
31 $function = $rule->create($tree->find('CreateFunctionStmt')[0]);
32 $procedure = $rule->create($tree->find('CreateFunctionStmt')[1]);
33 self::assertInstanceOf(ResultTable::class, $function->returns);
34 self::assertSame([false, true, true, 2], [$function->procedure, $procedure->procedure, $procedure->replace, count($function->options)]);
35 }
36
37 public function testParametersLowersAnEmptyList(): 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('CREATE FUNCTION f() RETURNS int RETURN 1');
41 self::assertSame([], (new FunctionRule($lowering))->parameters($tree->find('func_args_with_defaults')[0])->parameters);
42 }
43
44 public function testParameterLowersBothDefaultSpellings(): void
45 {
46 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
47 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f(a int DEFAULT 1, b int = 2) RETURNS int RETURN 1');
48 $rule = new FunctionRule($lowering);
49 self::assertSame([DefaultSpelling::Keyword, DefaultSpelling::EqualsSign], [$rule->parameter($tree->find('func_arg_with_default')[0])->defaultSpelling, $rule->parameter($tree->find('func_arg_with_default')[1])->defaultSpelling]);
50 }
51
52 public function testResultLowersSetof(): void
53 {
54 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
55 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS SETOF int RETURN 1');
56 self::assertTrue((new FunctionRule($lowering))->result($tree->find('func_return')[0])->setOf);
57 }
58
59 public function testTableLowersTheColumns(): void
60 {
61 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
62 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS TABLE (a int, b text) RETURN 1');
63 self::assertCount(2, (new FunctionRule($lowering))->table($tree->find('table_func_column_list')[0])->columns);
64 }
65
66 public function testBodyLowersNoBody(): void
67 {
68 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
69 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS int AS $$x$$');
70 self::assertNull((new FunctionRule($lowering))->body($tree->find('opt_routine_body')[0]));
71 }
72
73 public function testReturnLowersTheExpression(): void
74 {
75 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
76 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS int RETURN 1');
77 $value = (new FunctionRule($lowering))->return($tree->find('ReturnStmt')[0])->value;
78 self::assertInstanceOf(Constant::class, $value);
79 self::assertEquals(new IntegerConstant('1'), $value->value);
80 }
81
82 public function testStatementsDropsEmptyStatements(): void
83 {
84 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
85 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE FUNCTION f() RETURNS int BEGIN ATOMIC ; SELECT 1; ; RETURN 1; END');
86 self::assertCount(2, (new FunctionRule($lowering))->statements($tree->find('routine_body_stmt_list')[0]));
87 }
88}
89