packages/sql-semantics-postgres/tests/Unit/Statement/Query/With/WithClauseTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query\With;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Statement\Query\With\WithClause::class)]
12#[Small]
13final class WithClauseTest extends TestCase
14{
15    public function testDeriveCommonTablesBindsTheTables(): void
16    {
17        $derivation = new \SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation((new \SqlSemantics\Platform\PostgreSql\Platform())->context(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172), null, [], true));
18        $environment = (new \SqlSemantics\Platform\PostgreSql\Statement\Query\With\WithClause([new \SqlSemantics\Platform\PostgreSql\Statement\Query\With\CommonTableExpression(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('x'), 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('a')])], true))->deriveCommonTables($derivation, $derivation->environment());
19        self::assertSame('a', $environment->commonTable(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('x'))?->shape->slots[0]->name?->value);
20    }
21
22    public function testRenderWritesRecursive(): void
23    {
24        $out = new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Output(new \SqlSemantics\Platform\PostgreSql\Rendering\Codec(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172));
25        (new \SqlSemantics\Platform\PostgreSql\Statement\Query\With\WithClause([new \SqlSemantics\Platform\PostgreSql\Statement\Query\With\CommonTableExpression(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('x'), 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('a')])], true))->render($out);
26        self::assertSame('WITH RECURSIVE x (a) AS (SELECT 1)', (new \SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical())->join($out->pieces()));
27    }
28}
29