packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Program/Flow/LeaveTest.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\Program\Flow\Leave;
14
15#[CoversClass(Leave::class)]
16#[Medium]
17final class LeaveTest extends TestCase
18{
19 /**
20 * @param list<string> $messages
21 */
22 #[DataProvider('providerDeriveProgramLooksTheLabelUp')]
23 public function testDeriveProgramLooksTheLabelUp(string $sql, array $messages): void
24 {
25 $operation = (new Semantics())->inspect($sql);
26
27 self::assertSame($messages, array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
28 }
29
30 /**
31 * @return iterable<string, array{string, list<string>}>
32 */
33 public static function providerDeriveProgramLooksTheLabelUp(): iterable
34 {
35 yield 'the label of the loop' => ['CREATE PROCEDURE p() l: LOOP LEAVE l; END LOOP', []];
36 yield 'the label of an outer block' => ['CREATE PROCEDURE p() b: BEGIN l: LOOP LEAVE b; END LOOP; END', []];
37 yield 'a label in another case' => ['CREATE PROCEDURE p() L: LOOP LEAVE l; END LOOP l', []];
38 yield 'no label' => ['CREATE PROCEDURE p() BEGIN LEAVE zz; END', ['LEAVE with no matching label: zz']];
39 yield 'the label of a closed loop' => ['CREATE PROCEDURE p() BEGIN l: LOOP LEAVE l; END LOOP; LEAVE l; END', ['LEAVE with no matching label: l']];
40 yield 'a label outside a handler' => ['CREATE PROCEDURE p() a: BEGIN DECLARE CONTINUE HANDLER FOR SQLEXCEPTION LEAVE a; END a', ['LEAVE with no matching label: a']];
41 yield 'the label of a handler block' => ['CREATE PROCEDURE p() BEGIN DECLARE CONTINUE HANDLER FOR SQLEXCEPTION h: BEGIN LEAVE h; END; END', []];
42 }
43
44 #[DataProvider('providerRenderWritesTheStatement')]
45 public function testRenderWritesTheStatement(string $release, string $sql, string $expected): void
46 {
47 self::assertSame($expected, (new Semantics($release))->inspect($sql)->toString());
48 }
49
50 /**
51 * @return iterable<string, array{string, string, string}>
52 */
53 public static function providerRenderWritesTheStatement(): iterable
54 {
55 yield '5.6' => ['mysql-5.6.51', 'create procedure p() l: loop leave l; end loop l', 'CREATE PROCEDURE p() l: LOOP LEAVE l; END LOOP l'];
56 yield 'a quoted label in 8.4' => ['mysql-8.4.7', 'create procedure p() `my label`: begin leave `my label`; end', 'CREATE PROCEDURE p() `my label`: BEGIN LEAVE `my label`; END'];
57 }
58}
59