packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/Constructor/JsonParseTest.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\Expression\Predicate\JsonItemKind;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\JsonTest;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Constructor\JsonParse;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonUniqueKeys;
24use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonValueExpression;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
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(JsonParse::class)]
31#[Small]
32final class JsonParseTest extends TestCase
33{
34    public function testOutputNameIsJson(): void
35    {
36        self::assertSame('json', (new JsonParse(new JsonValueExpression(new NullLiteral())))->outputName()->value);
37    }
38
39    public function testDeriveScalarFollowsTheValue(): void
40    {
41        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42        $fact = $derivation->scalar(new JsonParse(new JsonValueExpression(new NullLiteral())), $derivation->environment());
43        self::assertEquals(new ScalarFact(new Known(Builtin::Json), Nullability::Nullable), $fact);
44    }
45
46    public function testRenderWritesTheUniqueness(): void
47    {
48        $parse = new JsonParse(new JsonValueExpression(new Constant(new StringConstant('{}'))), new JsonUniqueKeys(true));
49        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
50        $parse->render($out);
51        self::assertSame('JSON(\'{}\' WITH UNIQUE KEYS)', (new Lexical())->join($out->pieces()));
52    }
53
54    public function testRejectsAValueThatWouldTakeTheUniqueness(): void
55    {
56        $this->expectExceptionMessage('A value ending in IS JSON without a uniqueness clause would take the uniqueness clause; group it.');
57        new JsonParse(new JsonValueExpression(new JsonTest(new NullLiteral(), false, JsonItemKind::Json)), new JsonUniqueKeys(true));
58    }
59}
60