packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Syntax/KeywordCallTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Syntax;
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\Fact\ScalarFact;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
21use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
22use SqlSemantics\Platform\PostgreSql\Platform;
23use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
24use SqlSemantics\Platform\PostgreSql\Statement\Invocation\NamedArgument;
25use SqlSemantics\Platform\PostgreSql\Statement\Invocation\PositionalArgument;
26use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Syntax\KeywordCall;
27use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Syntax\KeywordFunction;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
30use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
31use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
32
33#[CoversClass(KeywordCall::class)]
34#[Small]
35final class KeywordCallTest extends TestCase
36{
37    public function testRejectsJsonObjectWithoutArguments(): void
38    {
39        $this->expectExceptionMessage('Call arguments are arguments.');
40        new KeywordCall(KeywordFunction::JsonObject, []);
41    }
42
43    public function testOutputNameIsTheFunctionName(): void
44    {
45        self::assertSame('overlay', (new KeywordCall(KeywordFunction::Overlay, []))->outputName()->value);
46    }
47
48    public function testDeriveScalarSearchesThePathForSubstring(): void
49    {
50        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
51        $fact = $derivation->scalar(new KeywordCall(KeywordFunction::Substring, [new PositionalArgument(new Constant(new StringConstant('abc'))), new PositionalArgument(new Constant(new IntegerConstant('1')))]), $derivation->environment());
52        self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::NotNull), $fact);
53    }
54
55    public function testDeriveScalarLeavesNamedArgumentsToTheRoutine(): void
56    {
57        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
58        $fact = $derivation->scalar(new KeywordCall(KeywordFunction::JsonObject, [new NamedArgument(new Name('a'), new Constant(new StringConstant('x')))]), $derivation->environment());
59        self::assertEquals(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('json_object'), new Name('pg_catalog')))]), $fact->type);
60    }
61
62    public function testRenderWritesTheKeywordAndTheArguments(): void
63    {
64        $call = new KeywordCall(KeywordFunction::JsonObject, [new PositionalArgument(new Constant(new StringConstant('{a,b}')))]);
65        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
66        $call->render($out);
67        self::assertSame('JSON_OBJECT(\'{a,b}\')', (new Lexical())->join($out->pieces()));
68    }
69}
70