packages/sql-semantics-sqlite/tests/Unit/Model/Resolution/TableLookupTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Resolution;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Contract\AnalysisContext;
11use SqlSemantics\Platform\Sqlite\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\Sqlite\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\Sqlite\Model\Resolution\TableLookup;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Table;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\ConditionalTable;
18use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\ConflictingTables;
19use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\DeclaredTable;
20use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\MissingTable;
21use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\UndeclaredTable;
22
23#[CoversClass(TableLookup::class)]
24#[Small]
25final class TableLookupTest extends TestCase
26{
27    public function testFindResolvesTheOneDeclarationOfTheFirstSchemaThatHasIt(): void
28    {
29        $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
30        $temp = new Table(new QualifiedName(new Name('t'), new Name('temp')), $profile, []);
31        $main = new Table(new QualifiedName(new Name('t'), new Name('main')), $profile, []);
32        $context = new AnalysisContext($profile, [new Name('temp'), new Name('main')], [$main, $temp]);
33
34        $resolution = (new TableLookup())->find($context, new QualifiedName(new Name('t')));
35
36        self::assertInstanceOf(DeclaredTable::class, $resolution);
37        self::assertSame($temp, $resolution->table);
38    }
39
40    public function testFindReportsConflictingDeclarationsOfOneSchema(): void
41    {
42        $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
43        $first = new Table(new QualifiedName(new Name('t')), $profile, []);
44        $second = new Table(new QualifiedName(new Name('t')), $profile, []);
45        $context = new AnalysisContext($profile, [new Name('main')], [$first, $second]);
46
47        $resolution = (new TableLookup())->find($context, new QualifiedName(new Name('t')));
48
49        self::assertInstanceOf(ConflictingTables::class, $resolution);
50        self::assertSame([$first, $second], $resolution->candidates);
51    }
52
53    public function testFindIsMissingInACompleteContextAndUndeclaredInAnOpenOne(): void
54    {
55        $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
56        $complete = new AnalysisContext($profile, [new Name('main')]);
57        $open = new AnalysisContext($profile, [new Name('main')], [], false);
58
59        $missing = (new TableLookup())->find($complete, new QualifiedName(new Name('t')));
60        $undeclared = (new TableLookup())->find($open, new QualifiedName(new Name('t'), new Name('aux')));
61
62        self::assertInstanceOf(MissingTable::class, $missing);
63        self::assertInstanceOf(UndeclaredTable::class, $undeclared);
64        self::assertSame('the declaration of relation aux.t', $undeclared->missing->describe());
65    }
66
67    public function testFindIsConditionalForALaterSchemaOfAnOpenContext(): void
68    {
69        $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
70        $main = new Table(new QualifiedName(new Name('t'), new Name('main')), $profile, []);
71        $open = new AnalysisContext($profile, [new Name('temp'), new Name('main')], [$main], false);
72        $complete = new AnalysisContext($profile, [new Name('temp'), new Name('main')], [$main]);
73
74        $conditional = (new TableLookup())->find($open, new QualifiedName(new Name('t')));
75        $declared = (new TableLookup())->find($complete, new QualifiedName(new Name('t')));
76
77        self::assertInstanceOf(ConditionalTable::class, $conditional);
78        self::assertSame([$main], $conditional->candidates);
79        self::assertSame('t', $conditional->missing->name->name->value);
80        self::assertInstanceOf(DeclaredTable::class, $declared);
81    }
82
83    public function testFindSearchesOnlyTheSchemaAQualifiedNameWrites(): void
84    {
85        $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
86        $main = new Table(new QualifiedName(new Name('t'), new Name('main')), $profile, []);
87        $open = new AnalysisContext($profile, [new Name('temp'), new Name('main')], [$main], false);
88
89        $qualified = (new TableLookup())->find($open, new QualifiedName(new Name('t'), new Name('main')));
90        $elsewhere = (new TableLookup())->find($open, new QualifiedName(new Name('t'), new Name('temp')));
91
92        self::assertInstanceOf(DeclaredTable::class, $qualified);
93        self::assertInstanceOf(UndeclaredTable::class, $elsewhere);
94    }
95}
96