packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/Constructor/JsonArrayAggregateTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Json\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\Fact\ScalarFact;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
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\Invocation\Json\Constructor\JsonArrayAggregate;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonNullHandling;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonReturning;
24use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonValueExpression;
25use SqlSemantics\Platform\PostgreSql\Statement\Invocation\WindowSpecification;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
29use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
30use SqlSemantics\Platform\PostgreSql\Statement\Query\SortItem;
31use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
32use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
33use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
34
35#[CoversClass(JsonArrayAggregate::class)]
36#[Small]
37final class JsonArrayAggregateTest extends TestCase
38{
39 public function testOutputNameIsJsonArrayagg(): void
40 {
41 self::assertSame('json_arrayagg', (new JsonArrayAggregate(new JsonValueExpression(new NullLiteral())))->outputName()->value);
42 }
43
44 public function testDeriveScalarDerivesTheOrdering(): void
45 {
46 $key = new Constant(new IntegerConstant('1'));
47 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
48 $fact = $derivation->scalar(new JsonArrayAggregate(new JsonValueExpression(new Constant(new IntegerConstant('1'))), [new SortItem($key)]), $derivation->environment());
49 self::assertTrue($derivation->facts()->covers($key));
50 self::assertEquals(new ScalarFact(new Known(Builtin::Json), Nullability::Nullable), $fact);
51 }
52
53 public function testRenderWritesTheOrderingAndTheWindow(): void
54 {
55 $aggregate = new JsonArrayAggregate(new JsonValueExpression(new Constant(new IntegerConstant('1'))), [new SortItem(new Constant(new IntegerConstant('1')))], JsonNullHandling::Absent, new JsonReturning(new TypeName(new NamedDesignation(new DottedName([new Name('text')])))), null, new WindowSpecification());
56 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
57 $aggregate->render($out);
58 self::assertSame('JSON_ARRAYAGG(1 ORDER BY 1 ABSENT ON NULL RETURNING text) OVER ()', (new Lexical())->join($out->pieces()));
59 }
60}
61