packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Program/Cursor/CursorDeclarationTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Program\Cursor;
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\Query\Select;
14use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
15use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
16use SqlSemantics\Platform\MySql\Statement\Routine\Program\Cursor\CursorDeclaration;
17
18#[CoversClass(CursorDeclaration::class)]
19#[Medium]
20final class CursorDeclarationTest extends TestCase
21{
22 public function testRenderWritesTheNameAndTheQuery(): void
23 {
24 $operation = (new Semantics())->inspect('CREATE PROCEDURE p() BEGIN DECLARE x INT; DECLARE c CURSOR FOR SELECT x; END');
25 $create = $operation->statement;
26 self::assertInstanceOf(CreateProcedure::class, $create);
27 $block = $create->body;
28 self::assertInstanceOf(Block::class, $block);
29 $cursor = $block->declarations[1];
30 self::assertInstanceOf(CursorDeclaration::class, $cursor);
31
32 self::assertSame('c', $cursor->name->value);
33 self::assertInstanceOf(Select::class, $cursor->query);
34 self::assertSame([], $operation->facts->diagnostics);
35 self::assertSame('CREATE PROCEDURE p() BEGIN DECLARE x INT; DECLARE c CURSOR FOR SELECT x; END', $operation->toString());
36 }
37
38 public function testRenderWritesAQueryThatSeesOnlyTheVariablesDeclaredBefore(): void
39 {
40 $operation = (new Semantics())->inspect('CREATE PROCEDURE p() BEGIN DECLARE c CURSOR FOR SELECT y; END');
41
42 self::assertSame(['Column y does not exist.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
43 self::assertSame('CREATE PROCEDURE p() BEGIN DECLARE c CURSOR FOR SELECT y; END', $operation->toString());
44 }
45
46 #[DataProvider('providerRenderWritesTheDeclaration')]
47 public function testRenderWritesTheDeclaration(string $release, string $sql, string $expected): void
48 {
49 self::assertSame($expected, (new Semantics($release))->inspect($sql)->toString());
50 }
51
52 /**
53 * @return iterable<string, array{string, string, string}>
54 */
55 public static function providerRenderWritesTheDeclaration(): iterable
56 {
57 yield 'a parenthesized union in 5.7' => ['mysql-5.7.44', 'create procedure p() begin declare c cursor for (select 1) union (select 2); end', 'CREATE PROCEDURE p() BEGIN DECLARE c CURSOR FOR (SELECT 1) UNION (SELECT 2); END'];
58 yield 'a WITH query in 8.0' => ['mysql-8.0.44', 'create procedure p() begin declare c cursor for with q as (select 1 as a) select a from q; end', 'CREATE PROCEDURE p() BEGIN DECLARE c CURSOR FOR WITH q AS (SELECT 1 AS a) SELECT a FROM q; END'];
59 yield 'VALUES in 9.1' => ['mysql-9.1.0', 'create procedure p() begin declare c cursor for values row(1); end', 'CREATE PROCEDURE p() BEGIN DECLARE c CURSOR FOR VALUES ROW(1); END'];
60 }
61}
62