packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/Query/JsonArgumentTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Json\Query;
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\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonValueExpression;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonArgument;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
22
23#[CoversClass(JsonArgument::class)]
24#[Small]
25final class JsonArgumentTest extends TestCase
26{
27    public function testDeriveClauseDerivesTheValue(): void
28    {
29        $value = new Constant(new IntegerConstant('1'));
30        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
31        (new JsonArgument(new JsonValueExpression($value), new Name('x')))->deriveClause($derivation, $derivation->environment());
32        self::assertTrue($derivation->facts()->covers($value));
33    }
34
35    public function testRenderWritesTheName(): void
36    {
37        $argument = new JsonArgument(new JsonValueExpression(new Constant(new IntegerConstant('1'))), new Name('x'));
38        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
39        $argument->render($out);
40        self::assertSame('1 AS x', (new Lexical())->join($out->pieces()));
41    }
42}
43