packages/sql-semantics-mysql/tests/Unit/Statement/Name/CollationNameTest.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\CollationName;
21
22#[CoversClass(CollationName::class)]
23#[Small]
24final class CollationNameTest extends TestCase
25{
26    public function testRenderWritesTheNameAsWritten(): void
27    {
28        $out = new Output(new Codec(GrammarRelease::MySql847));
29        (new CollationName(new Name('utf8mb4_bin')))->render($out);
30        (new CollationName(new Name('Latin1_General_CS')))->render($out);
31
32        self::assertSame('utf8mb4_bin Latin1_General_CS', (new Lexical())->join($out->pieces()));
33    }
34
35    public function testRenderWritesTheKeywordDefaultForAnAbsentName(): void
36    {
37        $out = new Output(new Codec(GrammarRelease::MySql847));
38        (new CollationName(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 testLoweredCollationReadsAQuotedStringAndTheBinaryKeywordAsNames(): 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        $plain = $lowering->charsets->collation($parser->parse('SET NAMES utf8 COLLATE utf8_bin')->find('opt_collate')[0]);
53        $quoted = $lowering->charsets->collation($parser->parse("SET NAMES utf8 COLLATE 'utf8_bin'")->find('opt_collate')[0]);
54        $binary = $lowering->charsets->collation($parser->parse('SET NAMES utf8 COLLATE binary')->find('opt_collate')[0]);
55        $out = new Output($platform->codec($profile));
56        $binary?->render($out);
57
58        self::assertSame('utf8_bin', $plain?->name?->value);
59        self::assertSame('utf8_bin', $quoted?->name?->value);
60        self::assertSame('binary', $binary?->name?->value);
61        self::assertSame('`binary`', (new Lexical())->join($out->pieces()));
62    }
63
64    public function testLoweredDefaultHasNoName(): void
65    {
66        $platform = new Platform();
67        $profile = $platform->profile('mysql-5.7.44', null, ParameterStyle::Native);
68        $node = $platform->parser($profile)->parse('ALTER DATABASE d COLLATE = DEFAULT')->find('collation_name_or_default')[0];
69        $collation = (new Lowering($platform->productions($profile), new Leaves(), $profile))->charsets->collation($node);
70
71        self::assertNotNull($collation);
72        self::assertNull($collation->name);
73    }
74}
75