packages/sql-semantics-mysql/tests/Unit/Rules/Routine/CursorFactsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
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\Rules\Routine\CursorFacts;
13use SqlSemantics\Platform\MySql\Semantics;
14
15#[CoversClass(CursorFacts::class)]
16#[Medium]
17final class CursorFactsTest extends TestCase
18{
19 /**
20 * @param list<string> $messages
21 */
22 #[DataProvider('providerCursorReportsACursorDeclaredNowhereAround')]
23 public function testCursorReportsACursorDeclaredNowhereAround(string $release, string $sql, array $messages): void
24 {
25 $operation = (new Semantics($release))->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, string, list<string>}>
32 */
33 public static function providerCursorReportsACursorDeclaredNowhereAround(): iterable
34 {
35 yield 'OPEN without a declaration in 5.6' => ['mysql-5.6.51', 'CREATE PROCEDURE p() OPEN c', ['Undefined CURSOR: c']];
36 yield 'CLOSE of another cursor in 8.0' => ['mysql-8.0.44', 'CREATE PROCEDURE p() BEGIN DECLARE c CURSOR FOR SELECT 1; CLOSE d; END', ['Undefined CURSOR: d']];
37 yield 'FETCH of another cursor in 9.1' => ['mysql-9.1.0', 'CREATE PROCEDURE p() BEGIN DECLARE x INT; DECLARE c CURSOR FOR SELECT 1; FETCH d INTO x; END', ['Undefined CURSOR: d']];
38 yield 'a cursor of an inner block' => ['mysql-9.1.0', 'CREATE PROCEDURE p() BEGIN BEGIN DECLARE c CURSOR FOR SELECT 1; END; OPEN c; END', ['Undefined CURSOR: c']];
39 yield 'a cursor declared after the handler' => ['mysql-9.1.0', 'CREATE PROCEDURE p() BEGIN DECLARE EXIT HANDLER FOR SQLWARNING OPEN c; DECLARE c CURSOR FOR SELECT 1; END', ['Undefined CURSOR: c', 'Cursor declaration after handler declaration']];
40 yield 'a cursor of an outer block' => ['mysql-9.1.0', 'CREATE PROCEDURE p() BEGIN DECLARE c CURSOR FOR SELECT 1; BEGIN OPEN c; CLOSE c; END; END', []];
41 yield 'a cursor in another case' => ['mysql-5.7.44', 'CREATE PROCEDURE p() BEGIN DECLARE x INT; DECLARE Cur CURSOR FOR SELECT 1; OPEN CUR; FETCH cur INTO x; CLOSE cUR; END', []];
42 yield 'a cursor in a handler' => ['mysql-9.1.0', 'CREATE PROCEDURE p() BEGIN DECLARE c CURSOR FOR SELECT 1; DECLARE CONTINUE HANDLER FOR NOT FOUND CLOSE c; END', []];
43 }
44
45 /**
46 * @param list<string> $messages
47 */
48 #[DataProvider('providerVariablesReportsEveryTargetThatIsNoVariable')]
49 public function testVariablesReportsEveryTargetThatIsNoVariable(string $sql, array $messages): void
50 {
51 $operation = (new Semantics())->inspect($sql);
52
53 self::assertSame($messages, array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
54 }
55
56 /**
57 * @return iterable<string, array{string, list<string>}>
58 */
59 public static function providerVariablesReportsEveryTargetThatIsNoVariable(): iterable
60 {
61 yield 'two undeclared targets' => ['CREATE PROCEDURE p() BEGIN DECLARE x INT; DECLARE c CURSOR FOR SELECT 1, 2, 3; FETCH NEXT FROM c INTO y, x, z; END', ['Undeclared variable: y', 'Undeclared variable: z']];
62 yield 'a variable of a closed block' => ['CREATE PROCEDURE p() BEGIN DECLARE c CURSOR FOR SELECT 1; BEGIN DECLARE x INT; END; FETCH c INTO x; END', ['Undeclared variable: x']];
63 yield 'a parameter and an outer variable' => ['CREATE PROCEDURE p(OUT a INT) BEGIN DECLARE b INT; DECLARE c CURSOR FOR SELECT 1, 2; BEGIN FETCH c INTO A, b; END; END', []];
64 yield 'targets in a handler' => ['CREATE PROCEDURE p() BEGIN DECLARE x INT; DECLARE c CURSOR FOR SELECT 1; DECLARE CONTINUE HANDLER FOR NOT FOUND FETCH c INTO x, y; END', ['Undeclared variable: y']];
65 }
66
67 public function testVariablesReportsATargetOfGetDiagnosticsThatIsNoVariable(): void
68 {
69 $operation = (new Semantics())->inspect('CREATE PROCEDURE p(n INT) GET DIAGNOSTICS n = NUMBER, r = ROW_COUNT');
70
71 self::assertSame(['Undeclared variable: r'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
72 }
73}
74