packages/sql-semantics-postgres/tests/Unit/Statement/Routine/Signature/OperatorSignatureTest.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\Name\DottedName;
19use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
20use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblem;
21use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblemKind;
22use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\OperatorArity;
23use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\OperatorSignature;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
26
27#[CoversClass(OperatorSignature::class)]
28#[Small]
29final class OperatorSignatureTest extends TestCase
30{
31 public function testLeftAnswersTheLeftOperand(): void
32 {
33 $left = new TypeName(new NamedDesignation(new DottedName([new Name('int4')])));
34 self::assertSame([$left, null], [(new OperatorSignature(new OperatorName(new Name('!')), OperatorArity::Postfix, [$left]))->left(), (new OperatorSignature(new OperatorName(new Name('-')), OperatorArity::Prefix, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]))->left()]);
35 }
36
37 public function testRightAnswersTheRightOperand(): void
38 {
39 $right = new TypeName(new NamedDesignation(new DottedName([new Name('text')])));
40 self::assertSame([$right, null], [(new OperatorSignature(new OperatorName(new Name('=')), OperatorArity::Binary, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), $right]))->right(), (new OperatorSignature(new OperatorName(new Name('=')), OperatorArity::Incomplete, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]))->right()]);
41 }
42
43 public function testDeriveClauseReportsASingleType(): void
44 {
45 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
46 (new OperatorSignature(new OperatorName(new Name('=')), OperatorArity::Incomplete, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]))->deriveClause($derivation, $derivation->environment());
47 self::assertEquals([new RoutineProblem(RoutineProblemKind::MissingOperatorArgument)], $derivation->facts()->diagnostics);
48 }
49
50 public function testRenderWritesNoneForAMissingOperand(): void
51 {
52 $prefix = new Output(new Codec(GrammarRelease::PostgreSql172));
53 (new OperatorSignature(new OperatorName(new Name('-'), [new Name('s')]), OperatorArity::Prefix, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]))->render($prefix);
54 $postfix = new Output(new Codec(GrammarRelease::PostgreSql172));
55 (new OperatorSignature(new OperatorName(new Name('!')), OperatorArity::Postfix, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]))->render($postfix);
56 self::assertSame(['s.- (NONE, int4)', '! (int4, NONE)'], [(new Lexical())->join($prefix->pieces()), (new Lexical())->join($postfix->pieces())]);
57 }
58
59 public function testRejectsTheExplicitSyntax(): void
60 {
61 $this->expectExceptionMessage('An operator signature names the operator without the OPERATOR(...) syntax.');
62 new OperatorSignature(new OperatorName(new Name('='), [], true), OperatorArity::Binary, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]);
63 }
64
65 public function testRejectsAWrongTypeCount(): void
66 {
67 $this->expectExceptionMessage('The number of operand types matches how they are written.');
68 new OperatorSignature(new OperatorName(new Name('=')), OperatorArity::Binary, [new TypeName(new NamedDesignation(new DottedName([new Name('int4')])))]);
69 }
70}
71