packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Program/Flow/SearchedCaseTest.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\Routine\CreateProcedure;
14use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\SearchedCase;
15
16#[CoversClass(SearchedCase::class)]
17#[Medium]
18final class SearchedCaseTest extends TestCase
19{
20    public function testDeriveProgramDerivesEveryBranch(): void
21    {
22        $operation = (new Semantics())->inspect('CREATE PROCEDURE p(a INT) CASE WHEN b THEN SELECT d; WHEN a THEN SELECT a; ELSE SELECT e; END CASE');
23        $create = $operation->statement;
24        self::assertInstanceOf(CreateProcedure::class, $create);
25        $case = $create->body;
26        self::assertInstanceOf(SearchedCase::class, $case);
27
28        self::assertCount(2, $case->branches);
29        self::assertCount(1, $case->otherwise);
30        self::assertSame(['Column b 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));
31    }
32
33    public function testDeriveProgramChecksTheLabelsInTheBranches(): void
34    {
35        $operation = (new Semantics())->inspect('CREATE PROCEDURE p(a INT) l: LOOP CASE WHEN a > 0 THEN LEAVE l; ELSE LEAVE m; END CASE; END LOOP');
36
37        self::assertSame(['LEAVE with no matching label: m'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
38    }
39
40    #[DataProvider('providerRenderWritesTheStatement')]
41    public function testRenderWritesTheStatement(string $release, string $sql, string $expected): void
42    {
43        self::assertSame($expected, (new Semantics($release))->inspect($sql)->toString());
44    }
45
46    /**
47     * @return iterable<string, array{string, string, string}>
48     */
49    public static function providerRenderWritesTheStatement(): iterable
50    {
51        yield 'without ELSE in 5.6' => ['mysql-5.6.51', 'create procedure p(a int) case when a > 1 then select 1; when a < 0 then select 2; end case', 'CREATE PROCEDURE p(a INT) CASE WHEN a > 1 THEN SELECT 1; WHEN a < 0 THEN SELECT 2; END CASE'];
52        yield 'with ELSE in 5.7' => ['mysql-5.7.44', 'create procedure p(a int) case when a > 1 then select 1; else select 3; select 4; end case', 'CREATE PROCEDURE p(a INT) CASE WHEN a > 1 THEN SELECT 1; ELSE SELECT 3; SELECT 4; END CASE'];
53        yield 'with an empty block in 9.1' => ['mysql-9.1.0', 'create procedure p(a int) case when a is null then begin end; end case', 'CREATE PROCEDURE p(a INT) CASE WHEN a IS NULL THEN BEGIN END; END CASE'];
54    }
55
56    public function testACaseWithoutBranchesIsRejected(): void
57    {
58        $this->expectExceptionMessage('CASE holds at least one WHEN branch.');
59
60        new SearchedCase([]);
61    }
62}
63