packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Condition/GeneralConditionTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Condition;
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\Contract\GrammarRelease;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Rendering\Codec;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ConditionClass;
17use SqlSemantics\Platform\MySql\Statement\Routine\Condition\GeneralCondition;
18use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
19use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
20use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerDeclaration;
21
22#[CoversClass(GeneralCondition::class)]
23#[Medium]
24final class GeneralConditionTest extends TestCase
25{
26 /**
27 * @return iterable<string, array{ConditionClass, string}>
28 */
29 public static function providerRenderWritesTheKeywordsOfTheClass(): iterable
30 {
31 yield 'warning' => [ConditionClass::SqlWarning, 'SQLWARNING'];
32 yield 'not found' => [ConditionClass::NotFound, 'NOT FOUND'];
33 yield 'exception' => [ConditionClass::SqlException, 'SQLEXCEPTION'];
34 }
35
36 #[DataProvider('providerRenderWritesTheKeywordsOfTheClass')]
37 public function testRenderWritesTheKeywordsOfTheClass(ConditionClass $class, string $expected): void
38 {
39 $out = new Output(new Codec(GrammarRelease::MySql847));
40 (new GeneralCondition($class))->render($out);
41
42 self::assertSame($expected, (new Lexical())->join($out->pieces()));
43 }
44
45 /**
46 * @return iterable<string, array{string}>
47 */
48 public static function providerRenderWritesTheClassesAHandlerNames(): iterable
49 {
50 yield 'mysql 5.6' => ['mysql-5.6.51'];
51 yield 'mysql 8.0' => ['mysql-8.0.44'];
52 yield 'mysql 9.1' => ['mysql-9.1.0'];
53 }
54
55 #[DataProvider('providerRenderWritesTheClassesAHandlerNames')]
56 public function testRenderWritesTheClassesAHandlerNames(string $release): void
57 {
58 $create = (new Semantics($release))->analyze('create procedure p() begin declare continue handler for sqlwarning, not found, sqlexception begin end; end');
59 $statement = $create->statement;
60 self::assertInstanceOf(CreateProcedure::class, $statement);
61 $body = $statement->body;
62 self::assertInstanceOf(Block::class, $body);
63 $handler = $body->declarations[0];
64 self::assertInstanceOf(HandlerDeclaration::class, $handler);
65
66 self::assertEquals(
67 [new GeneralCondition(ConditionClass::SqlWarning), new GeneralCondition(ConditionClass::NotFound), new GeneralCondition(ConditionClass::SqlException)],
68 $handler->conditions,
69 );
70 self::assertSame([], $create->facts->diagnostics);
71 self::assertSame('CREATE PROCEDURE p() BEGIN DECLARE CONTINUE HANDLER FOR SQLWARNING, NOT FOUND, SQLEXCEPTION BEGIN END; END', $create->toString());
72 }
73}
74