packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Condition/ErrorCodeTest.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\DataProvider;
9use PHPUnit\Framework\Attributes\Medium;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
15use SqlSemantics\Platform\MySql\Rendering\Codec;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
18use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ConditionName;
19use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ErrorCode;
20use SqlSemantics\Platform\MySql\Statement\Routine\CreateProcedure;
21use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
22use SqlSemantics\Platform\MySql\Statement\Routine\Program\ConditionDeclaration;
23use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerDeclaration;
24
25#[CoversClass(ErrorCode::class)]
26#[Medium]
27final class ErrorCodeTest extends TestCase
28{
29    public function testRenderWritesTheCode(): void
30    {
31        $out = new Output(new Codec(GrammarRelease::MySql847));
32        (new ErrorCode(new Numeral('1051')))->render($out);
33
34        self::assertSame('1051', (new Lexical())->join($out->pieces()));
35    }
36
37    public function testRenderWritesTheCodesOfADeclarationAndAHandler(): void
38    {
39        $create = (new Semantics('mysql-5.7.44'))->inspect('CREATE PROCEDURE p() BEGIN DECLARE c CONDITION FOR 1051; DECLARE CONTINUE HANDLER FOR c, 1052 BEGIN END; END');
40        $statement = $create->statement;
41        self::assertInstanceOf(CreateProcedure::class, $statement);
42        $body = $statement->body;
43        self::assertInstanceOf(Block::class, $body);
44        $declaration = $body->declarations[0];
45        self::assertInstanceOf(ConditionDeclaration::class, $declaration);
46        self::assertInstanceOf(ErrorCode::class, $declaration->value);
47        $handler = $body->declarations[1];
48        self::assertInstanceOf(HandlerDeclaration::class, $handler);
49        self::assertInstanceOf(ConditionName::class, $handler->conditions[0]);
50        self::assertInstanceOf(ErrorCode::class, $handler->conditions[1]);
51
52        self::assertSame('1051', $declaration->value->code->text);
53        self::assertSame('1052', $handler->conditions[1]->code->text);
54        self::assertSame([], $create->facts->diagnostics);
55        self::assertSame('CREATE PROCEDURE p() BEGIN DECLARE c CONDITION FOR 1051; DECLARE CONTINUE HANDLER FOR c, 1052 BEGIN END; END', $create->toString());
56    }
57
58    #[DataProvider('providerValidTellsWhetherTheCodeIsNotZero')]
59    public function testValidTellsWhetherTheCodeIsNotZero(string $text, bool $hexadecimal, bool $expected): void
60    {
61        self::assertSame($expected, (new ErrorCode(new Numeral($text, $hexadecimal)))->valid());
62    }
63
64    /**
65     * @return iterable<string, array{string, bool, bool}>
66     */
67    public static function providerValidTellsWhetherTheCodeIsNotZero(): iterable
68    {
69        yield 'a code' => ['1051', false, true];
70        yield 'zero' => ['0', false, false];
71        yield 'zeros' => ['000', false, false];
72        yield 'a decimal below one' => ['0.9', false, false];
73        yield 'a decimal without integer part' => ['.5', false, false];
74        yield 'a floating number' => ['1e2', false, true];
75        yield 'a hexadecimal code' => ['0A', true, true];
76        yield 'a hexadecimal zero' => ['00', true, false];
77        yield 'an empty hexadecimal literal' => ['', true, false];
78    }
79
80    #[DataProvider('providerNumberAnswersTheErrorNumberTheServerKeeps')]
81    public function testNumberAnswersTheErrorNumberTheServerKeeps(string $text, bool $hexadecimal, int $expected): void
82    {
83        self::assertSame($expected, (new ErrorCode(new Numeral($text, $hexadecimal)))->number());
84    }
85
86    /**
87     * @return iterable<string, array{string, bool, int}>
88     */
89    public static function providerNumberAnswersTheErrorNumberTheServerKeeps(): iterable
90    {
91        yield 'a code' => ['1051', false, 1051];
92        yield 'leading zeros' => ['001051', false, 1051];
93        yield 'a floating number' => ['1e2', false, 1];
94        yield 'a decimal' => ['1051.9', false, 1051];
95        yield 'a hexadecimal literal' => ['41b', true, 1051];
96        yield 'beyond 32 bits' => ['4294967297', false, 1];
97        yield 'beyond 64 bits' => ['99999999999999999999999', false, 4294967295];
98        yield 'the largest 64-bit value' => ['18446744073709551615', false, 4294967295];
99        yield 'a hexadecimal literal beyond 32 bits' => ['100000001', true, 1];
100        yield 'a hexadecimal literal beyond the signed 64-bit range' => ['8000000000000001', true, 4294967295];
101    }
102
103    #[DataProvider('providerDigitsAnswersTheDigitsTheGrammarConverts')]
104    public function testDigitsAnswersTheDigitsTheGrammarConverts(string $text, bool $hexadecimal, string $expected): void
105    {
106        self::assertSame($expected, (new ErrorCode(new Numeral($text, $hexadecimal)))->digits());
107    }
108
109    /**
110     * @return iterable<string, array{string, bool, string}>
111     */
112    public static function providerDigitsAnswersTheDigitsTheGrammarConverts(): iterable
113    {
114        yield 'an integer' => ['1051', false, '1051'];
115        yield 'a floating number' => ['12e3', false, '12'];
116        yield 'a decimal' => ['7.5', false, '7'];
117        yield 'a decimal without integer part' => ['.5', false, ''];
118        yield 'a hexadecimal literal' => ['0aF', true, '0aF'];
119    }
120
121    public function testValidLetsTheProgramReportTheCodeZero(): void
122    {
123        $create = (new Semantics('mysql-5.6.51'))->inspect('CREATE PROCEDURE p() BEGIN DECLARE c CONDITION FOR 0.5; END');
124
125        self::assertSame(["Incorrect CONDITION value: '0'"], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $create->facts->diagnostics));
126        self::assertSame('CREATE PROCEDURE p() BEGIN DECLARE c CONDITION FOR 0.5; END', $create->toString());
127    }
128}
129