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