packages/sql-semantics-mysql/tests/Unit/Statement/Literal/ByteSizeTest.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\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
15use SqlSemantics\Platform\MySql\Model\Rendering\Output;
16use SqlSemantics\Platform\MySql\Model\Rendering\PieceKind;
17use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
18use SqlSemantics\Platform\MySql\Platform;
19use SqlSemantics\Platform\MySql\Rendering\Codec;
20use SqlSemantics\Platform\MySql\Statement\Literal\ByteSize;
21use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
22
23#[CoversClass(ByteSize::class)]
24#[Small]
25final class ByteSizeTest extends TestCase
26{
27    public function testRenderWritesTheNumber(): void
28    {
29        $out = new Output(new Codec(GrammarRelease::MySql847));
30        (new ByteSize(new Numeral('1024')))->render($out);
31
32        self::assertSame('1024', (new Lexical())->join($out->pieces()));
33    }
34
35    public function testRenderWritesTheWordAsOneName(): void
36    {
37        $out = new Output(new Codec(GrammarRelease::MySql847));
38        (new ByteSize(null, new Name('16M')))->render($out);
39        $pieces = $out->pieces();
40
41        self::assertCount(1, $pieces);
42        self::assertSame(PieceKind::Name, $pieces[0]->kind);
43        self::assertSame('`16M`', $pieces[0]->text);
44    }
45
46    public function testRenderReadsBackAsTheSameWord(): void
47    {
48        $platform = new Platform();
49        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
50        $parser = $platform->parser($profile);
51        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
52        $size = $lowering->numbers->size($parser->parse("CREATE TABLESPACE ts ADD DATAFILE 'f' INITIAL_SIZE = 16M")->find('size_number')[0]);
53        $out = new Output($platform->codec($profile));
54        $size->render($out);
55        $again = $lowering->numbers->size($parser->parse("CREATE TABLESPACE ts ADD DATAFILE 'f' INITIAL_SIZE = " . (new Lexical())->join($out->pieces()))->find('size_number')[0]);
56
57        self::assertNull($size->number);
58        self::assertSame('16M', $size->word?->value);
59        self::assertSame('16M', $again->word?->value);
60    }
61
62    public function testLoweredHexadecimalSizeKeepsItsDigits(): void
63    {
64        $platform = new Platform();
65        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
66        $node = $platform->parser($profile)->parse("CREATE TABLESPACE ts ADD DATAFILE 'f' INITIAL_SIZE = 0x400")->find('size_number')[0];
67        $size = (new Lowering($platform->productions($profile), new Leaves(), $profile))->numbers->size($node);
68        $out = new Output($platform->codec($profile));
69        $size->render($out);
70
71        self::assertNull($size->word);
72        self::assertSame('400', $size->number?->text);
73        self::assertTrue($size->number->hexadecimal);
74        self::assertSame('0x400', (new Lexical())->join($out->pieces()));
75    }
76
77    public function testRejectsASizeWithNeitherNumberNorWord(): void
78    {
79        $this->expectExceptionMessage('A size is either a number or a word.');
80
81        new ByteSize(null);
82    }
83
84    public function testRejectsASizeWithBothNumberAndWord(): void
85    {
86        $this->expectExceptionMessage('A size is either a number or a word.');
87
88        new ByteSize(new Numeral('16'), new Name('16M'));
89    }
90}
91