packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/Query/JsonBehaviorTest.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\Platform;
16use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
17use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonBehavior;
18use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonBehaviorKind;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
21
22#[CoversClass(JsonBehavior::class)]
23#[Small]
24final class JsonBehaviorTest extends TestCase
25{
26 public function testRejectsDefaultWithoutExpression(): void
27 {
28 $this->expectExceptionMessage('A JSON behavior has an expression exactly when it is DEFAULT.');
29 new JsonBehavior(JsonBehaviorKind::Default);
30 }
31
32 public function testDeriveClauseDerivesTheDefault(): void
33 {
34 $value = new Constant(new IntegerConstant('1'));
35 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
36 (new JsonBehavior(JsonBehaviorKind::Default, $value))->deriveClause($derivation, $derivation->environment());
37 self::assertTrue($derivation->facts()->covers($value));
38 }
39
40 public function testRenderWritesTheKeywords(): void
41 {
42 $behavior = new JsonBehavior(JsonBehaviorKind::EmptyObject);
43 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
44 $behavior->render($out);
45 self::assertSame('EMPTY OBJECT', (new Lexical())->join($out->pieces()));
46 }
47}
48