packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Program/Flow/RepeatLoopTest.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\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Semantics;
14use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
15use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\Leave;
16use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\RepeatLoop;
17
18#[CoversClass(RepeatLoop::class)]
19#[Medium]
20final class RepeatLoopTest extends TestCase
21{
22    /**
23     * @param list<string> $messages
24     */
25    #[DataProvider('providerDeriveProgramDerivesTheLabelsTheConditionAndTheStatements')]
26    public function testDeriveProgramDerivesTheLabelsTheConditionAndTheStatements(string $sql, array $messages): void
27    {
28        $operation = (new Semantics())->inspect($sql);
29
30        self::assertSame($messages, array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
31    }
32
33    /**
34     * @return iterable<string, array{string, list<string>}>
35     */
36    public static function providerDeriveProgramDerivesTheLabelsTheConditionAndTheStatements(): iterable
37    {
38        yield 'a variable in the condition' => ['CREATE PROCEDURE p() BEGIN DECLARE x INT DEFAULT 0; r: REPEAT SET x = x + 1; ITERATE r; UNTIL x > 3 END REPEAT r; END', []];
39        yield 'an unknown name in the condition' => ['CREATE PROCEDURE p(a INT) REPEAT SELECT 1; UNTIL b END REPEAT', ['Column b does not exist.']];
40        yield 'an unknown name in the statements' => ['CREATE PROCEDURE p(a INT) REPEAT SELECT z; UNTIL a END REPEAT', ['Column z does not exist.']];
41        yield 'an end label without match' => ['CREATE PROCEDURE p(a INT) r: REPEAT SELECT 1; UNTIL a END REPEAT s', ['End-label s without match']];
42        yield 'a redefined label' => ['CREATE PROCEDURE p(a INT) r: LOOP r: REPEAT LEAVE r; UNTIL a END REPEAT; END LOOP', ['Redefining label r']];
43    }
44
45    #[DataProvider('providerRenderWritesTheLoop')]
46    public function testRenderWritesTheLoop(string $release, string $sql, string $expected): void
47    {
48        self::assertSame($expected, (new Semantics($release))->inspect($sql)->toString());
49    }
50
51    /**
52     * @return iterable<string, array{string, string, string}>
53     */
54    public static function providerRenderWritesTheLoop(): iterable
55    {
56        yield 'without label in 5.6' => ['mysql-5.6.51', 'create procedure p(a int) repeat select 1; until a end repeat', 'CREATE PROCEDURE p(a INT) REPEAT SELECT 1; UNTIL a END REPEAT'];
57        yield 'with both labels in 5.7' => ['mysql-5.7.44', 'create procedure p(a int) again: repeat select 1; select 2; until a > 0 end repeat again', 'CREATE PROCEDURE p(a INT) again: REPEAT SELECT 1; SELECT 2; UNTIL a > 0 END REPEAT again'];
58        yield 'with a label in 9.1' => ['mysql-9.1.0', 'create procedure p(a int) again: repeat leave again; until a in (1, 2) end repeat', 'CREATE PROCEDURE p(a INT) again: REPEAT LEAVE again; UNTIL a IN (1, 2) END REPEAT'];
59    }
60
61    public function testALoopWithoutStatementsIsRejected(): void
62    {
63        $this->expectExceptionMessage('A statement list holds statements.');
64
65        new RepeatLoop(new NumberLiteral('1'), []);
66    }
67
68    public function testAnEndLabelWithoutALabelIsRejected(): void
69    {
70        $this->expectExceptionMessage('Only a labeled loop has an end label.');
71
72        new RepeatLoop(new NumberLiteral('1'), [new Leave(new Name('r'))], null, new Name('r'));
73    }
74}
75