packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Condition/Diagnostics/GetDiagnosticsTest.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\DataProvider;
9use PHPUnit\Framework\Attributes\Medium;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
12use SqlSemantics\Platform\MySql\Semantics;
13use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\ConditionDiagnostics;
14use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\DiagnosticsArea;
15use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\GetDiagnostics;
16use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Diagnostics\StatementDiagnostics;
17use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
18use SqlSemantics\Platform\MySql\Statement\Routine\Problem\ProgramProblem;
19use SqlSemantics\Platform\MySql\Statement\Routine\Problem\ProgramRule;
20use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
21use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerDeclaration;
22
23#[CoversClass(GetDiagnostics::class)]
24#[Medium]
25final class GetDiagnosticsTest extends TestCase
26{
27    public function testDeriveStatementReadsStatementInformationIntoUserVariables(): void
28    {
29        $get = (new Semantics())->inspect('GET DIAGNOSTICS @n = NUMBER, @r = ROW_COUNT');
30        $statement = $get->statement;
31        self::assertInstanceOf(GetDiagnostics::class, $statement);
32
33        self::assertNull($statement->area);
34        self::assertInstanceOf(StatementDiagnostics::class, $statement->information);
35        self::assertCount(2, $statement->information->items);
36        self::assertSame([], $get->facts->diagnostics);
37    }
38
39    public function testDeriveStatementReadsConditionInformationOfAVariableNumber(): void
40    {
41        $get = (new Semantics())->inspect('GET CURRENT DIAGNOSTICS CONDITION @i @m = MESSAGE_TEXT, @e = MYSQL_ERRNO, @s = RETURNED_SQLSTATE');
42        $statement = $get->statement;
43        self::assertInstanceOf(GetDiagnostics::class, $statement);
44
45        self::assertSame(DiagnosticsArea::Current, $statement->area);
46        self::assertInstanceOf(ConditionDiagnostics::class, $statement->information);
47        self::assertCount(3, $statement->information->items);
48        self::assertSame([], $get->facts->diagnostics);
49    }
50
51    public function testDeriveStatementReportsANameTargetOutsideAProgram(): void
52    {
53        $get = (new Semantics())->inspect('GET DIAGNOSTICS n = NUMBER');
54        $problem = $get->facts->diagnostics[0] ?? null;
55        self::assertInstanceOf(ProgramProblem::class, $problem);
56
57        self::assertCount(1, $get->facts->diagnostics);
58        self::assertSame(ProgramRule::UndeclaredVariable, $problem->rule);
59        self::assertSame('Undeclared variable: n', $problem->message());
60    }
61
62    public function testDeriveStatementDerivesTheConditionNumber(): void
63    {
64        $get = (new Semantics())->inspect('GET DIAGNOSTICS CONDITION k @m = MESSAGE_TEXT');
65
66        self::assertSame(['Column k does not exist.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $get->facts->diagnostics));
67    }
68
69    public function testDeriveStatementRefusesTheStackedAreaBeforeMySql57(): void
70    {
71        $this->expectExceptionMessage("Unexpected 'STACKED' at line 1, column 5, expected DIAGNOSTICS_SYM");
72
73        (new Semantics('mysql-5.6.51'))->inspect('GET STACKED DIAGNOSTICS CONDITION 1 @m = MESSAGE_TEXT');
74    }
75
76    public function testDeriveProgramResolvesParametersAndLocalVariables(): void
77    {
78        $create = (new Semantics())->inspect('CREATE PROCEDURE p(i INT) BEGIN DECLARE m TEXT; DECLARE EXIT HANDLER FOR SQLEXCEPTION GET STACKED DIAGNOSTICS CONDITION i m = MESSAGE_TEXT, @e = MYSQL_ERRNO; END');
79        $statement = $create->statement;
80        self::assertInstanceOf(CreateProcedure::class, $statement);
81        $body = $statement->body;
82        self::assertInstanceOf(Block::class, $body);
83        $handler = $body->declarations[1];
84        self::assertInstanceOf(HandlerDeclaration::class, $handler);
85        $get = $handler->statement;
86        self::assertInstanceOf(GetDiagnostics::class, $get);
87
88        self::assertSame(DiagnosticsArea::Stacked, $get->area);
89        self::assertInstanceOf(ConditionDiagnostics::class, $get->information);
90        self::assertSame([], $create->facts->diagnostics);
91    }
92
93    /**
94     * @return iterable<string, array{string, list<string>}>
95     */
96    public static function providerDeriveProgramReportsAnUndeclaredTarget(): iterable
97    {
98        yield 'statement information' => [
99            'CREATE PROCEDURE p() BEGIN DECLARE n INT; GET DIAGNOSTICS n = NUMBER, x = ROW_COUNT; END',
100            ['Undeclared variable: x'],
101        ];
102        yield 'condition information' => [
103            'CREATE PROCEDURE p() BEGIN DECLARE m TEXT; GET DIAGNOSTICS CONDITION 1 m = MESSAGE_TEXT, e = MYSQL_ERRNO; END',
104            ['Undeclared variable: e'],
105        ];
106        yield 'variable of a closed block' => [
107            'CREATE PROCEDURE p() BEGIN BEGIN DECLARE n INT; END; GET DIAGNOSTICS n = NUMBER; END',
108            ['Undeclared variable: n'],
109        ];
110    }
111
112    /**
113     * @param list<string> $messages
114     */
115    #[DataProvider('providerDeriveProgramReportsAnUndeclaredTarget')]
116    public function testDeriveProgramReportsAnUndeclaredTarget(string $sql, array $messages): void
117    {
118        $create = (new Semantics())->inspect($sql);
119
120        self::assertSame($messages, array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $create->facts->diagnostics));
121    }
122
123    /**
124     * @return iterable<string, array{string, string, string}>
125     */
126    public static function providerRenderWritesTheStatement(): iterable
127    {
128        yield 'mysql 5.6' => ['mysql-5.6.51', 'get current diagnostics @n = number', 'GET CURRENT DIAGNOSTICS @n = NUMBER'];
129        yield 'mysql 5.7' => ['mysql-5.7.44', 'get stacked diagnostics condition 1 @m = message_text', 'GET STACKED DIAGNOSTICS CONDITION 1 @m = MESSAGE_TEXT'];
130        yield 'mysql 8.0' => ['mysql-8.0.44', 'GET DIAGNOSTICS @n = NUMBER, @r = ROW_COUNT', 'GET DIAGNOSTICS @n = NUMBER, @r = ROW_COUNT'];
131        yield 'mysql 8.4' => ['mysql-8.4.7', 'GET DIAGNOSTICS CONDITION @c @t = TABLE_NAME, @s = RETURNED_SQLSTATE', 'GET DIAGNOSTICS CONDITION @c @t = TABLE_NAME, @s = RETURNED_SQLSTATE'];
132        yield 'mysql 9.1' => ['mysql-9.1.0', 'GET STACKED DIAGNOSTICS CONDITION 2 @o = CLASS_ORIGIN', 'GET STACKED DIAGNOSTICS CONDITION 2 @o = CLASS_ORIGIN'];
133    }
134
135    #[DataProvider('providerRenderWritesTheStatement')]
136    public function testRenderWritesTheStatement(string $release, string $sql, string $expected): void
137    {
138        $get = (new Semantics($release))->inspect($sql);
139
140        self::assertInstanceOf(GetDiagnostics::class, $get->statement);
141        self::assertSame($expected, $get->toString());
142    }
143}
144