packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Program/Flow/IfStatementTest.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\IfStatement;
15
16#[CoversClass(IfStatement::class)]
17#[Medium]
18final class IfStatementTest extends TestCase
19{
20    public function testDeriveProgramDerivesEveryBranch(): void
21    {
22        $operation = (new Semantics())->inspect('CREATE PROCEDURE p(a INT) IF b THEN SELECT c; ELSEIF a THEN SELECT d; ELSE SELECT e; END IF');
23        $create = $operation->statement;
24        self::assertInstanceOf(CreateProcedure::class, $create);
25        $if = $create->body;
26        self::assertInstanceOf(IfStatement::class, $if);
27
28        self::assertCount(2, $if->branches);
29        self::assertCount(1, $if->otherwise);
30        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));
31    }
32
33    public function testDeriveProgramResolvesTheVariablesInScope(): void
34    {
35        $operation = (new Semantics())->inspect('CREATE PROCEDURE p() BEGIN DECLARE x INT DEFAULT 1; l: LOOP IF x > 1 THEN LEAVE l; ELSE SET x = x + 1; ITERATE l; END IF; END LOOP; END');
36
37        self::assertSame([], $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 'IF, ELSEIF and ELSE in 5.6' => ['mysql-5.6.51', 'create procedure p(a int) if a = 1 then select 1; elseif a = 2 then select 2; select 3; else select 4; end if', 'CREATE PROCEDURE p(a INT) IF a = 1 THEN SELECT 1; ELSEIF a = 2 THEN SELECT 2; SELECT 3; ELSE SELECT 4; END IF'];
52        yield 'IF alone in 8.0' => ['mysql-8.0.44', 'create procedure p(a int) if a then select 1; end if', 'CREATE PROCEDURE p(a INT) IF a THEN SELECT 1; END IF'];
53        yield 'nested IF in 9.1' => ['mysql-9.1.0', 'create procedure p(a int) if a then if not a then begin end; end if; end if', 'CREATE PROCEDURE p(a INT) IF a THEN IF NOT a THEN BEGIN END; END IF; END IF'];
54    }
55
56    public function testAnIfWithoutBranchesIsRejected(): void
57    {
58        $this->expectExceptionMessage('IF holds at least one branch.');
59
60        new IfStatement([]);
61    }
62}
63