packages/sql-semantics-postgres/tests/Unit/Rules/Routine/RoutineChecksTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Rules\Routine\RoutineChecks;
11use SqlSemantics\Platform\PostgreSql\Semantics;
12use SqlSemantics\Platform\PostgreSql\Statement\Routine\CreateFunction;
13use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblem;
14use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblemKind;
15
16#[CoversClass(RoutineChecks::class)]
17#[Medium]
18final class RoutineChecksTest extends TestCase
19{
20    public function testOptionsReportsInvalidParallelModes(): void
21    {
22        $operation = (new Semantics())->inspect('ALTER FUNCTION f() PARALLEL fast', []);
23        self::assertEquals([new RoutineProblem(RoutineProblemKind::ParallelLevel)], $operation->facts->diagnostics);
24    }
25
26    public function testBodyReportsBodyProblems(): void
27    {
28        $semantics = new Semantics();
29        self::assertEquals(
30            [[new RoutineProblem(RoutineProblemKind::MissingBody)], [new RoutineProblem(RoutineProblemKind::DuplicateBody)], [new RoutineProblem(RoutineProblemKind::InlineBodyLanguage)]],
31            [
32                $semantics->inspect('CREATE PROCEDURE p() LANGUAGE sql')->facts->diagnostics,
33                $semantics->inspect("CREATE PROCEDURE p() AS 'x' BEGIN ATOMIC END")->facts->diagnostics,
34                $semantics->inspect('CREATE PROCEDURE p() LANGUAGE plpgsql BEGIN ATOMIC END')->facts->diagnostics,
35            ],
36        );
37    }
38
39    public function testBodyReportsALinkSymbolOutsideLanguageC(): void
40    {
41        $semantics = new Semantics();
42        self::assertEquals(
43            [[new RoutineProblem(RoutineProblemKind::SingleDefinition, 'sql')], []],
44            [$semantics->inspect("CREATE FUNCTION f() RETURNS int4 LANGUAGE sql AS 'a', 'b'")->facts->diagnostics, $semantics->inspect("CREATE FUNCTION f() RETURNS int4 LANGUAGE C AS 'a', 'b'")->facts->diagnostics],
45        );
46    }
47
48    public function testParametersReportsParameterProblems(): void
49    {
50        $semantics = new Semantics();
51        self::assertEquals(
52            [
53                [new RoutineProblem(RoutineProblemKind::OutputInTableFunction)],
54                [new RoutineProblem(RoutineProblemKind::DefaultOnOutput)],
55                [new RoutineProblem(RoutineProblemKind::ProcedureOutputAfterDefault)],
56                [],
57            ],
58            [
59                $semantics->inspect("CREATE FUNCTION f(OUT a int4) RETURNS TABLE (b int4) LANGUAGE sql AS 'x'")->facts->diagnostics,
60                $semantics->inspect("CREATE FUNCTION f(OUT a int4 DEFAULT 1) LANGUAGE sql AS 'x'")->facts->diagnostics,
61                $semantics->inspect("CREATE PROCEDURE p(a int4 DEFAULT 1, OUT b int4) LANGUAGE sql AS 'x'")->facts->diagnostics,
62                $semantics->inspect("CREATE FUNCTION f(VARIADIC a int4[], OUT b int4) LANGUAGE sql AS 'x'")->facts->diagnostics,
63            ],
64        );
65    }
66
67    public function testParameterReportsAnInputAfterVariadic(): void
68    {
69        $operation = (new Semantics())->inspect("CREATE FUNCTION f(VARIADIC a int4[], b int4) LANGUAGE sql AS 'x'");
70        $statement = $operation->statement;
71        self::assertInstanceOf(CreateFunction::class, $statement);
72        self::assertSame([null, RoutineProblemKind::VariadicNotLast], [(new RoutineChecks())->parameter($statement, $statement->parameters->parameters[1], false, false), (new RoutineChecks())->parameter($statement, $statement->parameters->parameters[1], true, false)]);
73    }
74
75    public function testNamesReportsSharedInputOrOutputNames(): void
76    {
77        $semantics = new Semantics();
78        self::assertEquals(
79            [[new RoutineProblem(RoutineProblemKind::DuplicateParameter, 'a')], [], [new RoutineProblem(RoutineProblemKind::OutputInTableFunction), new RoutineProblem(RoutineProblemKind::DuplicateParameter, 'a')]],
80            [
81                $semantics->inspect("CREATE FUNCTION f(a int4, INOUT a int4) LANGUAGE sql AS 'x'")->facts->diagnostics,
82                $semantics->inspect("CREATE FUNCTION f(a int4, OUT a int4) LANGUAGE sql AS 'x'")->facts->diagnostics,
83                $semantics->inspect("CREATE FUNCTION f(OUT a int4) RETURNS TABLE (a int4) LANGUAGE sql AS 'x'")->facts->diagnostics,
84            ],
85        );
86    }
87}
88