packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Program/HandlerDeclarationTest.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\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\MySql\Semantics;
13use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
14use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ConditionClass;
15use SqlSemantics\Platform\MySql\Statement\Routine\Condition\GeneralCondition;
16use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
17use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
18use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\Leave;
19use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerAction;
20use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerDeclaration;
21
22#[CoversClass(HandlerDeclaration::class)]
23#[Medium]
24final class HandlerDeclarationTest extends TestCase
25{
26    public function testRenderWritesTheActionTheConditionsAndTheStatement(): void
27    {
28        $operation = (new Semantics())->analyze('CREATE PROCEDURE p() BEGIN DECLARE e CONDITION FOR 1051; DECLARE CONTINUE HANDLER FOR e, NOT FOUND, 1146 BEGIN END; END');
29        $create = $operation->statement;
30        self::assertInstanceOf(CreateProcedure::class, $create);
31        $block = $create->body;
32        self::assertInstanceOf(Block::class, $block);
33        $handler = $block->declarations[1];
34        self::assertInstanceOf(HandlerDeclaration::class, $handler);
35
36        self::assertSame(HandlerAction::Continue, $handler->action);
37        self::assertCount(3, $handler->conditions);
38        self::assertInstanceOf(Block::class, $handler->statement);
39        self::assertSame([], $operation->facts->diagnostics);
40        self::assertSame('CREATE PROCEDURE p() BEGIN DECLARE e CONDITION FOR 1051; DECLARE CONTINUE HANDLER FOR e, NOT FOUND, 1146 BEGIN END; END', $operation->toString());
41    }
42
43    #[DataProvider('providerRenderWritesTheHandler')]
44    public function testRenderWritesTheHandler(string $release, string $sql, string $expected): void
45    {
46        self::assertSame($expected, (new Semantics($release))->analyze($sql)->toString());
47    }
48
49    /**
50     * @return iterable<string, array{string, string, string}>
51     */
52    public static function providerRenderWritesTheHandler(): iterable
53    {
54        yield 'EXIT with an SQL statement in 5.6' => ['mysql-5.6.51', "create procedure p() begin declare exit handler for sqlstate value '42S02', sqlwarning select 1; end", "CREATE PROCEDURE p() BEGIN DECLARE EXIT HANDLER FOR SQLSTATE '42S02', SQLWARNING SELECT 1; END"];
55        yield 'CONTINUE with SET in 5.7' => ['mysql-5.7.44', 'create procedure p() begin declare x int; declare continue handler for sqlexception set x = 1; end', 'CREATE PROCEDURE p() BEGIN DECLARE x INT; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET x = 1; END'];
56        yield 'a labeled block in 9.1' => ['mysql-9.1.0', 'create procedure p() begin declare exit handler for not found h: begin leave h; end h; end', 'CREATE PROCEDURE p() BEGIN DECLARE EXIT HANDLER FOR NOT FOUND h: BEGIN LEAVE h; END h; END'];
57    }
58
59    public function testAHandlerWithoutConditionsIsRejected(): void
60    {
61        $this->expectExceptionMessage('A handler names at least one condition.');
62
63        new HandlerDeclaration(HandlerAction::Exit, [], new Leave(new Name('a')));
64    }
65
66    public function testAStatementOfAnotherClassIsRejected(): void
67    {
68        $this->expectExceptionMessage('A member of a stored program is a program statement or an SQL statement.');
69
70        new HandlerDeclaration(HandlerAction::Exit, [new GeneralCondition(ConditionClass::SqlWarning)], new NumberLiteral('1'));
71    }
72}
73