packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Condition/SqlStateTest.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\Rendering\Codec;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Literal\EscapeRule;
17use SqlSemantics\Platform\MySql\Statement\Literal\Radix;
18use SqlSemantics\Platform\MySql\Statement\Literal\Text;
19use SqlSemantics\Platform\MySql\Statement\Routine\Condition\Signal;
20use SqlSemantics\Platform\MySql\Statement\Routine\Condition\SqlState;
21
22#[CoversClass(SqlState::class)]
23#[Medium]
24final class SqlStateTest extends TestCase
25{
26    /**
27     * @return iterable<string, array{string, bool}>
28     */
29    public static function providerValidTellsTheFormOfTheValue(): iterable
30    {
31        yield 'user-defined exception' => ['45000', true];
32        yield 'warning' => ['01000', true];
33        yield 'letters and digits' => ['42S02', true];
34        yield 'not found' => ['02000', true];
35        yield 'success' => ['00000', false];
36        yield 'success class' => ['00123', false];
37        yield 'four characters' => ['4500', false];
38        yield 'six characters' => ['450000', false];
39        yield 'lower-case letter' => ['4500a', false];
40        yield 'punctuation' => ['45-00', false];
41        yield 'empty' => ['', false];
42    }
43
44    #[DataProvider('providerValidTellsTheFormOfTheValue')]
45    public function testValidTellsTheFormOfTheValue(string $value, bool $expected): void
46    {
47        self::assertSame($expected, (new SqlState(new Text($value)))->valid());
48    }
49
50    public function testRenderWritesTheKeywordAndTheQuotedValue(): void
51    {
52        $out = new Output(new Codec(GrammarRelease::MySql847));
53        (new SqlState(new Text('45000')))->render($out);
54
55        self::assertSame("SQLSTATE '45000'", (new Lexical())->join($out->pieces()));
56    }
57
58    /**
59     * @return iterable<string, array{string}>
60     */
61    public static function providerRenderDropsTheWordValue(): iterable
62    {
63        yield 'mysql 5.6' => ['mysql-5.6.51'];
64        yield 'mysql 5.7' => ['mysql-5.7.44'];
65        yield 'mysql 8.4' => ['mysql-8.4.7'];
66        yield 'mysql 9.1' => ['mysql-9.1.0'];
67    }
68
69    #[DataProvider('providerRenderDropsTheWordValue')]
70    public function testRenderDropsTheWordValue(string $release): void
71    {
72        $signal = (new Semantics($release))->analyze("signal sqlstate value '42S02'");
73        $statement = $signal->statement;
74        self::assertInstanceOf(Signal::class, $statement);
75        self::assertInstanceOf(SqlState::class, $statement->condition);
76
77        self::assertSame('42S02', $statement->condition->state->value);
78        self::assertSame("SIGNAL SQLSTATE '42S02'", $signal->toString());
79    }
80
81    public function testRefusesAHexadecimalText(): void
82    {
83        $this->expectExceptionMessage('An SQLSTATE value is written as a quoted string.');
84
85        new SqlState(new Text('4500', EscapeRule::Backslash, Radix::Hexadecimal));
86    }
87
88    public function testRefusesABitText(): void
89    {
90        $this->expectExceptionMessage('An SQLSTATE value is written as a quoted string.');
91
92        new SqlState(new Text('0101', EscapeRule::Backslash, Radix::Bit));
93    }
94}
95