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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query\Facts;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Query\Facts\QueryExpressionFacts::class)]
12#[Medium]
13final class QueryExpressionFactsTest extends TestCase
14{
15    public function testDeriveReportsLockingOfASetOperation(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $query = $semantics->inspect('SELECT 1 UNION SELECT 2 FOR UPDATE');
19        self::assertSame('FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT', $query->facts->diagnostics[0]->message());
20    }
21
22    public function testMergedReportsASecondLimit(): void
23    {
24        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
25        $query = $semantics->inspect('(SELECT 1 LIMIT 1) LIMIT 2');
26        self::assertSame('multiple LIMIT clauses not allowed', $query->facts->diagnostics[0]->message());
27    }
28
29    public function testAnyTellsWhetherAnOptionMatches(): void
30    {
31        self::assertTrue((new \SqlSemantics\Platform\PostgreSql\Rules\Query\Facts\QueryExpressionFacts())->any([null, new \SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\SelectOptions(readOnly: true)], static fn (\SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\SelectOptions $options): bool => $options->readOnly));
32    }
33}
34