packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Declaration/ImplicitColumnTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement\Declaration;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\ImplicitColumn;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
15
16#[CoversClass(ImplicitColumn::class)]
17#[Medium]
18final class ImplicitColumnTest extends TestCase
19{
20    public function testEveryNameFindsTheColumnAndItIsNotPartOfTheStar(): void
21    {
22        $semantics = new Semantics();
23        $table = $semantics->analyze('CREATE TABLE t (a TEXT)')->declarations()[0];
24
25        $query = $semantics->analyze('SELECT *, oid FROM t', [$table]);
26        $fields = $query->fields();
27
28        self::assertCount(1, $table->implicit);
29        self::assertSame(['rowid', 'oid', '_rowid_'], array_map(static fn (Name $name): string => $name->value, $table->implicit[0]->names));
30        self::assertNotNull($fields);
31        self::assertCount(2, $fields);
32        self::assertSame('a', $fields->at(0)->name?->value);
33        self::assertSame($table->implicit[0]->column, $fields->at(1)->column());
34        self::assertSame('rowid', $fields->at(1)->name?->value);
35    }
36
37    public function testAnImplicitColumnNamesAtLeastOneName(): void
38    {
39        $this->expectExceptionMessage('An implicit column has at least one name.');
40
41        new ImplicitColumn([], new Column(new Name('rowid'), Storage::Integer));
42    }
43}
44