packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Reference/Column/MissingColumnTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement\Reference\Column;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
11use SqlSemantics\Platform\Sqlite\Semantics;
12
13#[CoversClass(MissingColumn::class)]
14#[Medium]
15final class MissingColumnTest extends TestCase
16{
17    public function testMessageNamesTheUnqualifiedColumn(): void
18    {
19        $semantics = new Semantics();
20        $table = $semantics->inspect('CREATE TABLE t (a INTEGER)');
21
22        $resolution = $semantics->inspect('SELECT b FROM t', [$table])->field('b')->resolution;
23
24        self::assertInstanceOf(MissingColumn::class, $resolution);
25        self::assertSame('Column b does not exist.', $resolution->message());
26        self::assertSame('b', $resolution->name->value);
27        self::assertNull($resolution->qualifier);
28    }
29
30    public function testMessageNamesTheQualifierTheUseWrote(): void
31    {
32        $semantics = new Semantics();
33        $table = $semantics->inspect('CREATE TABLE t (a INTEGER)');
34
35        $resolution = $semantics->inspect('SELECT t.b FROM t', [$table])->field('b')->resolution;
36
37        self::assertInstanceOf(MissingColumn::class, $resolution);
38        self::assertSame('Column t.b does not exist.', $resolution->message());
39        self::assertSame('t', $resolution->qualifier?->name->value);
40    }
41
42    public function testMessageIsAlsoADiagnosticOfTheOperation(): void
43    {
44        $semantics = new Semantics();
45        $table = $semantics->inspect('CREATE TABLE t (a INTEGER)');
46
47        $query = $semantics->inspect('SELECT b FROM t', [$table]);
48
49        self::assertSame([$query->field('b')->resolution], $query->facts->diagnostics);
50    }
51}
52