packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Program/ConditionDeclarationTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Program;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\Medium;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Platform\MySql\Semantics;
12use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ErrorCode;
13use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
14use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
15use SqlSemantics\Platform\MySql\Statement\Routine\Program\ConditionDeclaration;
16
17#[CoversClass(ConditionDeclaration::class)]
18#[Medium]
19final class ConditionDeclarationTest extends TestCase
20{
21 public function testRenderWritesTheNameAndTheErrorCode(): void
22 {
23 $operation = (new Semantics())->analyze('CREATE PROCEDURE p() BEGIN DECLARE gone CONDITION FOR 1051; END');
24 $create = $operation->statement;
25 self::assertInstanceOf(CreateProcedure::class, $create);
26 $block = $create->body;
27 self::assertInstanceOf(Block::class, $block);
28 $declaration = $block->declarations[0];
29 self::assertInstanceOf(ConditionDeclaration::class, $declaration);
30
31 self::assertSame('gone', $declaration->name->value);
32 self::assertInstanceOf(ErrorCode::class, $declaration->value);
33 self::assertSame('CREATE PROCEDURE p() BEGIN DECLARE gone CONDITION FOR 1051; END', $operation->toString());
34 }
35
36 #[DataProvider('providerRenderWritesTheDeclaration')]
37 public function testRenderWritesTheDeclaration(string $release, string $sql, string $expected): void
38 {
39 self::assertSame($expected, (new Semantics($release))->analyze($sql)->toString());
40 }
41
42 /**
43 * @return iterable<string, array{string, string, string}>
44 */
45 public static function providerRenderWritesTheDeclaration(): iterable
46 {
47 yield 'an SQLSTATE value in 5.6' => ['mysql-5.6.51', "create procedure p() begin declare e condition for sqlstate value '42S02'; end", "CREATE PROCEDURE p() BEGIN DECLARE e CONDITION FOR SQLSTATE '42S02'; END"];
48 yield 'an SQLSTATE value in 8.4' => ['mysql-8.4.7', "create procedure p() begin declare e condition for sqlstate '42S02'; end", "CREATE PROCEDURE p() BEGIN DECLARE e CONDITION FOR SQLSTATE '42S02'; END"];
49 yield 'an error code in 9.1' => ['mysql-9.1.0', 'create procedure p() begin declare `e x` condition for 1146; end', 'CREATE PROCEDURE p() BEGIN DECLARE `e x` CONDITION FOR 1146; END'];
50 }
51}
52