packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Program/Flow/WhileLoopTest.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\WhileLoop;
17
18#[CoversClass(WhileLoop::class)]
19#[Medium]
20final class WhileLoopTest 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 3; w: WHILE x > 0 DO SET x = x - 1; ITERATE w; END WHILE w; END', []];
39        yield 'an unknown name in the condition' => ['CREATE PROCEDURE p(a INT) WHILE b > 0 DO SELECT 1; END WHILE', ['Column b does not exist.']];
40        yield 'an unknown name in the statements' => ['CREATE PROCEDURE p(a INT) WHILE a DO SELECT z; END WHILE', ['Column z does not exist.']];
41        yield 'an end label without match' => ['CREATE PROCEDURE p(a INT) w: WHILE a > 0 DO LEAVE w; END WHILE v', ['End-label v without match']];
42        yield 'a redefined label' => ['CREATE PROCEDURE p(a INT) w: BEGIN w: WHILE a DO LEAVE w; END WHILE; END', ['Redefining label w']];
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) while a do select 1; end while', 'CREATE PROCEDURE p(a INT) WHILE a DO SELECT 1; END WHILE'];
57        yield 'with both labels in 8.0' => ['mysql-8.0.44', 'create procedure p(a int) again: while a > 0 do select 1; select 2; end while again', 'CREATE PROCEDURE p(a INT) again: WHILE a > 0 DO SELECT 1; SELECT 2; END WHILE again'];
58        yield 'with a label in 9.1' => ['mysql-9.1.0', 'create procedure p(a int) again: while a is not null do leave again; end while', 'CREATE PROCEDURE p(a INT) again: WHILE a IS NOT NULL DO LEAVE again; END WHILE'];
59    }
60
61    public function testALoopWithoutStatementsIsRejected(): void
62    {
63        $this->expectExceptionMessage('A statement list holds statements.');
64
65        new WhileLoop(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 WhileLoop(new NumberLiteral('1'), [new Leave(new Name('w'))], null, new Name('w'));
73    }
74}
75