packages/sql-semantics-postgres/tests/Unit/Statement/Operator/OperatorMemberTest.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\Name\OperatorName;
21use SqlSemantics\Platform\PostgreSql\Statement\Operator\MemberPurpose;
22use SqlSemantics\Platform\PostgreSql\Statement\Operator\OperatorMember;
23use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\OperatorArity;
24use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\OperatorSignature;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
27
28#[CoversClass(OperatorMember::class)]
29#[Small]
30final class OperatorMemberTest extends TestCase
31{
32    public function testDeriveClauseDerivesTheOperandTypes(): void
33    {
34        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
35        (new OperatorMember(new IntegerConstant('1'), new OperatorSignature(new OperatorName(new Name('<')), OperatorArity::Binary, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))])))->deriveClause($derivation, $derivation->environment());
36        self::assertSame([], $derivation->facts()->diagnostics);
37    }
38
39    public function testRenderWritesStrategyOperatorAndPurpose(): void
40    {
41        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
42        (new OperatorMember(new IntegerConstant('3'), new OperatorName(new Name('<->')), new MemberPurpose(new DottedName([new Name('f')]))))->render($out);
43        self::assertSame('OPERATOR 3 <-> FOR ORDER BY f', (new Lexical())->join($out->pieces()));
44    }
45
46    public function testRejectsTheExplicitSyntax(): void
47    {
48        $this->expectExceptionMessage('An operator class names an operator without OPERATOR(...).');
49        new OperatorMember(new IntegerConstant('1'), new OperatorName(new Name('<'), [], true));
50    }
51}
52