packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/Constructor/JsonSerializeTest.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\JsonSerialize;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonReturning;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonValueExpression;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
27use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
28use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
29use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
30use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
31
32#[CoversClass(JsonSerialize::class)]
33#[Small]
34final class JsonSerializeTest extends TestCase
35{
36    public function testOutputNameIsJsonSerialize(): void
37    {
38        self::assertSame('json_serialize', (new JsonSerialize(new JsonValueExpression(new NullLiteral())))->outputName()->value);
39    }
40
41    public function testDeriveScalarIsTextWithoutReturning(): void
42    {
43        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
44        $fact = $derivation->scalar(new JsonSerialize(new JsonValueExpression(new Constant(new StringConstant('{}')))), $derivation->environment());
45        self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::NotNull), $fact);
46    }
47
48    public function testRenderWritesTheReturningClause(): void
49    {
50        $serialize = new JsonSerialize(new JsonValueExpression(new Constant(new StringConstant('{}'))), new JsonReturning(new TypeName(new NamedDesignation(new DottedName([new Name('text')])))));
51        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52        $serialize->render($out);
53        self::assertSame('JSON_SERIALIZE(\'{}\' RETURNING text)', (new Lexical())->join($out->pieces()));
54    }
55}
56