packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Condition/Diagnostics/ConditionDiagnosticsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Condition\Diagnostics;
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\Diagnostics\ConditionDiagnostics;
19use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\GetDiagnostics;
20use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\InformationItem;
21use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\StatementItemName;
22use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
23
24#[CoversClass(ConditionDiagnostics::class)]
25#[Medium]
26final class ConditionDiagnosticsTest extends TestCase
27{
28    public function testRenderWritesTheNumberAndTheItems(): void
29    {
30        $out = new Output(new Codec(GrammarRelease::MySql847));
31        (new ConditionDiagnostics(new NumberLiteral('2'), [
32            new InformationItem(new UserVariable(new Name('m')), ConditionItemName::MessageText),
33            new InformationItem(new Name('e'), ConditionItemName::MysqlErrno),
34        ]))->render($out);
35
36        self::assertSame('CONDITION 2 @m = MESSAGE_TEXT, e = MYSQL_ERRNO', (new Lexical())->join($out->pieces()));
37    }
38
39    public function testRenderWritesTheAnalyzedInformation(): void
40    {
41        $get = (new Semantics('mysql-8.0.44'))->analyze('GET DIAGNOSTICS CONDITION 2 @m = MESSAGE_TEXT, @c = CURSOR_NAME');
42        $statement = $get->statement;
43        self::assertInstanceOf(GetDiagnostics::class, $statement);
44        self::assertInstanceOf(ConditionDiagnostics::class, $statement->information);
45
46        self::assertEquals(new NumberLiteral('2'), $statement->information->number);
47        self::assertSame(
48            [ConditionItemName::MessageText, ConditionItemName::CursorName],
49            array_map(static fn (InformationItem $item): ConditionItemName|StatementItemName => $item->item, $statement->information->items),
50        );
51        self::assertSame('GET DIAGNOSTICS CONDITION 2 @m = MESSAGE_TEXT, @c = CURSOR_NAME', $get->toString());
52    }
53
54    public function testRefusesAnEmptyList(): void
55    {
56        $this->expectExceptionMessage('GET DIAGNOSTICS reads at least one item.');
57
58        new ConditionDiagnostics(new NumberLiteral('1'), []);
59    }
60
61    public function testRefusesAStatementInformationItem(): void
62    {
63        $this->expectExceptionMessage('Condition information holds condition information items.');
64
65        new ConditionDiagnostics(new NumberLiteral('1'), [
66            new InformationItem(new UserVariable(new Name('m')), ConditionItemName::MessageText),
67            new InformationItem(new UserVariable(new Name('n')), StatementItemName::Number),
68        ]);
69    }
70}
71