packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Predicate/JsonTestTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Predicate;
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\Type\Invalid;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\Negation;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\JsonItemKind;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\JsonTest;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(JsonTest::class)]
29#[Small]
30final class JsonTestTest extends TestCase
31{
32    public function testDeriveScalarIsBooleanForAString(): void
33    {
34        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
35        $fact = $derivation->scalar(new JsonTest(new Constant(new StringConstant('{}')), false, JsonItemKind::JsonObject), $derivation->environment());
36        self::assertEquals(new Known(Builtin::Bool), $fact->type);
37    }
38
39    public function testDeriveScalarReportsAnInteger(): void
40    {
41        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42        $fact = $derivation->scalar(new JsonTest(new Constant(new IntegerConstant('1')), false, JsonItemKind::Json), $derivation->environment());
43        self::assertInstanceOf(Invalid::class, $fact->type);
44    }
45
46    public function testRenderWritesTheKind(): void
47    {
48        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
49        (new JsonTest(new NullLiteral(), true, JsonItemKind::JsonValue))->render($out);
50        self::assertSame('NULL IS NOT JSON VALUE', (new Lexical())->join($out->pieces()));
51    }
52
53    public function testRejectsANegation(): void
54    {
55        $this->expectExceptionMessage('The tested value needs parentheses to keep its place.');
56        new JsonTest(new Negation(new NullLiteral()), false, JsonItemKind::Json);
57    }
58}
59