packages/sql-semantics-postgres/tests/Unit/Statement/Object/Reference/MemberNameTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Object\Reference;
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\Model\Statement\Identifier\QualifiedName;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
19use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
20use SqlSemantics\Platform\PostgreSql\Statement\Object\Reference\MemberName;
21
22#[CoversClass(MemberName::class)]
23#[Small]
24final class MemberNameTest extends TestCase
25{
26 public function testTableAnswersTheOwningRelation(): void
27 {
28 $dotted = new MemberName(new Name('r'), new DottedName([new Name('s'), new Name('t')]));
29 $domain = new MemberName(new Name('c'), new DottedName([new Name('d')]), true);
30 $long = new MemberName(new Name('r'), new DottedName([new Name('a'), new Name('b'), new Name('c'), new Name('d')]));
31 self::assertSame(['s', null, null], [$dotted->table()?->schema?->value, $domain->table(), $long->table()]);
32 }
33
34 public function testDeriveClauseRecordsNothing(): void
35 {
36 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
37 (new MemberName(new Name('r'), new QualifiedName(new Name('t'))))->deriveClause($derivation, $derivation->environment());
38 self::assertSame([], $derivation->facts()->diagnostics);
39 }
40
41 public function testRenderWritesTheOwner(): void
42 {
43 $relation = new Output(new Codec(GrammarRelease::PostgreSql172));
44 (new MemberName(new Name('r'), new QualifiedName(new Name('t'), new Name('s'))))->render($relation);
45 $domain = new Output(new Codec(GrammarRelease::PostgreSql172));
46 (new MemberName(new Name('c'), new DottedName([new Name('d')]), true))->render($domain);
47 self::assertSame(['r ON s.t', 'c ON DOMAIN d'], [(new Lexical())->join($relation->pieces()), (new Lexical())->join($domain->pieces())]);
48 }
49}
50