packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Json/JsonFormatTest.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\Invocation\Json\JsonEncoding;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Json\JsonFormat;
20
21#[CoversClass(JsonFormat::class)]
22#[Small]
23final class JsonFormatTest extends TestCase
24{
25    public function testRejectsAnUnknownEncoding(): void
26    {
27        $this->expectExceptionMessage('A JSON encoding is UTF8, UTF16 or UTF32.');
28        new JsonFormat(new Name('latin1'));
29    }
30
31    public function testEncodingAnswersTheEncodingWritten(): void
32    {
33        self::assertSame([JsonEncoding::Utf8, null], [(new JsonFormat(new Name('UTF8')))->encoding(), (new JsonFormat())->encoding()]);
34    }
35
36    public function testDeriveClauseDerivesNothing(): void
37    {
38        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
39        (new JsonFormat())->deriveClause($derivation, $derivation->environment());
40        self::assertSame([], $derivation->facts()->diagnostics);
41    }
42
43    public function testRenderKeepsTheEncodingAsWritten(): void
44    {
45        $format = new JsonFormat(new Name('UTF16'));
46        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
47        $format->render($out);
48        self::assertSame('FORMAT JSON ENCODING "UTF16"', (new Lexical())->join($out->pieces()));
49    }
50}
51