packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/Query/JsonBehaviorClauseTest.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\JsonBehaviorClause;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\Query\JsonBehaviorKind;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
22
23#[CoversClass(JsonBehaviorClause::class)]
24#[Small]
25final class JsonBehaviorClauseTest extends TestCase
26{
27    public function testRejectsNoBehavior(): void
28    {
29        $this->expectExceptionMessage('A behavior clause has at least one behavior.');
30        new JsonBehaviorClause(null, null);
31    }
32
33    public function testDeriveClauseDerivesBothDefaults(): void
34    {
35        $empty = new Constant(new IntegerConstant('1'));
36        $error = new Constant(new IntegerConstant('1'));
37        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
38        (new JsonBehaviorClause(new JsonBehavior(JsonBehaviorKind::Default, $empty), new JsonBehavior(JsonBehaviorKind::Default, $error)))->deriveClause($derivation, $derivation->environment());
39        self::assertTrue($derivation->facts()->covers($empty));
40        self::assertTrue($derivation->facts()->covers($error));
41    }
42
43    public function testRenderWritesOnEmptyThenOnError(): void
44    {
45        $clause = new JsonBehaviorClause(new JsonBehavior(JsonBehaviorKind::Null), new JsonBehavior(JsonBehaviorKind::Error));
46        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
47        $clause->render($out);
48        self::assertSame('NULL ON EMPTY ERROR ON ERROR', (new Lexical())->join($out->pieces()));
49    }
50}
51