packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Fact/DiagnosticTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement\Fact;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\Diagnostic;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\MissingTable;
13use SqlSemantics\Platform\Sqlite\Semantics;
14
15#[CoversClass(Diagnostic::class)]
16#[Medium]
17final class DiagnosticTest extends TestCase
18{
19    public function testMessageDescribesEachProblemInDerivationOrder(): void
20    {
21        $operation = (new Semantics())->inspect('SELECT b FROM t', []);
22
23        self::assertSame(['Relation t does not exist.', 'Column b does not exist.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
24        self::assertInstanceOf(MissingTable::class, $operation->facts->diagnostics[0]);
25        self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[1]);
26    }
27
28    public function testMessageIsNotNeededForAStatementWithoutProblems(): void
29    {
30        $semantics = new Semantics();
31        $table = $semantics->inspect('CREATE TABLE t (b INTEGER)');
32
33        self::assertSame([], $semantics->inspect('SELECT b FROM t', [$table])->facts->diagnostics);
34    }
35}
36