packages/sql-semantics-postgres/tests/Unit/Lowering/Expression/SublinkRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Expression\SublinkRule;
12use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\InList;
18use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\MatchKeyword;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\QuantifiedArray;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Subquery\Quantifier;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
22
23#[CoversClass(SublinkRule::class)]
24#[Small]
25final class SublinkRuleTest extends TestCase
26{
27 public function testLowerLowersAComparisonWithAnArray(): void
28 {
29 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
30 $form = $lowering->productions->form((new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 = ANY ($1)')->find('a_expr')[0]);
31 self::assertInstanceOf(QuantifiedArray::class, (new SublinkRule($lowering))->lower($form));
32 }
33
34 public function testMembershipLowersAList(): void
35 {
36 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
37 $in = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 IN (1, 2)')->find('in_expr')[0];
38 self::assertInstanceOf(InList::class, (new SublinkRule($lowering))->membership(new NullLiteral(), false, $in));
39 }
40
41 public function testComparatorLowersAKeyword(): void
42 {
43 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
44 $operator = (new PostgreSqlParser('pg-17.2'))->parse("SELECT 'a' NOT ILIKE ALL ($1)")->find('subquery_Op')[0];
45 self::assertSame(MatchKeyword::NotILike, (new SublinkRule($lowering))->comparator($operator));
46 }
47
48 public function testQuantifierLowersSome(): void
49 {
50 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
51 $quantifier = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 = SOME ($1)')->find('sub_type')[0];
52 self::assertSame(Quantifier::Some, (new SublinkRule($lowering))->quantifier($quantifier));
53 }
54}
55