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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Program;
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\Statement\Fact\Diagnostic;
12use SqlSemantics\Platform\MySql\Model\Statement\Shape\OutputSlot;
13use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
17use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
18use SqlSemantics\Platform\MySql\Statement\Routine\Program\VariableDeclaration;
19use SqlSemantics\Platform\MySql\Statement\Type\Integral;
20use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
21
22#[CoversClass(VariableDeclaration::class)]
23#[Medium]
24final class VariableDeclarationTest extends TestCase
25{
26    public function testDeriveRelationAnswersANullablePositionPerName(): void
27    {
28        $operation = (new Semantics())->inspect('CREATE PROCEDURE p() BEGIN DECLARE a, b VARCHAR(5) DEFAULT \'x\'; END');
29        $create = $operation->statement;
30        self::assertInstanceOf(CreateProcedure::class, $create);
31        $block = $create->body;
32        self::assertInstanceOf(Block::class, $block);
33        $declaration = $block->declarations[0];
34        self::assertInstanceOf(VariableDeclaration::class, $declaration);
35        $slots = $operation->facts->relation($declaration)->shape->slots;
36
37        self::assertSame([], $operation->facts->diagnostics);
38        self::assertSame(['a', 'b'], array_map(static fn (OutputSlot $slot): ?string => $slot->name?->value, $slots));
39        self::assertSame([Nullability::Nullable, Nullability::Nullable], array_map(static fn (OutputSlot $slot): Nullability => $slot->nullability, $slots));
40        self::assertEquals(new Known($declaration->type), $slots[0]->type);
41    }
42
43    public function testDeriveRelationDerivesTheDefaultBeforeTheDeclaration(): void
44    {
45        $operation = (new Semantics())->inspect('CREATE PROCEDURE p(a INT) BEGIN DECLARE b INT DEFAULT a + c; END');
46
47        self::assertSame(['Column c does not exist.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
48    }
49
50    #[DataProvider('providerRenderWritesTheDeclaration')]
51    public function testRenderWritesTheDeclaration(string $release, string $sql, string $expected): void
52    {
53        self::assertSame($expected, (new Semantics($release))->inspect($sql)->toString());
54    }
55
56    /**
57     * @return iterable<string, array{string, string, string}>
58     */
59    public static function providerRenderWritesTheDeclaration(): iterable
60    {
61        yield 'several names with a default' => ['mysql-9.1.0', 'create procedure p() begin declare a, b int default 0; end', 'CREATE PROCEDURE p() BEGIN DECLARE a, b INT DEFAULT 0; END'];
62        yield 'a character set and a collation' => ['mysql-8.4.7', 'create procedure p() begin declare x varchar(10) character set utf8mb4 collate utf8mb4_bin default "a"; end', 'CREATE PROCEDURE p() BEGIN DECLARE x VARCHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT \'a\'; END'];
63        yield 'a collation in 5.6' => ['mysql-5.6.51', 'create procedure p() begin declare x text collate utf8_bin; end', 'CREATE PROCEDURE p() BEGIN DECLARE x TEXT COLLATE utf8_bin; END'];
64        yield 'type synonyms in 8.0' => ['mysql-8.0.44', 'create procedure p() begin declare y double precision; declare z national char(2); end', 'CREATE PROCEDURE p() BEGIN DECLARE y DOUBLE; DECLARE z NCHAR(2); END'];
65        yield 'JSON in 5.7' => ['mysql-5.7.44', 'create procedure p() begin declare x json default null; end', 'CREATE PROCEDURE p() BEGIN DECLARE x JSON DEFAULT NULL; END'];
66        yield 'a display width in 5.6' => ['mysql-5.6.51', 'create procedure p() begin declare x int(11) unsigned zerofill default 1; end', 'CREATE PROCEDURE p() BEGIN DECLARE x INT(11) UNSIGNED ZEROFILL DEFAULT 1; END'];
67    }
68
69    public function testADeclarationWithoutNamesIsRejected(): void
70    {
71        $this->expectExceptionMessage('DECLARE names at least one variable.');
72
73        new VariableDeclaration([], new Integral(IntegralKind::Int));
74    }
75}
76