packages/sql-semantics-mysql/tests/Unit/Statement/Literal/TextTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Literal;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Lowering;
11use SqlSemantics\Platform\MySql\Mode;
12use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
13use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
14use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
15use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
16use SqlSemantics\Platform\MySql\Model\Rendering\Output;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Rendering\Codec;
19use SqlSemantics\Platform\MySql\Statement\Literal\EscapeRule;
20use SqlSemantics\Platform\MySql\Statement\Literal\Radix;
21use SqlSemantics\Platform\MySql\Statement\Literal\Text;
22
23#[CoversClass(Text::class)]
24#[Small]
25final class TextTest extends TestCase
26{
27    public function testRenderWritesAQuotedStringUnderTheBackslashRule(): void
28    {
29        $out = new Output(new Codec(GrammarRelease::MySql847));
30        (new Text("it's"))->render($out);
31        (new Text('a\\b'))->render($out);
32
33        self::assertSame("'it''s' 'a\\\\b'", (new Lexical())->join($out->pieces()));
34    }
35
36    public function testRenderKeepsBackslashesUnderTheVerbatimRule(): void
37    {
38        $out = new Output(new Codec(GrammarRelease::MySql847));
39        (new Text('a\\b', EscapeRule::Verbatim))->render($out);
40
41        self::assertSame("'a\\b'", (new Lexical())->join($out->pieces()));
42    }
43
44    public function testRenderSpellsHexadecimalDigitsByTheirCount(): void
45    {
46        $out = new Output(new Codec(GrammarRelease::MySql847));
47        (new Text('4142', EscapeRule::Backslash, Radix::Hexadecimal))->render($out);
48        (new Text('A', EscapeRule::Backslash, Radix::Hexadecimal))->render($out);
49
50        self::assertSame("x'4142' 0xA", (new Lexical())->join($out->pieces()));
51    }
52
53    public function testRenderQuotesBitDigits(): void
54    {
55        $out = new Output(new Codec(GrammarRelease::MySql847));
56        (new Text('101', EscapeRule::Backslash, Radix::Bit))->render($out);
57        (new Text('', EscapeRule::Backslash, Radix::Bit))->render($out);
58
59        self::assertSame("b'101' b''", (new Lexical())->join($out->pieces()));
60    }
61
62    public function testLoweredEnumMembersHoldDecodedBytesOrDigits(): void
63    {
64        $platform = new Platform();
65        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
66        $nodes = $platform->parser($profile)->parse("CREATE TABLE t (c ENUM('a\\nb', X'41', 0b1))")->find('text_string');
67        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
68        $quoted = $lowering->literals->text($nodes[0]);
69        $hexadecimal = $lowering->literals->text($nodes[1]);
70        $bit = $lowering->literals->text($nodes[2]);
71
72        self::assertSame("a\nb", $quoted->value);
73        self::assertNull($quoted->radix);
74        self::assertSame(EscapeRule::Backslash, $quoted->escapes);
75        self::assertSame('41', $hexadecimal->value);
76        self::assertSame(Radix::Hexadecimal, $hexadecimal->radix);
77        self::assertSame('1', $bit->value);
78        self::assertSame(Radix::Bit, $bit->radix);
79    }
80
81    public function testLoweredTextFollowsTheEscapeRuleOfTheProfile(): void
82    {
83        $platform = new Platform();
84        $profile = $platform->profile('mysql-8.4.7', Mode::fromString('NO_BACKSLASH_ESCAPES'), ParameterStyle::Native);
85        $node = $platform->parser($profile)->parse("CREATE TABLE t (c ENUM('a\\nb'))")->find('text_string')[0];
86        $text = (new Lowering($platform->productions($profile), new Leaves(), $profile))->literals->text($node);
87        $out = new Output($platform->codec($profile));
88        $text->render($out);
89
90        self::assertSame('a\\nb', $text->value);
91        self::assertSame(EscapeRule::Verbatim, $text->escapes);
92        self::assertSame("'a\\nb'", (new Lexical())->join($out->pieces()));
93    }
94
95    public function testRejectsNonHexadecimalDigits(): void
96    {
97        $this->expectExceptionMessage('A hexadecimal text holds hexadecimal digits.');
98
99        new Text('G', EscapeRule::Backslash, Radix::Hexadecimal);
100    }
101
102    public function testRejectsNonBinaryDigits(): void
103    {
104        $this->expectExceptionMessage('A bit text holds binary digits.');
105
106        new Text('2', EscapeRule::Backslash, Radix::Bit);
107    }
108}
109