packages/sql-semantics-postgres/tests/Unit/Rules/Query/Facts/SetOperationFactsTest.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\SetOperationFacts::class)]
12#[Medium]
13final class SetOperationFactsTest extends TestCase
14{
15    public function testDeriveKeepsTheNamesOfTheFirstOperand(): void
16    {
17        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18        $query = $semantics->inspect('SELECT 1 AS a INTERSECT SELECT 2 AS b');
19        self::assertSame('a', $query->field(0)->name?->value);
20    }
21
22    public function testCombinedReportsTypesThatCannotBeMatched(): void
23    {
24        $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
25        $query = $semantics->inspect('SELECT 1 UNION SELECT TRUE');
26        self::assertSame('UNION types integer and boolean cannot be matched.', $query->facts->diagnostics[0]->message());
27    }
28
29    public function testReboundBindsTheRecursiveTable(): void
30    {
31        $context = (new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true);
32        $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation($context);
33        $fact = $derivation->query(new \SqlSemantics\Platform\PostgreSql\Statement\Query\QueryExpression(new \SqlSemantics\Platform\PostgreSql\Statement\Query\With\WithClause([new \SqlSemantics\Platform\PostgreSql\Statement\Query\With\CommonTableExpression(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('r'), new \SqlSemantics\Platform\PostgreSql\Statement\Query\SetOperation(new \SqlSemantics\Platform\PostgreSql\Statement\Query\Select([new \SqlSemantics\Platform\PostgreSql\Statement\Query\ExpressionTarget(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1')), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('n'))]), \SqlSemantics\Platform\PostgreSql\Statement\Query\SetOperator::Union, new \SqlSemantics\Platform\PostgreSql\Statement\Query\Select([new \SqlSemantics\Platform\PostgreSql\Statement\Query\ExpressionTarget(new \SqlSemantics\Platform\PostgreSql\Statement\Expression\BinaryOperation(new \SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('+')), new \SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('n')]), new \SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant(new \SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant('1'))))], new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('r')))))), [], null, null, null)], true), new \SqlSemantics\Platform\PostgreSql\Statement\Query\Select([new \SqlSemantics\Platform\PostgreSql\Statement\Query\ExpressionTarget(new \SqlSemantics\Platform\PostgreSql\Statement\Expression\ColumnReference([new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('n')]))], new \SqlSemantics\Platform\PostgreSql\Statement\Relation\TableInput(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('r')))))), $derivation->environment());
34        self::assertSame('integer', ($fact->fields()?->at(0)->type instanceof \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known ? $fact->fields()->at(0)->type->descriptor->name() : null));
35    }
36}
37