packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/JsonValueExpressionTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Json;
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\JsonFormat;
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\StringConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(JsonValueExpression::class)]
29#[Small]
30final class JsonValueExpressionTest extends TestCase
31{
32 public function testDeriveValueAnswersTheFactsOfTheValue(): void
33 {
34 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
35 $fact = (new JsonValueExpression(new Constant(new IntegerConstant('1'))))->deriveValue($derivation, $derivation->environment());
36 self::assertEquals(new ScalarFact(new Known(Builtin::Int4), Nullability::NotNull), $fact);
37 }
38
39 public function testDeriveValueReportsAnEncodingOnText(): void
40 {
41 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42 (new JsonValueExpression(new Constant(new StringConstant('x')), new JsonFormat(new Name('utf8'))))->deriveValue($derivation, $derivation->environment(), true);
43 self::assertSame('cannot use JSON FORMAT ENCODING clause for non-bytea input types', $derivation->facts()->diagnostics[0]->message());
44 }
45
46 public function testDeriveClauseDerivesTheValue(): void
47 {
48 $value = new Constant(new IntegerConstant('1'));
49 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
50 (new JsonValueExpression($value))->deriveClause($derivation, $derivation->environment());
51 self::assertTrue($derivation->facts()->covers($value));
52 }
53
54 public function testRenderWritesTheFormat(): void
55 {
56 $value = new JsonValueExpression(new Constant(new StringConstant('{}')), new JsonFormat());
57 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
58 $value->render($out);
59 self::assertSame('\'{}\' FORMAT JSON', (new Lexical())->join($out->pieces()));
60 }
61}
62