packages/sql-semantics-sqlite/tests/Unit/Statement/Query/TableStarTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query;
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\Model\Statement\Reference\Table\MissingTable;
14use SqlSemantics\Platform\Sqlite\Semantics;
15use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
16use SqlSemantics\Platform\Sqlite\Statement\Query\TableStar;
17use SqlSemantics\Platform\Sqlite\Statement\Relation\TableInput;
18
19#[CoversClass(TableStar::class)]
20#[Medium]
21final class TableStarTest extends TestCase
22{
23 public function testRenderWritesTheQualifierAndSelectsTheColumnsOfThatRelation(): void
24 {
25 $semantics = new Semantics();
26 $tables = [$semantics->analyze('CREATE TABLE t (a INTEGER NOT NULL, b TEXT)'), $semantics->analyze('CREATE TABLE u (c TEXT)')];
27 $query = $semantics->analyze('select u.*, "t".* from t join u', $tables);
28
29 self::assertInstanceOf(Select::class, $query->statement);
30 self::assertInstanceOf(TableStar::class, $query->statement->columns[0]);
31 self::assertSame('u', $query->statement->columns[0]->table->value);
32 self::assertSame('SELECT u.*, t.* FROM t JOIN u', $query->toString());
33 self::assertSame(['c', 'a', 'b'], array_map(static fn (object $field): ?string => $field->name?->value, [...$query->fields() ?? []]));
34 }
35
36 public function testRenderWritesANewlyBuiltQualifiedStarAndReportsAnUnknownQualifier(): void
37 {
38 $semantics = new Semantics();
39 $operation = new Operation($semantics->context(), new Select([new TableStar(new Name('x'))], new TableInput(new QualifiedName(new Name('t')))));
40
41 self::assertSame('SELECT x.* FROM t', $operation->toString());
42 self::assertInstanceOf(MissingTable::class, $operation->facts->diagnostics[0]);
43 self::assertSame('Relation x does not exist.', $operation->facts->diagnostics[0]->message());
44 }
45}
46