packages/sql-semantics-postgres/tests/Unit/Statement/Operator/FunctionMemberTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Operator;
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\IntegerConstant;
19use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
20use SqlSemantics\Platform\PostgreSql\Statement\Operator\FunctionMember;
21use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\RoutineSignature;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
24
25#[CoversClass(FunctionMember::class)]
26#[Small]
27final class FunctionMemberTest extends TestCase
28{
29    public function testDeriveClauseDerivesTypesAndSignature(): void
30    {
31        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
32        (new FunctionMember(new IntegerConstant('1'), new RoutineSignature(new DottedName([new Name('f')]), []), [new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]))->deriveClause($derivation, $derivation->environment());
33        self::assertSame([], $derivation->facts()->diagnostics);
34    }
35
36    public function testRenderWritesTheOperandTypes(): void
37    {
38        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
39        (new FunctionMember(new IntegerConstant('2'), new RoutineSignature(new DottedName([new Name('f')])), [new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]))->render($out);
40        self::assertSame('FUNCTION 2 (int4, int4) f', (new Lexical())->join($out->pieces()));
41    }
42
43    public function testRejectsAnEmptyTypeList(): void
44    {
45        $this->expectExceptionMessage('The operand types of a support function are at least one type name.');
46        new FunctionMember(new IntegerConstant('1'), new RoutineSignature(new DottedName([new Name('f')])), []);
47    }
48}
49