packages/sql-semantics-postgres/tests/Unit/Statement/Routine/ResultTableTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine;
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;
19use SqlSemantics\Platform\PostgreSql\Statement\Routine\ResultColumn;
20use SqlSemantics\Platform\PostgreSql\Statement\Routine\ResultTable;
21use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
23
24#[CoversClass(ResultTable::class)]
25#[Small]
26final class ResultTableTest extends TestCase
27{
28 public function testDeriveClauseDerivesEveryColumn(): void
29 {
30 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
31 (new ResultTable([new ResultColumn(new Name('a'), new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))))]))->deriveClause($derivation, $derivation->environment());
32 self::assertSame([], $derivation->facts()->diagnostics);
33 }
34
35 public function testRenderWritesTableAndColumns(): void
36 {
37 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
38 (new ResultTable([new ResultColumn(new Name('a'), new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))), new ResultColumn(new Name('b'), new TypeName(new NamedDesignation(new DottedName([new Name('text')]))))]))->render($out);
39 self::assertSame('TABLE (a int4, b text)', (new Lexical())->join($out->pieces()));
40 }
41
42 public function testRejectsAnEmptyTable(): void
43 {
44 $this->expectExceptionMessage('A result table has at least one column.');
45 new ResultTable([]);
46 }
47}
48