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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Literal;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
12use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
13use SqlSemantics\Platform\MySql\Semantics;
14use SqlSemantics\Platform\MySql\Statement\Expression\Comparison;
15use SqlSemantics\Platform\MySql\Statement\Literal\Radix;
16use SqlSemantics\Platform\MySql\Statement\Literal\RadixLiteral;
17use SqlSemantics\Platform\MySql\Statement\Query\Select;
18use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
19use SqlSemantics\Platform\MySql\Statement\Type\Binary;
20use SqlSemantics\Platform\MySql\Statement\Type\Character;
21use SqlSemantics\Platform\MySql\Statement\Type\Kind\BinaryKind;
22use SqlSemantics\Platform\MySql\Statement\Type\Kind\CharacterKind;
23use SqlSemantics\Platform\MySql\Statement\Type\Kind\CharsetForm;
24
25#[CoversClass(RadixLiteral::class)]
26#[Medium]
27final class RadixLiteralTest extends TestCase
28{
29    public function testDeriveScalarAnswersVarBinaryWithoutAnIntroducer(): void
30    {
31        $operation = (new Semantics())->analyze("SELECT a FROM t WHERE a = X'1F'");
32        $select = $operation->statement;
33        self::assertInstanceOf(Select::class, $select);
34        self::assertInstanceOf(Comparison::class, $select->where);
35        $literal = $select->where->right;
36        self::assertInstanceOf(RadixLiteral::class, $literal);
37        $fact = $operation->facts->scalar($literal);
38
39        self::assertSame(Radix::Hexadecimal, $literal->radix);
40        self::assertSame('1F', $literal->digits);
41        self::assertNull($literal->introducer);
42        self::assertInstanceOf(Known::class, $fact->type);
43        self::assertInstanceOf(Binary::class, $fact->type->descriptor);
44        self::assertSame(BinaryKind::VarBinary, $fact->type->descriptor->kind);
45        self::assertNull($fact->type->descriptor->length);
46        self::assertSame(Nullability::NotNull, $fact->nullability);
47    }
48
49    public function testDeriveScalarAnswersVarCharOfTheIntroducedCharacterSet(): void
50    {
51        $operation = (new Semantics())->analyze("SELECT _LATIN1 b'1'");
52        $select = $operation->statement;
53        self::assertInstanceOf(Select::class, $select);
54        $item = $select->items[0];
55        self::assertInstanceOf(SelectExpression::class, $item);
56        $literal = $item->expression;
57        self::assertInstanceOf(RadixLiteral::class, $literal);
58        $fact = $operation->facts->scalar($literal);
59
60        self::assertSame(Radix::Bit, $literal->radix);
61        self::assertSame('1', $literal->digits);
62        self::assertSame('latin1', $literal->introducer?->value);
63        self::assertInstanceOf(Known::class, $fact->type);
64        self::assertInstanceOf(Character::class, $fact->type->descriptor);
65        self::assertSame(CharacterKind::VarChar, $fact->type->descriptor->kind);
66        self::assertFalse($fact->type->descriptor->national);
67        self::assertSame(CharsetForm::Named, $fact->type->descriptor->charset?->form);
68        self::assertSame('latin1', $fact->type->descriptor->charset->charset?->value);
69        self::assertSame(Nullability::NotNull, $fact->nullability);
70    }
71
72    public function testRenderQuotesAnEvenCountOfHexadecimalDigits(): void
73    {
74        $semantics = new Semantics();
75
76        self::assertSame("SELECT x'1F' AS v", $semantics->analyze('SELECT 0x1F AS v')->toString());
77        self::assertSame("SELECT x'1F' AS v", $semantics->analyze("SELECT X'1F' AS v")->toString());
78        self::assertSame("SELECT x'' AS v", $semantics->analyze("SELECT x'' AS v")->toString());
79        self::assertSame('SELECT 0x1F', $semantics->analyze('SELECT 0x1F')->toString());
80    }
81
82    public function testRenderPrefixesAnOddCountOfHexadecimalDigits(): void
83    {
84        $operation = (new Semantics())->analyze('SELECT 0x1');
85        $select = $operation->statement;
86        self::assertInstanceOf(Select::class, $select);
87        $item = $select->items[0];
88        self::assertInstanceOf(SelectExpression::class, $item);
89        $literal = $item->expression;
90        self::assertInstanceOf(RadixLiteral::class, $literal);
91
92        self::assertSame('1', $literal->digits);
93        self::assertSame('SELECT 0x1', $operation->toString());
94    }
95
96    public function testRenderQuotesBitDigitsOfEitherSpelling(): void
97    {
98        $operation = (new Semantics())->analyze('SELECT 0b01 AS v');
99        $select = $operation->statement;
100        self::assertInstanceOf(Select::class, $select);
101        $item = $select->items[0];
102        self::assertInstanceOf(SelectExpression::class, $item);
103        $literal = $item->expression;
104        self::assertInstanceOf(RadixLiteral::class, $literal);
105
106        self::assertSame('01', $literal->digits);
107        self::assertSame("SELECT b'01' AS v", $operation->toString());
108        self::assertSame("SELECT b'101' AS v", (new Semantics())->analyze("SELECT B'101' AS v")->toString());
109    }
110
111    public function testRenderWritesTheIntroducerInLowerCase(): void
112    {
113        self::assertSame("SELECT _latin1 x'1F' AS v", (new Semantics())->analyze('SELECT _Latin1 0x1F AS v')->toString());
114    }
115
116    public function testRejectsDigitsOutsideTheHexadecimalSystem(): void
117    {
118        $this->expectExceptionMessage('The digits belong to the digit system of the literal.');
119
120        new RadixLiteral(Radix::Hexadecimal, '1G');
121    }
122
123    public function testRejectsDigitsOutsideTheBitSystem(): void
124    {
125        $this->expectExceptionMessage('The digits belong to the digit system of the literal.');
126
127        new RadixLiteral(Radix::Bit, '102');
128    }
129
130    public function testRejectsAnUnknownIntroducer(): void
131    {
132        $this->expectExceptionMessage('An introducer names a character set of the server in lower case.');
133
134        new RadixLiteral(Radix::Hexadecimal, '1F', new Name('nope'));
135    }
136
137    public function testRejectsAnUpperCaseIntroducer(): void
138    {
139        $this->expectExceptionMessage('An introducer names a character set of the server in lower case.');
140
141        new RadixLiteral(Radix::Hexadecimal, '1F', new Name('UTF8MB4'));
142    }
143}
144