packages/sql-semantics-postgres/tests/Unit/Statement/Routine/FunctionParameterTest.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\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\Type\Designation\NamedDesignation;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
26
27#[CoversClass(FunctionParameter::class)]
28#[Small]
29final class FunctionParameterTest extends TestCase
30{
31 public function testInputIsTrueWithoutAMode(): void
32 {
33 self::assertTrue((new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))))->input());
34 }
35
36 public function testOutputFollowsTheMode(): void
37 {
38 $type = new TypeName(new NamedDesignation(new DottedName([new Name('int4')])));
39 self::assertTrue((new FunctionParameter($type, null, ParameterMode::InOut))->output());
40 self::assertFalse((new FunctionParameter($type))->output());
41 }
42
43 public function testDeriveClauseDerivesTheDefaultValue(): void
44 {
45 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
46 $default = new Constant(new IntegerConstant('1'));
47 (new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), null, null, false, $default, DefaultSpelling::EqualsSign))->deriveClause($derivation, $derivation->environment());
48 self::assertTrue($derivation->facts()->covers($default));
49 }
50
51 public function testRenderWritesTheNameBeforeTheModeWhenWrittenSo(): void
52 {
53 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
54 (new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new Name('a'), ParameterMode::InAndOut, true, new Constant(new IntegerConstant('1')), DefaultSpelling::Keyword))->render($out);
55 self::assertSame('a IN OUT int4 DEFAULT 1', (new Lexical())->join($out->pieces()));
56 }
57
58 public function testRejectsANameFirstWithoutAMode(): void
59 {
60 $this->expectExceptionMessage('A parameter name is written before the mode only when both are written.');
61 new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new Name('a'), null, true);
62 }
63
64 public function testRejectsASpellingWithoutADefault(): void
65 {
66 $this->expectExceptionMessage('A default spelling is given exactly with a default value.');
67 new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), null, null, false, null, DefaultSpelling::Keyword);
68 }
69}
70