packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Condition/SignalItemTest.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\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
12use SqlSemantics\Platform\MySql\Model\Rendering\Output;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Rendering\Codec;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
17use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ConditionItemName;
18use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Signal;
19use SqlSemantics\Platform\MySql\Statement\Routine\Condition\SignalItem;
20use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
21
22#[CoversClass(SignalItem::class)]
23#[Medium]
24final class SignalItemTest extends TestCase
25{
26 public function testRenderWritesTheItemAndItsValue(): void
27 {
28 $out = new Output(new Codec(GrammarRelease::MySql847));
29 (new SignalItem(ConditionItemName::MysqlErrno, new NumberLiteral('1001')))->render($out);
30
31 self::assertSame('MYSQL_ERRNO = 1001', (new Lexical())->join($out->pieces()));
32 }
33
34 public function testRenderWritesEveryKindOfValue(): void
35 {
36 $signal = (new Semantics())->analyze("SIGNAL SQLSTATE '45000' SET message_text = @m, mysql_errno = NULL, class_origin = x'41', table_name = 't'");
37 $statement = $signal->statement;
38 self::assertInstanceOf(Signal::class, $statement);
39
40 self::assertSame(
41 [ConditionItemName::MessageText, ConditionItemName::MysqlErrno, ConditionItemName::ClassOrigin, ConditionItemName::TableName],
42 array_map(static fn (SignalItem $item): ConditionItemName => $item->name, $statement->items),
43 );
44 self::assertEquals(new UserVariable(new Name('m')), $statement->items[0]->value);
45 self::assertSame("SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @m, MYSQL_ERRNO = NULL, CLASS_ORIGIN = x'41', TABLE_NAME = 't'", $signal->toString());
46 }
47
48 public function testRefusesTheReturnedSqlstate(): void
49 {
50 $this->expectExceptionMessage('RETURNED_SQLSTATE is set by the condition value, not by an item.');
51
52 new SignalItem(ConditionItemName::ReturnedSqlstate, new UserVariable(new Name('s')));
53 }
54}
55