packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Subquery/ArraySubqueryTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Subquery;
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\Fact\QueryFact;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Query;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
22use SqlSemantics\Platform\PostgreSql\Platform;
23use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
24use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\ArraySubquery;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\ArrayOf;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(ArraySubquery::class)]
29#[Small]
30final class ArraySubqueryTest extends TestCase
31{
32 public function testOutputNameIsArray(): void
33 {
34 self::assertSame('array', (new ArraySubquery(self::createStub(Query::class)))->outputName()->value);
35 }
36
37 public function testDeriveScalarIsAnArrayOfTheColumnType(): void
38 {
39 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
40 $query = self::createConfiguredStub(Query::class, ['deriveQuery' => new QueryFact([new Field(0, new OutputSlot(new Name('a'), new Known(Builtin::Int4), Nullability::NotNull))], $context->columnNames)]);
41 $derivation = new Derivation($context);
42 $fact = $derivation->scalar(new ArraySubquery($query), $derivation->environment());
43 self::assertEquals(new Known(new ArrayOf(Builtin::Int4)), $fact->type);
44 self::assertSame(Nullability::NotNull, $fact->nullability);
45 }
46
47 public function testRenderWritesArray(): void
48 {
49 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
50 (new ArraySubquery(self::createStub(Query::class)))->render($out);
51 self::assertSame('ARRAY ()', (new Lexical())->join($out->pieces()));
52 }
53}
54