packages/sql-semantics-sqlite/tests/Unit/Statement/Query/With/CommonTableTest.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\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Query\Ordering\ListedColumn;
16use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
17use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
18use SqlSemantics\Platform\Sqlite\Statement\Query\Star;
19use SqlSemantics\Platform\Sqlite\Statement\Query\ValuesClause;
20use SqlSemantics\Platform\Sqlite\Statement\Query\With\CommonTable;
21use SqlSemantics\Platform\Sqlite\Statement\Query\With\Materialization;
22use SqlSemantics\Platform\Sqlite\Statement\Query\With\WithClause;
23use SqlSemantics\Platform\Sqlite\Statement\Query\With\WithQuery;
24use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
25
26#[CoversClass(CommonTable::class)]
27#[Medium]
28final class CommonTableTest extends TestCase
29{
30    public function testRenderWritesTheColumnListAndTheHint(): void
31    {
32        $query = (new Semantics())->analyze('with c(x, y) as materialized (select 1, 2), d as not materialized (values (3)), e as (select 4) select * from c');
33
34        self::assertInstanceOf(WithQuery::class, $query->statement);
35        $tables = $query->statement->with->tables;
36        self::assertSame('c', $tables[0]->name->value);
37        self::assertSame(['x', 'y'], array_map(static fn (ListedColumn $column): string => $column->name->value, $tables[0]->columns));
38        self::assertSame(Materialization::Materialized, $tables[0]->materialization);
39        self::assertInstanceOf(Select::class, $tables[0]->query);
40        self::assertSame([], $tables[1]->columns);
41        self::assertSame(Materialization::NotMaterialized, $tables[1]->materialization);
42        self::assertInstanceOf(ValuesClause::class, $tables[1]->query);
43        self::assertNull($tables[2]->materialization);
44        self::assertSame('WITH c (x, y) AS MATERIALIZED (SELECT 1, 2), d AS NOT MATERIALIZED (VALUES (3)), e AS (SELECT 4) SELECT * FROM c', $query->toString());
45    }
46
47    public function testRenderWritesANewlyBuiltTable(): void
48    {
49        $semantics = new Semantics();
50        $table = new CommonTable(new Name('c'), new Select([new ResultColumn(new IntegerLiteral('1'))]), [new ListedColumn(new Name('x'))], Materialization::NotMaterialized);
51        $operation = new Operation($semantics->context([]), new WithQuery(new WithClause([$table]), new Select([new Star()], new TableInput(new QualifiedName(new Name('c'))))));
52
53        self::assertSame('WITH c (x) AS NOT MATERIALIZED (SELECT 1) SELECT * FROM c', $operation->toString());
54        self::assertSame('x', $operation->field(0)->name?->value);
55    }
56}
57