packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Reference/Table/ConflictingTablesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement\Reference\Table;
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\Reference\Table\ConflictingTables;
13use SqlSemantics\Platform\Sqlite\Semantics;
14
15#[CoversClass(ConflictingTables::class)]
16#[Medium]
17final class ConflictingTablesTest extends TestCase
18{
19    public function testMessageAndCandidatesOfANameTwoDeclarationsClaim(): void
20    {
21        $semantics = new Semantics();
22        $first = $semantics->inspect('CREATE TABLE t (a INTEGER)');
23        $second = $semantics->inspect('CREATE TABLE t (b TEXT)');
24        $query = $semantics->inspect('SELECT 1 FROM t', [$first, $second]);
25
26        $resolution = $query->facts->relation($query->singleNamedInput())->table;
27
28        self::assertInstanceOf(ConflictingTables::class, $resolution);
29        self::assertSame('Relation t has conflicting declarations.', $resolution->message());
30        self::assertSame([$first->declarations()[0], $second->declarations()[0]], $resolution->candidates);
31        self::assertSame('t', $resolution->name->name->value);
32        self::assertSame([$resolution], $query->facts->diagnostics);
33    }
34
35    public function testMessageIsNotReachedWithFewerThanTwoDeclarations(): void
36    {
37        $table = (new Semantics())->inspect('CREATE TABLE t (a INTEGER)')->declarations()[0];
38
39        $this->expectExceptionMessage('A declaration conflict has at least two declarations.');
40
41        new ConflictingTables(new QualifiedName(new Name('t')), [$table]);
42    }
43}
44