packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/Constructor/JsonArrayConstructorTest.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\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Constructor\JsonArrayConstructor;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonNullHandling;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonValueExpression;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(JsonArrayConstructor::class)]
29#[Small]
30final class JsonArrayConstructorTest extends TestCase
31{
32    public function testRejectsANullClauseWithoutValues(): void
33    {
34        $this->expectExceptionMessage('JSON_ARRAY without values takes no NULL clause.');
35        new JsonArrayConstructor([], JsonNullHandling::Absent);
36    }
37
38    public function testOutputNameIsJsonArray(): void
39    {
40        self::assertSame('json_array', (new JsonArrayConstructor([]))->outputName()->value);
41    }
42
43    public function testDeriveScalarIsJson(): void
44    {
45        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
46        $fact = $derivation->scalar(new JsonArrayConstructor([new JsonValueExpression(new Constant(new IntegerConstant('1')))]), $derivation->environment());
47        self::assertEquals(new ScalarFact(new Known(Builtin::Json), Nullability::NotNull), $fact);
48    }
49
50    public function testRenderWritesTheNullClause(): void
51    {
52        $array = new JsonArrayConstructor([new JsonValueExpression(new Constant(new IntegerConstant('1'))), new JsonValueExpression(new NullLiteral())], JsonNullHandling::Keep);
53        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
54        $array->render($out);
55        self::assertSame('JSON_ARRAY(1, NULL NULL ON NULL)', (new Lexical())->join($out->pieces()));
56    }
57}
58