packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Condition/ConditionTest.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\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Semantics;
11use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Condition;
12use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ConditionName;
13use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ErrorCode;
14use SqlSemantics\Platform\MySql\Statement\Routine\Condition\GeneralCondition;
15use SqlSemantics\Platform\MySql\Statement\Routine\Condition\SignalItem;
16use SqlSemantics\Platform\MySql\Statement\Routine\Condition\SqlState;
17use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
18use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
19use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerDeclaration;
20
21#[CoversClass(Condition::class)]
22#[Medium]
23final class ConditionTest extends TestCase
24{
25 public function testImplementationsAreTheValuesAHandlerNames(): void
26 {
27 $create = (new Semantics())->analyze("CREATE PROCEDURE p() BEGIN DECLARE c CONDITION FOR 1051; DECLARE CONTINUE HANDLER FOR SQLWARNING, 1052, SQLSTATE '42S02', c BEGIN END; END");
28 $statement = $create->statement;
29 self::assertInstanceOf(CreateProcedure::class, $statement);
30 $body = $statement->body;
31 self::assertInstanceOf(Block::class, $body);
32 $handler = $body->declarations[1];
33 self::assertInstanceOf(HandlerDeclaration::class, $handler);
34
35 self::assertSame(
36 [GeneralCondition::class, ErrorCode::class, SqlState::class, ConditionName::class],
37 array_map(static fn (Condition $condition): string => $condition::class, $handler->conditions),
38 );
39 self::assertNotContains(Condition::class, class_implements(SignalItem::class));
40 }
41}
42