packages/sql-semantics-postgres/tests/Unit/Rules/Query/PositionsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Query\Positions::class)]
12#[Medium]
13final class PositionsTest extends TestCase
14{
15    public function testValueFoldsMinusSignsAndParentheses(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $query = $semantics->analyze('SELECT 1 ORDER BY - (- 1)');
19        $select = $query->statement;
20        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Select::class, $select);
21        $position = $select->options?->order[0]->expression;
22        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\OutputPosition::class, $position);
23        self::assertSame('1', (new \SqlSemantics\Platform\PostgreSql\Rules\Query\Positions())->value($position->position));
24    }
25
26    public function testMisusedTellsANonIntegerConstant(): void
27    {
28        self::assertSame([true, false], [(new \SqlSemantics\Platform\PostgreSql\Rules\Query\Positions())->misused(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral()), (new \SqlSemantics\Platform\PostgreSql\Rules\Query\Positions())->misused(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1')))]);
29    }
30
31    public function testMinusTellsThePlainMinusSign(): void
32    {
33        self::assertTrue((new \SqlSemantics\Platform\PostgreSql\Rules\Query\Positions())->minus(new \SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\UnaryOperation(new \SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('-')), new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1')))));
34    }
35}
36