packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Condition/Diagnostics/StatementDiagnosticsTest.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\Routine\Condition\ConditionItemName;
17use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\GetDiagnostics;
18use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\InformationItem;
19use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\StatementDiagnostics;
20use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\StatementItemName;
21use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
22
23#[CoversClass(StatementDiagnostics::class)]
24#[Medium]
25final class StatementDiagnosticsTest extends TestCase
26{
27 public function testRenderWritesTheItems(): void
28 {
29 $out = new Output(new Codec(GrammarRelease::MySql5744));
30 (new StatementDiagnostics([
31 new InformationItem(new Name('n'), StatementItemName::Number),
32 new InformationItem(new UserVariable(new Name('r')), StatementItemName::RowCount),
33 ]))->render($out);
34
35 self::assertSame('n = NUMBER, @r = ROW_COUNT', (new Lexical())->join($out->pieces()));
36 }
37
38 public function testRenderWritesTheAnalyzedInformation(): void
39 {
40 $get = (new Semantics('mysql-5.6.51'))->analyze('get diagnostics @r = row_count, @n = number');
41 $statement = $get->statement;
42 self::assertInstanceOf(GetDiagnostics::class, $statement);
43 self::assertInstanceOf(StatementDiagnostics::class, $statement->information);
44
45 self::assertSame(
46 [StatementItemName::RowCount, StatementItemName::Number],
47 array_map(static fn (InformationItem $item): ConditionItemName|StatementItemName => $item->item, $statement->information->items),
48 );
49 self::assertSame('GET DIAGNOSTICS @r = ROW_COUNT, @n = NUMBER', $get->toString());
50 }
51
52 public function testRefusesAnEmptyList(): void
53 {
54 $this->expectExceptionMessage('GET DIAGNOSTICS reads at least one item.');
55
56 new StatementDiagnostics([]);
57 }
58
59 public function testRefusesAConditionInformationItem(): void
60 {
61 $this->expectExceptionMessage('Statement information holds statement information items.');
62
63 new StatementDiagnostics([new InformationItem(new UserVariable(new Name('m')), ConditionItemName::MessageText)]);
64 }
65}
66