packages/sql-semantics-sqlite/tests/Unit/Model/Contract/AnalysisContextTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Contract;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
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\Contract\ParameterStyle;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Declaration\Table;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Comparison;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\QualifiedName;
18use SqlSemantics\Platform\Sqlite\Semantics;
19
20#[CoversClass(AnalysisContext::class)]
21#[Medium]
22final class AnalysisContextTest extends TestCase
23{
24 public function testDeclaredKeepsOneCopyOfTheSameDeclarationObject(): void
25 {
26 $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
27 $table = new Table(new QualifiedName(new Name('t')), $profile, []);
28
29 $context = new AnalysisContext($profile, [new Name('main')], [$table, $table]);
30
31 self::assertSame([$table], $context->tables);
32 self::assertSame([$table], $context->declared(new QualifiedName(new Name('t')), new Name('main')));
33 }
34
35 public function testDeclaredKeepsDistinctDeclarationsWithTheSameName(): void
36 {
37 $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
38 $first = new Table(new QualifiedName(new Name('t')), $profile, []);
39 $second = new Table(new QualifiedName(new Name('t')), $profile, []);
40
41 $context = new AnalysisContext($profile, [new Name('main')], [$first, $second]);
42
43 self::assertSame([$first, $second], $context->declared(new QualifiedName(new Name('t')), new Name('main')));
44 }
45
46 public function testDeclaredSearchesOneSchemaAndUsesTheDeclarationSchemaForUnqualifiedTables(): void
47 {
48 $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
49 $unqualified = new Table(new QualifiedName(new Name('t')), $profile, []);
50 $qualified = new Table(new QualifiedName(new Name('t'), new Name('aux')), $profile, []);
51
52 $context = new AnalysisContext($profile, [new Name('temp'), new Name('main')], [$unqualified, $qualified], true, Comparison::Sensitive, Comparison::Sensitive, new Name('main'));
53
54 self::assertSame('main', $context->declarationSchema->value);
55 self::assertSame([$unqualified], $context->declared(new QualifiedName(new Name('t')), new Name('main')));
56 self::assertSame([$qualified], $context->declared(new QualifiedName(new Name('t')), new Name('aux')));
57 self::assertSame([], $context->declared(new QualifiedName(new Name('t')), new Name('temp')));
58 }
59
60 public function testDeclaredComparesNamesAsTheContextSays(): void
61 {
62 $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
63 $table = new Table(new QualifiedName(new Name('Users')), $profile, []);
64 $sensitive = new AnalysisContext($profile, [new Name('main')], [$table]);
65 $insensitive = new AnalysisContext($profile, [new Name('main')], [$table], true, Comparison::AsciiInsensitive);
66
67 self::assertSame([], $sensitive->declared(new QualifiedName(new Name('USERS')), new Name('main')));
68 self::assertSame([$table], $insensitive->declared(new QualifiedName(new Name('USERS')), new Name('MAIN')));
69 }
70
71 public function testDeclaredMatchesACatalogOnlyWhenBothSidesWriteOne(): void
72 {
73 $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
74 $table = new Table(new QualifiedName(new Name('t'), new Name('main'), new Name('db')), $profile, []);
75 $context = new AnalysisContext($profile, [new Name('main')], [$table]);
76
77 self::assertSame([$table], $context->declared(new QualifiedName(new Name('t')), new Name('main')));
78 self::assertSame([$table], $context->declared(new QualifiedName(new Name('t'), new Name('main'), new Name('db')), new Name('main')));
79 self::assertSame([], $context->declared(new QualifiedName(new Name('t'), new Name('main'), new Name('other')), new Name('main')));
80 }
81
82 public function testDeclaredDefaultsTheDeclarationSchemaToTheFirstSearchedSchema(): void
83 {
84 $profile = new LanguageProfile(GrammarRelease::Sqlite3472);
85
86 $context = new AnalysisContext($profile, [new Name('first'), new Name('second')]);
87
88 self::assertSame('first', $context->declarationSchema->value);
89 self::assertTrue($context->complete);
90 self::assertSame([], $context->tables);
91 }
92
93 public function testDeclaredRefusesADeclarationOfAnotherProfile(): void
94 {
95 $sqlite = new LanguageProfile(GrammarRelease::Sqlite3472);
96 $table = new Table(new QualifiedName(new Name('t')), new LanguageProfile(GrammarRelease::Sqlite3472, parameters: ParameterStyle::Named), []);
97
98 $this->expectExceptionMessage('A declaration must belong to the language profile of the context.');
99
100 new AnalysisContext($sqlite, [new Name('main')], [$table]);
101 }
102
103 public function testDeclaredRefusesAnEmptySearchPath(): void
104 {
105 $this->expectExceptionMessage('A context searches at least one schema.');
106
107 new AnalysisContext(new LanguageProfile(GrammarRelease::Sqlite3472), []);
108 }
109
110 public function testDeclaredFindsTheTableAStatementProvidedToTheFacade(): void
111 {
112 $semantics = new Semantics();
113 $table = $semantics->analyze('CREATE TABLE t (a INTEGER)')->declarations()[0];
114
115 $context = $semantics->context([$table]);
116
117 self::assertTrue($context->complete);
118 self::assertSame([$table], $context->declared(new QualifiedName(new Name('T')), new Name('main')));
119 self::assertFalse($semantics->context()->complete);
120 }
121}
122