packages/sql-semantics-mysql/tests/Unit/Statement/Routine/ParameterListTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\Medium;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\MySql\Model\Rendering\Output;
15use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
16use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\MySql\Model\Statement\Shape\OutputSlot;
18use SqlSemantics\Platform\MySql\Model\Statement\Shape\RowShape;
19use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
20use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
21use SqlSemantics\Platform\MySql\Platform;
22use SqlSemantics\Platform\MySql\Rendering\Codec;
23use SqlSemantics\Platform\MySql\Semantics;
24use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
25use SqlSemantics\Platform\MySql\Statement\Routine\Parameter;
26use SqlSemantics\Platform\MySql\Statement\Routine\ParameterList;
27use SqlSemantics\Platform\MySql\Statement\Routine\ParameterMode;
28use SqlSemantics\Platform\MySql\Statement\Type\Character;
29use SqlSemantics\Platform\MySql\Statement\Type\Integral;
30use SqlSemantics\Platform\MySql\Statement\Type\Kind\CharacterKind;
31use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
32
33#[CoversClass(ParameterList::class)]
34#[Medium]
35final class ParameterListTest extends TestCase
36{
37    public function testDeriveRelationAnswersOneNullablePositionPerParameter(): void
38    {
39        $platform = new Platform();
40        $derivation = new Derivation($platform->context($platform->profile('mysql-9.1.0', null, ParameterStyle::Native), null, [], false));
41        $fact = $derivation->relation(new ParameterList([
42            new Parameter(new Name('x'), new Integral(IntegralKind::Int), null, ParameterMode::Out),
43            new Parameter(new Name('y'), new Character(CharacterKind::VarChar, '3')),
44        ]), $derivation->environment());
45
46        self::assertEquals(new RowShape([
47            new OutputSlot(new Name('x'), new Known(new Integral(IntegralKind::Int)), Nullability::Nullable),
48            new OutputSlot(new Name('y'), new Known(new Character(CharacterKind::VarChar, '3')), Nullability::Nullable),
49        ]), $fact->shape);
50        self::assertNull($fact->table);
51    }
52
53    public function testDeriveRelationAnswersTheRowOfAnAnalyzedRoutine(): void
54    {
55        $create = (new Semantics())->inspect('CREATE PROCEDURE p(INOUT x INT, y BIGINT) SELECT x');
56        $statement = $create->statement;
57        self::assertInstanceOf(CreateProcedure::class, $statement);
58
59        self::assertEquals(new RowShape([
60            new OutputSlot(new Name('x'), new Known(new Integral(IntegralKind::Int)), Nullability::Nullable),
61            new OutputSlot(new Name('y'), new Known(new Integral(IntegralKind::BigInt)), Nullability::Nullable),
62        ]), $create->facts->relation($statement->parameters)->shape);
63    }
64
65    public function testDeriveRelationResolvesEveryDirectionInTheBody(): void
66    {
67        $create = (new Semantics())->inspect('CREATE PROCEDURE p(IN a INT, OUT b INT, INOUT c INT, d INT) SELECT a, b, c, d, z', []);
68
69        self::assertSame(['Column z does not exist.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $create->facts->diagnostics));
70    }
71
72    public function testDeriveRelationLetsALocalVariableHideAParameter(): void
73    {
74        self::assertSame([], (new Semantics())->inspect('CREATE PROCEDURE p(x INT) BEGIN DECLARE x BIGINT; SELECT x; END', [])->facts->diagnostics);
75    }
76
77    /**
78     * @return iterable<string, array{string, string, string}>
79     */
80    public static function providerRenderWritesTheParentheses(): iterable
81    {
82        yield 'MySQL 5.6 empty' => ['mysql-5.6.51', 'create procedure p() select 1', 'CREATE PROCEDURE p() SELECT 1'];
83        yield 'MySQL 5.7 two parameters' => ['mysql-5.7.44', 'create procedure p(a int,b int) select 1', 'CREATE PROCEDURE p(a INT, b INT) SELECT 1'];
84        yield 'MySQL 8.0 function' => ['mysql-8.0.44', 'create function f(a int, b char(2)) returns int return 1', 'CREATE FUNCTION f(a INT, b CHAR(2)) RETURNS INT RETURN 1'];
85        yield 'MySQL 9.1 duplicate kept' => ['mysql-9.1.0', 'create procedure p(in x int, x int) select 1', 'CREATE PROCEDURE p(IN x INT, x INT) SELECT 1'];
86    }
87
88    #[DataProvider('providerRenderWritesTheParentheses')]
89    public function testRenderWritesTheParentheses(string $release, string $sql, string $expected): void
90    {
91        self::assertSame($expected, (new Semantics($release))->inspect($sql)->toString());
92    }
93
94    public function testRenderWritesAConstructedList(): void
95    {
96        $semantics = new Semantics();
97        $empty = new Output(new Codec($semantics->profile()->grammar));
98        (new ParameterList([]))->render($empty);
99        $two = new Output(new Codec($semantics->profile()->grammar));
100        (new ParameterList([new Parameter(new Name('a'), new Integral(IntegralKind::Int)), new Parameter(new Name('b'), new Integral(IntegralKind::Int), null, ParameterMode::Out)]))->render($two);
101
102        self::assertSame('()', (new Lexical())->join($empty->pieces()));
103        self::assertSame('(a INT, OUT b INT)', (new Lexical())->join($two->pieces()));
104    }
105}
106