packages/sql-semantics-mysql/tests/Unit/Statement/Name/TableWildcardTest.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\Statement\Identifier\Name;
17use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
18use SqlSemantics\Platform\MySql\Platform;
19use SqlSemantics\Platform\MySql\Rendering\Codec;
20use SqlSemantics\Platform\MySql\Statement\Name\TableWildcard;
21
22#[CoversClass(TableWildcard::class)]
23#[Small]
24final class TableWildcardTest extends TestCase
25{
26    public function testRenderWritesTheTableQualifierAndTheStar(): void
27    {
28        $out = new Output(new Codec(GrammarRelease::MySql847));
29        (new TableWildcard(new QualifiedName(new Name('t'))))->render($out);
30
31        self::assertSame('t.*', (new Lexical())->join($out->pieces()));
32    }
33
34    public function testRenderWritesTheDatabaseQualifierAndQuotesWhatNeedsIt(): void
35    {
36        $out = new Output(new Codec(GrammarRelease::MySql847));
37        (new TableWildcard(new QualifiedName(new Name('t'), new Name('my db'))))->render($out);
38
39        self::assertSame('`my db`.t.*', (new Lexical())->join($out->pieces()));
40    }
41
42    public function testLoweredWildcardKeepsTheQualifiersAsWritten(): void
43    {
44        $platform = new Platform();
45        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
46        $parser = $platform->parser($profile);
47        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
48        $qualified = $lowering->names->wildcard($parser->parse('SELECT db.T.* FROM db.t')->find('table_wild')[0]);
49        $plain = $lowering->names->wildcard($parser->parse('SELECT t.* FROM t')->find('table_wild')[0]);
50
51        self::assertSame('T', $qualified->table->name->value);
52        self::assertSame('db', $qualified->table->schema?->value);
53        self::assertSame('t', $plain->table->name->value);
54        self::assertNull($plain->table->schema);
55    }
56
57    public function testRejectsACatalogQualifier(): void
58    {
59        $this->expectExceptionMessage('A table is qualified by at most a database.');
60
61        new TableWildcard(new QualifiedName(new Name('t'), new Name('db'), new Name('catalog')));
62    }
63}
64