packages/sql-semantics-postgres/tests/Unit/Statement/Name/DottedNameTest.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\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
19
20#[CoversClass(DottedName::class)]
21#[Small]
22final class DottedNameTest extends TestCase
23{
24 public function testLastAnswersTheObjectName(): void
25 {
26 self::assertSame('c', (new DottedName([new Name('a'), new Name('b'), new Name('c')]))->last()->value);
27 }
28
29 public function testQualifiedReadsUpToThreeParts(): void
30 {
31 $name = (new DottedName([new Name('c'), new Name('s'), new Name('t')]))->qualified();
32 self::assertSame(['c', 's', 't'], [$name?->catalog?->value, $name?->schema?->value, $name?->name->value]);
33 self::assertNull((new DottedName([new Name('t')]))->qualified()?->schema);
34 self::assertNull((new DottedName([new Name('a'), new Name('b'), new Name('c'), new Name('d')]))->qualified());
35 }
36
37 public function testDeriveClauseRecordsNothing(): void
38 {
39 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
40 (new DottedName([new Name('t')]))->deriveClause($derivation, $derivation->environment());
41 self::assertSame([], $derivation->facts()->diagnostics);
42 }
43
44 public function testRenderQuotesOnlyThePartsThatNeedIt(): void
45 {
46 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
47 (new DottedName([new Name('App'), new Name('select'), new Name('x y')]))->render($out);
48 self::assertSame('"App".select."x y"', (new Lexical())->join($out->pieces()));
49 }
50
51 public function testRejectsANameWithoutParts(): void
52 {
53 $this->expectExceptionMessage('A dotted name has at least one part.');
54 new DottedName([]);
55 }
56}
57