packages/sql-semantics-postgres/tests/Unit/Lowering/Leaf/OperatorsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Leaf;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Lexer\Token;
11use SqlParser\PostgreSql\PostgreSqlParser;
12use SqlSemantics\Platform\PostgreSql\Lowering\Leaf\Operators;
13use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
15use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
16use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
18use SqlSemantics\Platform\PostgreSql\Platform;
19
20#[CoversClass(Operators::class)]
21#[Small]
22final class OperatorsTest extends TestCase
23{
24 public function testSymbolReadsNotEqualsAsItsOperator(): void
25 {
26 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
27 self::assertSame('<>', $lowering->operators->symbol(new Token(1, 'NOT_EQUALS', '!=', 0))->value);
28 self::assertSame('@>', $lowering->operators->symbol(new Token(1, 'Op', '@>', 0))->value);
29 self::assertCount(2, $lowering->leaves->all());
30 }
31
32 public function testOperatorLowersTheOperatorSyntax(): void
33 {
34 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
35 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 ORDER BY 1 USING OPERATOR(pg_catalog.<)');
36 $operator = $lowering->operators->operator($tree->find('qual_all_Op')[0]);
37 self::assertSame(['<', 'pg_catalog', true], [$operator->name->value, $operator->qualifiers[0]->value, $operator->explicit]);
38 }
39
40 public function testOperatorLowersABareOperator(): void
41 {
42 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
43 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 ORDER BY 1 USING >=');
44 self::assertSame('>=', $lowering->operators->operator($tree->find('qual_all_Op')[0])->name->value);
45 }
46
47 public function testQualifiedLowersTheQualifierChain(): void
48 {
49 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
50 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP OPERATOR a.b.+ (int, int)');
51 $operator = $lowering->operators->qualified($tree->find('any_operator')[0], false);
52 self::assertSame(['a', 'b'], array_map(static fn (Name $name): string => $name->value, $operator->qualifiers));
53 self::assertFalse($operator->explicit);
54 }
55}
56