packages/sql-semantics-mysql/tests/Unit/Statement/Name/CharsetNameTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Name;
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\Name\CharsetName;
21
22#[CoversClass(CharsetName::class)]
23#[Small]
24final class CharsetNameTest extends TestCase
25{
26    public function testRenderWritesTheNameAsWritten(): void
27    {
28        $out = new Output(new Codec(GrammarRelease::MySql847));
29        (new CharsetName(new Name('utf8mb4')))->render($out);
30        (new CharsetName(new Name('UTF8MB4')))->render($out);
31
32        self::assertSame('utf8mb4 UTF8MB4', (new Lexical())->join($out->pieces()));
33    }
34
35    public function testRenderWritesTheKeywordDefaultForAnAbsentName(): void
36    {
37        $out = new Output(new Codec(GrammarRelease::MySql847));
38        (new CharsetName(null))->render($out);
39        $pieces = $out->pieces();
40
41        self::assertCount(1, $pieces);
42        self::assertSame(PieceKind::Keyword, $pieces[0]->kind);
43        self::assertSame('DEFAULT', $pieces[0]->text);
44    }
45
46    public function testLoweredCharsetReadsTheBinaryKeywordAndAQuotedStringAsNames(): 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        $binary = $lowering->charsets->charset($parser->parse('SELECT CONVERT(a USING binary)')->find('charset_name')[0]);
53        $quoted = $lowering->charsets->charset($parser->parse("SELECT CONVERT(a USING 'latin1')")->find('charset_name')[0]);
54        $out = new Output($platform->codec($profile));
55        $binary->render($out);
56
57        self::assertSame('binary', $binary->name?->value);
58        self::assertSame('latin1', $quoted->name?->value);
59        self::assertSame('`binary`', (new Lexical())->join($out->pieces()));
60    }
61
62    public function testLoweredDefaultHasNoName(): void
63    {
64        $platform = new Platform();
65        $profile = $platform->profile('mysql-5.7.44', null, ParameterStyle::Native);
66        $node = $platform->parser($profile)->parse('ALTER DATABASE d CHARACTER SET = DEFAULT')->find('charset_name_or_default')[0];
67        $charset = (new Lowering($platform->productions($profile), new Leaves(), $profile))->charsets->charset($node);
68
69        self::assertNull($charset->name);
70    }
71}
72