packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Declaration/TableTest.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\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\Sqlite\Model\Contract\LanguageProfile;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Column;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\ImplicitColumn;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\RelationKind;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Table;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Comparison;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
19use SqlSemantics\Platform\Sqlite\Semantics;
20use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
21
22#[CoversClass(Table::class)]
23#[Small]
24final class TableTest extends TestCase
25{
26    public function testMatchingColumnsFindsDeclaredColumnsBeforeImplicitOnes(): void
27    {
28        $id = new Column(new Name('id'), Storage::Integer);
29        $rowid = new Column(new Name('rowid'), Storage::Integer);
30        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::Sqlite3472), [$id], [new ImplicitColumn([new Name('rowid'), new Name('id')], $rowid)]);
31
32        self::assertSame([$id], $table->matchingColumns('id', Comparison::Sensitive));
33        self::assertSame([$rowid], $table->matchingColumns('rowid', Comparison::Sensitive));
34        self::assertSame([], $table->matchingColumns('other', Comparison::Sensitive));
35    }
36
37    public function testMatchingColumnsKeepsARepeatedDeclaredName(): void
38    {
39        $first = new Column(new Name('a'), Storage::Integer);
40        $second = new Column(new Name('A'), Storage::Text);
41        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::Sqlite3472), [$first, $second]);
42
43        self::assertSame([$first, $second], $table->matchingColumns('a', Comparison::AsciiInsensitive));
44        self::assertSame([$first], $table->matchingColumns('a', Comparison::Sensitive));
45    }
46
47    public function testMatchingColumnsSearchesEveryImplicitNameWhenNoDeclaredColumnMatches(): void
48    {
49        $rowid = new Column(new Name('rowid'), Storage::Integer);
50        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::Sqlite3472), [], [new ImplicitColumn([new Name('rowid'), new Name('oid')], $rowid)], false);
51
52        self::assertSame([$rowid], $table->matchingColumns('OID', Comparison::AsciiInsensitive));
53        self::assertSame([], $table->matchingColumns('OID', Comparison::Sensitive));
54        self::assertFalse($table->complete);
55    }
56
57    public function testACompleteTableIsTheDefault(): void
58    {
59        $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::Sqlite3472), []);
60
61        self::assertTrue($table->complete);
62        self::assertSame([], $table->implicit);
63    }
64
65    public function testKindIsABaseTableUnlessStated(): void
66    {
67        $profile = (new Semantics())->profile();
68        $name = new QualifiedName(new Name('v'));
69
70        self::assertSame(RelationKind::BaseTable, (new Table($name, $profile, []))->kind);
71        self::assertSame(RelationKind::View, (new Table($name, $profile, [], [], true, RelationKind::View))->kind);
72    }
73}
74