packages/sql-semantics-postgres/tests/Unit/Statement/Name/OperatorNameTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Name;
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\OperatorName;
19
20#[CoversClass(OperatorName::class)]
21#[Small]
22final class OperatorNameTest extends TestCase
23{
24    public function testDeriveClauseRecordsNothing(): void
25    {
26        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
27        (new OperatorName(new Name('+')))->deriveClause($derivation, $derivation->environment());
28        self::assertSame([], $derivation->facts()->diagnostics);
29    }
30
31    public function testRenderWritesABareOperator(): void
32    {
33        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
34        (new OperatorName(new Name('@>')))->render($out);
35        self::assertSame('@>', (new Lexical())->join($out->pieces()));
36    }
37
38    public function testRenderWritesTheOperatorSyntaxWithQualifiers(): void
39    {
40        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
41        (new OperatorName(new Name('`'), [new Name('pg_catalog')], true))->render($out);
42        self::assertSame('OPERATOR (pg_catalog.`)', (new Lexical())->join($out->pieces()));
43    }
44
45    public function testRenderWritesBareQualifiersForAnObjectName(): void
46    {
47        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
48        (new OperatorName(new Name('+'), [new Name('App')]))->render($out);
49        self::assertSame('"App".+', (new Lexical())->join($out->pieces()));
50    }
51
52    public function testRejectsTextThatIsNotAnOperator(): void
53    {
54        $this->expectExceptionMessage('An operator name is 1 to 63 operator characters.');
55        new OperatorName(new Name('a'));
56    }
57
58    public function testRejectsTheAliasOfNotEquals(): void
59    {
60        $this->expectExceptionMessage('An operator name holds no comment start');
61        new OperatorName(new Name('!='));
62    }
63
64    public function testRejectsATrailingSignWithoutASpecialCharacter(): void
65    {
66        $this->expectExceptionMessage('A multi-character operator ends in + or - only when');
67        new OperatorName(new Name('*-'));
68    }
69}
70