packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/JsonPairTest.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\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Expression\BinaryOperation;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonPair;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonValueExpression;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\KeyValueSpelling;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
25use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
26
27#[CoversClass(JsonPair::class)]
28#[Small]
29final class JsonPairTest extends TestCase
30{
31 public function testRejectsAKeyBeforeValueThatIsNotPrimary(): void
32 {
33 $this->expectExceptionMessage('A key before VALUE is a primary expression; group it.');
34 new JsonPair(new BinaryOperation(new OperatorName(new Name('=')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('1'))), new JsonValueExpression(new Constant(new IntegerConstant('1'))), KeyValueSpelling::Value);
35 }
36
37 public function testDeriveClauseDerivesTheKeyAndTheValue(): void
38 {
39 $key = new Constant(new StringConstant('a'));
40 $value = new Constant(new IntegerConstant('1'));
41 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42 (new JsonPair($key, new JsonValueExpression($value)))->deriveClause($derivation, $derivation->environment());
43 self::assertTrue($derivation->facts()->covers($key));
44 self::assertTrue($derivation->facts()->covers($value));
45 }
46
47 public function testRenderWritesBothSpellings(): void
48 {
49 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
50 (new JsonPair(new Constant(new StringConstant('a')), new JsonValueExpression(new Constant(new IntegerConstant('1'))), KeyValueSpelling::Value))->render($out);
51 $out->symbol(',');
52 (new JsonPair(new Constant(new StringConstant('b')), new JsonValueExpression(new Constant(new IntegerConstant('1')))))->render($out);
53 self::assertSame('\'a\' VALUE 1, \'b\' : 1', (new Lexical())->join($out->pieces()));
54 }
55}
56