packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Constructor/RowConstructorTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Constructor;
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\Shape\OutputSlot;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\Composite;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\RowConstructor;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\Constructor\RowSpelling;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
29
30#[CoversClass(RowConstructor::class)]
31#[Small]
32final class RowConstructorTest extends TestCase
33{
34 public function testOutputNameIsRow(): void
35 {
36 self::assertSame('row', (new RowConstructor([]))->outputName()->value);
37 }
38
39 public function testDeriveScalarIsARecordOfTheFields(): void
40 {
41 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42 $fact = $derivation->scalar(new RowConstructor([new Constant(new IntegerConstant('1')), new Constant(new StringConstant('a'))]), $derivation->environment());
43 self::assertEquals(new Known(new Composite([new OutputSlot(new Name('f1'), new Known(Builtin::Int4), Nullability::NotNull), new OutputSlot(new Name('f2'), new Known(Builtin::Text), Nullability::NotNull)])), $fact->type);
44 self::assertSame(Nullability::NotNull, $fact->nullability);
45 }
46
47 public function testRenderWritesBothSpellings(): void
48 {
49 $explicit = new Output(new Codec(GrammarRelease::PostgreSql172));
50 (new RowConstructor([new NullLiteral()]))->render($explicit);
51 $implicit = new Output(new Codec(GrammarRelease::PostgreSql172));
52 (new RowConstructor([new NullLiteral(), new NullLiteral()], RowSpelling::Implicit))->render($implicit);
53 self::assertSame(['ROW (NULL)', '(NULL, NULL)'], [(new Lexical())->join($explicit->pieces()), (new Lexical())->join($implicit->pieces())]);
54 }
55
56 public function testRejectsAnImplicitRowOfOneField(): void
57 {
58 $this->expectExceptionMessage('A row without ROW has at least two fields.');
59 new RowConstructor([new NullLiteral()], RowSpelling::Implicit);
60 }
61}
62