packages/sql-semantics-postgres/tests/Unit/Rendering/SpellingTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rendering;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\NameUse;
12use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
16use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
17use SqlSemantics\Platform\PostgreSql\Rendering\Spelling;
18
19#[CoversClass(Spelling::class)]
20#[Small]
21final class SpellingTest extends TestCase
22{
23    public function testStringDoublesQuotesAndKeepsBackslashes(): void
24    {
25        self::assertSame("'a''b\\n'", (new Spelling())->string("a'b\\n"));
26    }
27
28    public function testDottedWritesLaterPartsAsLabels(): void
29    {
30        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
31        (new Spelling())->dotted($out, [new Name('between'), new Name('select')]);
32        self::assertSame('"between".select', (new Lexical())->join($out->pieces()));
33    }
34
35    public function testDottedWritesASinglePartForItsPosition(): void
36    {
37        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
38        (new Spelling())->dotted($out, [new Name('left')], NameUse::Routine);
39        self::assertSame('left', (new Lexical())->join($out->pieces()));
40    }
41
42    public function testColumnsWritesEveryPartAsAColumnIdentifier(): void
43    {
44        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
45        (new Spelling())->columns($out, [new Name('a'), new Name('select')]);
46        self::assertSame('a."select"', (new Lexical())->join($out->pieces()));
47    }
48
49    public function testQualifiedWritesCatalogSchemaAndName(): void
50    {
51        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52        (new Spelling())->qualified($out, new QualifiedName(new Name('T'), new Name('s'), new Name('c')));
53        self::assertSame('c.s."T"', (new Lexical())->join($out->pieces()));
54    }
55}
56