packages/sql-semantics-mysql/tests/Unit/Lowering/Routine/Condition/ConditionRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Routine\Condition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\Medium;
10use PHPUnit\Framework\TestCase;
11use SqlParser\Parser\Node;
12use SqlSemantics\Platform\MySql\Lowering\Lowering;
13use SqlSemantics\Platform\MySql\Lowering\Routine\Condition\ConditionRule;
14use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
15use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Semantics;
19use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
20use SqlSemantics\Platform\MySql\Statement\Literal\Text;
21use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ConditionClass;
22use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ConditionName;
23use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ErrorCode;
24use SqlSemantics\Platform\MySql\Statement\Routine\Condition\GeneralCondition;
25use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Resignal;
26use SqlSemantics\Platform\MySql\Statement\Routine\Condition\SqlState;
27use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
28use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
29use SqlSemantics\Platform\MySql\Statement\Routine\Program\ConditionDeclaration;
30use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerAction;
31use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerDeclaration;
32
33#[CoversClass(ConditionRule::class)]
34#[Medium]
35final class ConditionRuleTest extends TestCase
36{
37 /**
38 * @return iterable<string, array{string}>
39 */
40 public static function providerValueLowersAnErrorCodeAndAnSqlstate(): iterable
41 {
42 yield 'mysql 5.6' => ['mysql-5.6.51'];
43 yield 'mysql 5.7' => ['mysql-5.7.44'];
44 yield 'mysql 8.0' => ['mysql-8.0.44'];
45 yield 'mysql 9.1' => ['mysql-9.1.0'];
46 }
47
48 #[DataProvider('providerValueLowersAnErrorCodeAndAnSqlstate')]
49 public function testValueLowersAnErrorCodeAndAnSqlstate(string $release): void
50 {
51 $operation = (new Semantics($release))->analyze("create procedure p() begin declare c condition for 1051; declare d condition for sqlstate '42S02'; declare e condition for sqlstate value '42S01'; end");
52 $statement = $operation->statement;
53 self::assertInstanceOf(CreateProcedure::class, $statement);
54 self::assertInstanceOf(Block::class, $statement->body);
55 self::assertEquals([
56 new ConditionDeclaration(new Name('c'), new ErrorCode(new Numeral('1051'))),
57 new ConditionDeclaration(new Name('d'), new SqlState(new Text('42S02'))),
58 new ConditionDeclaration(new Name('e'), new SqlState(new Text('42S01'))),
59 ], $statement->body->declarations);
60 self::assertSame("CREATE PROCEDURE p() BEGIN DECLARE c CONDITION FOR 1051; DECLARE d CONDITION FOR SQLSTATE '42S02'; DECLARE e CONDITION FOR SQLSTATE '42S01'; END", $operation->toString());
61 }
62
63 public function testValueLowersTheSqlstateOfResignal(): void
64 {
65 $operation = (new Semantics())->analyze("resignal sqlstate value '45000'");
66
67 self::assertEquals(new Resignal(new SqlState(new Text('45000'))), $operation->statement);
68 self::assertSame("RESIGNAL SQLSTATE '45000'", $operation->toString());
69 }
70
71 public function testValueRefusesANodeOfNoProduction(): void
72 {
73 $platform = new Platform();
74 $profile = $platform->profile(null, null, ParameterStyle::Native);
75 $rule = new ConditionRule(new Lowering($platform->productions($profile), new Leaves(), $profile));
76
77 $this->expectExceptionMessage('The grammar release has no production rule#0.');
78
79 $rule->value(new Node('rule', 0, []));
80 }
81
82 /**
83 * @return iterable<string, array{string}>
84 */
85 public static function providerHandledLowersEveryKindOfCondition(): iterable
86 {
87 yield 'mysql 5.6' => ['mysql-5.6.51'];
88 yield 'mysql 5.7' => ['mysql-5.7.44'];
89 yield 'mysql 8.0' => ['mysql-8.0.44'];
90 yield 'mysql 9.1' => ['mysql-9.1.0'];
91 }
92
93 #[DataProvider('providerHandledLowersEveryKindOfCondition')]
94 public function testHandledLowersEveryKindOfCondition(string $release): void
95 {
96 $operation = (new Semantics($release))->analyze("create procedure p() begin declare c condition for 1051; declare continue handler for c, 1052, sqlstate value '42000', sqlwarning, not found, sqlexception set @x = 1; end");
97 $statement = $operation->statement;
98 self::assertInstanceOf(CreateProcedure::class, $statement);
99 self::assertInstanceOf(Block::class, $statement->body);
100 $handler = $statement->body->declarations[1];
101 self::assertInstanceOf(HandlerDeclaration::class, $handler);
102 self::assertSame(HandlerAction::Continue, $handler->action);
103 self::assertEquals([
104 new ConditionName(new Name('c')),
105 new ErrorCode(new Numeral('1052')),
106 new SqlState(new Text('42000')),
107 new GeneralCondition(ConditionClass::SqlWarning),
108 new GeneralCondition(ConditionClass::NotFound),
109 new GeneralCondition(ConditionClass::SqlException),
110 ], $handler->conditions);
111 self::assertSame("CREATE PROCEDURE p() BEGIN DECLARE c CONDITION FOR 1051; DECLARE CONTINUE HANDLER FOR c, 1052, SQLSTATE '42000', SQLWARNING, NOT FOUND, SQLEXCEPTION SET @x = 1; END", $operation->toString());
112 self::assertSame([], $operation->facts->diagnostics);
113 }
114
115 public function testHandledLowersASingleCondition(): void
116 {
117 $operation = (new Semantics())->analyze('create procedure p() begin declare exit handler for sqlexception begin end; end');
118 $statement = $operation->statement;
119 self::assertInstanceOf(CreateProcedure::class, $statement);
120 self::assertInstanceOf(Block::class, $statement->body);
121
122 self::assertEquals([new HandlerDeclaration(HandlerAction::Exit, [new GeneralCondition(ConditionClass::SqlException)], new Block())], $statement->body->declarations);
123 self::assertSame('CREATE PROCEDURE p() BEGIN DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN END; END', $operation->toString());
124 }
125}
126