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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine;
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\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
19use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
20use SqlSemantics\Platform\PostgreSql\Statement\Routine\FunctionParameter;
21use SqlSemantics\Platform\PostgreSql\Statement\Routine\ParameterMode;
22use SqlSemantics\Platform\PostgreSql\Statement\Routine\RoutineParameters;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
26
27#[CoversClass(RoutineParameters::class)]
28#[Small]
29final class RoutineParametersTest extends TestCase
30{
31    public function testInfersUndeclaredIsFalse(): void
32    {
33        self::assertFalse((new RoutineParameters([]))->infersUndeclared());
34    }
35
36    public function testDeriveRelationHoldsTheInputParameters(): void
37    {
38        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
39        $parameters = new RoutineParameters([
40            new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new Name('a')),
41            new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('text')]))), new Name('b'), ParameterMode::Out),
42            new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int8')])))),
43        ]);
44        $shape = $derivation->relation($parameters, $derivation->environment())->shape;
45        self::assertSame(['a', null], [$shape->slots[0]->name?->value, $shape->slots[1]->name]);
46        self::assertEquals([new Known(Builtin::Int4), new Known(Builtin::Int8)], [$shape->slots[0]->type, $shape->slots[1]->type]);
47    }
48
49    public function testRenderWritesTheParenthesizedList(): void
50    {
51        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52        (new RoutineParameters([]))->render($out);
53        self::assertSame('()', (new Lexical())->join($out->pieces()));
54    }
55
56}
57