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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Program\Flow;
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\Semantics;
13use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
14use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
15use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
16use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\SimpleCase;
17
18#[CoversClass(SimpleCase::class)]
19#[Medium]
20final class SimpleCaseTest extends TestCase
21{
22    public function testDeriveProgramDerivesTheOperandAndEveryBranch(): void
23    {
24        $operation = (new Semantics())->inspect('CREATE PROCEDURE p(a INT) CASE b WHEN c THEN SELECT d; WHEN 1 THEN SELECT a; ELSE SELECT e; END CASE');
25        $create = $operation->statement;
26        self::assertInstanceOf(CreateProcedure::class, $create);
27        $case = $create->body;
28        self::assertInstanceOf(SimpleCase::class, $case);
29
30        self::assertInstanceOf(ColumnUse::class, $case->operand);
31        self::assertCount(2, $case->branches);
32        self::assertCount(1, $case->otherwise);
33        self::assertSame(['Column b does not exist.', 'Column c does not exist.', 'Column d does not exist.', 'Column e does not exist.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
34    }
35
36    public function testDeriveProgramResolvesAVariableAsTheOperand(): void
37    {
38        $operation = (new Semantics())->inspect('CREATE PROCEDURE p() BEGIN DECLARE x INT DEFAULT 1; CASE x WHEN 1 THEN SET x = 2; ELSE BEGIN END; END CASE; END');
39
40        self::assertSame([], $operation->facts->diagnostics);
41    }
42
43    #[DataProvider('providerRenderWritesTheStatement')]
44    public function testRenderWritesTheStatement(string $release, string $sql, string $expected): void
45    {
46        self::assertSame($expected, (new Semantics($release))->inspect($sql)->toString());
47    }
48
49    /**
50     * @return iterable<string, array{string, string, string}>
51     */
52    public static function providerRenderWritesTheStatement(): iterable
53    {
54        yield 'without ELSE in 5.6' => ['mysql-5.6.51', 'create procedure p(a int) case a when 1 then select 1; when 2 then select 2; end case', 'CREATE PROCEDURE p(a INT) CASE a WHEN 1 THEN SELECT 1; WHEN 2 THEN SELECT 2; END CASE'];
55        yield 'with ELSE in 8.0' => ['mysql-8.0.44', 'create procedure p(a int) case a + 1 when 1 then select 1; else select 2; select 3; end case', 'CREATE PROCEDURE p(a INT) CASE a + 1 WHEN 1 THEN SELECT 1; ELSE SELECT 2; SELECT 3; END CASE'];
56        yield 'a string operand in 9.1' => ['mysql-9.1.0', "create procedure p(a char(1)) case a when 'x' then begin end; end case", "CREATE PROCEDURE p(a CHAR(1)) CASE a WHEN 'x' THEN BEGIN END; END CASE"];
57    }
58
59    public function testACaseWithoutBranchesIsRejected(): void
60    {
61        $this->expectExceptionMessage('CASE holds at least one WHEN branch.');
62
63        new SimpleCase(new NumberLiteral('1'), []);
64    }
65}
66