packages/sql-semantics-postgres/tests/Unit/Lowering/Expression/OperatorRuleTest.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\OperatorRule;
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\Cast;
18use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\BooleanOperation;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\DistinctTest;
20
21#[CoversClass(OperatorRule::class)]
22#[Small]
23final class OperatorRuleTest extends TestCase
24{
25    public function testLowerAnswersNullForAPredicate(): void
26    {
27        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
28        $form = $lowering->productions->form((new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 IS NULL')->find('a_expr')[0]);
29        self::assertNull((new OperatorRule($lowering))->lower($form));
30    }
31
32    public function testOperatorsLowersAConjunction(): void
33    {
34        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
35        $form = $lowering->productions->form((new PostgreSqlParser('pg-17.2'))->parse('SELECT true AND NOT false')->find('a_expr')[0]);
36        self::assertInstanceOf(BooleanOperation::class, (new OperatorRule($lowering))->operators($form));
37    }
38
39    public function testPostfixLowersACastAndADistinctTest(): void
40    {
41        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
42        $cast = $lowering->productions->form((new PostgreSqlParser('pg-17.2'))->parse('SELECT 1::text')->find('a_expr')[0]);
43        $distinct = $lowering->productions->form((new PostgreSqlParser('pg-17.2'))->parse('SELECT 1 IS NOT DISTINCT FROM 2')->find('a_expr')[0]);
44        self::assertInstanceOf(Cast::class, (new OperatorRule($lowering))->postfix($cast));
45        self::assertInstanceOf(DistinctTest::class, (new OperatorRule($lowering))->postfix($distinct));
46    }
47}
48