packages/sql-semantics-postgres/tests/Unit/Statement/Routine/Signature/RoutineSignatureTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Signature;
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\Literal\Constant;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
20use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
21use SqlSemantics\Platform\PostgreSql\Statement\Routine\DefaultSpelling;
22use SqlSemantics\Platform\PostgreSql\Statement\Routine\FunctionParameter;
23use SqlSemantics\Platform\PostgreSql\Statement\Routine\ParameterMode;
24use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\RoutineSignature;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
27
28#[CoversClass(RoutineSignature::class)]
29#[Small]
30final class RoutineSignatureTest extends TestCase
31{
32    public function testDeriveClauseDerivesTheArguments(): void
33    {
34        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
35        (new RoutineSignature(new DottedName([new Name('f')]), [new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))))]))->deriveClause($derivation, $derivation->environment());
36        self::assertSame([], $derivation->facts()->diagnostics);
37    }
38
39    public function testRenderWritesTheArgumentListOnlyWhenWritten(): void
40    {
41        $listed = new Output(new Codec(GrammarRelease::PostgreSql172));
42        (new RoutineSignature(new DottedName([new Name('s'), new Name('f')]), [new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new Name('a'), ParameterMode::Out)]))->render($listed);
43        $empty = new Output(new Codec(GrammarRelease::PostgreSql172));
44        (new RoutineSignature(new DottedName([new Name('f')]), []))->render($empty);
45        $bare = new Output(new Codec(GrammarRelease::PostgreSql172));
46        (new RoutineSignature(new DottedName([new Name('f')])))->render($bare);
47        self::assertSame(['s.f (OUT a int4)', 'f ()', 'f'], [(new Lexical())->join($listed->pieces()), (new Lexical())->join($empty->pieces()), (new Lexical())->join($bare->pieces())]);
48    }
49
50    public function testRejectsADefaultValue(): void
51    {
52        $this->expectExceptionMessage('An argument of a signature has no default value.');
53        new RoutineSignature(new DottedName([new Name('f')]), [new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), null, null, false, new Constant(new IntegerConstant('1')), DefaultSpelling::Keyword)]);
54    }
55}
56