packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/Constructor/JsonObjectConstructorTest.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\Expression\Predicate\JsonItemKind;
22use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\JsonTest;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Constructor\JsonObjectConstructor;
24use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonNullHandling;
25use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonPair;
26use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonReturning;
27use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonUniqueKeys;
28use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonValueExpression;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
30use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
31use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
32use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
33use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
34use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
35use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
36use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
37
38#[CoversClass(JsonObjectConstructor::class)]
39#[Small]
40final class JsonObjectConstructorTest extends TestCase
41{
42    public function testRejectsANullClauseWithoutPairs(): void
43    {
44        $this->expectExceptionMessage('JSON_OBJECT without pairs takes no NULL or uniqueness clause.');
45        new JsonObjectConstructor([], JsonNullHandling::Keep);
46    }
47
48    public function testOutputNameIsJsonObject(): void
49    {
50        self::assertSame('json_object', (new JsonObjectConstructor([]))->outputName()->value);
51    }
52
53    public function testDeriveScalarIsJsonOrTheReturningType(): void
54    {
55        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
56        $plain = $derivation->scalar(new JsonObjectConstructor([new JsonPair(new Constant(new StringConstant('a')), new JsonValueExpression(new Constant(new IntegerConstant('1'))))]), $derivation->environment());
57        $returning = $derivation->scalar(new JsonObjectConstructor([], null, null, new JsonReturning(new TypeName(new NamedDesignation(new DottedName([new Name('text')]))))), $derivation->environment());
58        self::assertEquals([new ScalarFact(new Known(Builtin::Json), Nullability::NotNull), new ScalarFact(new Known(Builtin::Text), Nullability::NotNull)], [$plain, $returning]);
59    }
60
61    public function testRenderWritesTheClauses(): void
62    {
63        $object = new JsonObjectConstructor([new JsonPair(new Constant(new StringConstant('a')), new JsonValueExpression(new Constant(new IntegerConstant('1'))))], JsonNullHandling::Absent, new JsonUniqueKeys(true), new JsonReturning(new TypeName(new NamedDesignation(new DottedName([new Name('text')])))));
64        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
65        $object->render($out);
66        self::assertSame('JSON_OBJECT(\'a\' : 1 ABSENT ON NULL WITH UNIQUE KEYS RETURNING text)', (new Lexical())->join($out->pieces()));
67    }
68
69    public function testRejectsALastValueThatWouldTakeTheUniqueness(): void
70    {
71        $this->expectExceptionMessage('A last value ending in IS JSON without a uniqueness clause would take the uniqueness clause; group it.');
72        new JsonObjectConstructor([new JsonPair(new NullLiteral(), new JsonValueExpression(new JsonTest(new NullLiteral(), false, JsonItemKind::Json)))], null, new JsonUniqueKeys(false));
73    }
74}
75