packages/sql-semantics-mysql/tests/Unit/SemanticsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\AnalysisContext;
11use SqlSemantics\AnalysisException;
12use SqlSemantics\Analyzer;
13use SqlSemantics\Platform\MySql\Model\Diagnostic\SemanticException;
14use SqlSemantics\Platform\MySql\Model\Statement\Operation;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Query;
18
19#[CoversClass(Semantics::class)]
20#[Medium]
21final class SemanticsTest extends TestCase
22{
23    /**
24     * @throws AnalysisException
25     */
26    public function testAnalyzeReturnsTheDatabaseOperationThroughTheCommonInterface(): void
27    {
28        $semantics = new Semantics();
29        $table = $semantics->analyze('CREATE TABLE t (id INTEGER)');
30        $context = $semantics->context([$table]);
31        $analyze = static fn (Analyzer $analyzer): Query => $analyzer->analyze('select id from t', $context);
32        $query = $analyze($semantics);
33
34        self::assertInstanceOf(Operation::class, $query);
35        self::assertSame('SELECT id FROM t', $query->toString());
36        self::assertSame($table->declarations()[0]->columns[0], $query->field('id')->column());
37        self::assertSame($context, $query->context);
38    }
39
40    public function testLeavesMissingInputsInAnOpenContextAsFacts(): void
41    {
42        $query = (new Semantics())->analyze('SELECT id FROM t');
43
44        $type = $query->field(0)->type;
45        self::assertInstanceOf(Dependent::class, $type);
46        self::assertSame('the declaration of relation t', $type->missing[0]->describe());
47        self::assertSame([], $query->facts->diagnostics);
48    }
49
50    public function testThrowsAnAnalysisExceptionForSqlOutsideTheGrammar(): void
51    {
52        $this->expectException(AnalysisException::class);
53
54        (new Semantics())->analyze('SELECT FROM WHERE');
55    }
56
57    public function testThrowsAnExceptionWithTheSemanticDiagnostics(): void
58    {
59        $semantics = new Semantics();
60        $table = $semantics->analyze('CREATE TABLE t (id INTEGER)');
61
62        try {
63            $semantics->analyze('SELECT missing FROM t', [$table]);
64            self::fail('Expected a semantic error.');
65        } catch (AnalysisException $error) {
66            self::assertInstanceOf(SemanticException::class, $error);
67            self::assertStringContainsString('missing', $error->getMessage());
68            self::assertStringContainsString('missing', $error->diagnostics[0]->message());
69        }
70    }
71
72    public function testRejectsAContextOfAnotherImplementation(): void
73    {
74        $this->expectExceptionMessage('The context must belong to the selected database.');
75
76        (new Semantics())->analyze('SELECT 1', new class () implements AnalysisContext {
77        });
78    }
79
80    public function testAnalyzeAllThrowsWhenAnyStatementHasASemanticError(): void
81    {
82        $this->expectException(SemanticException::class);
83
84        (new Semantics())->analyzeAll('CREATE TABLE t (id INTEGER); SELECT id FROM t', []);
85    }
86
87    public function testProfileIsFixedForAnAnalyzer(): void
88    {
89        $semantics = new Semantics();
90
91        self::assertSame($semantics->profile(), $semantics->profile());
92    }
93
94    public function testContextPreparesDeclarationsAndChecksPreparedContexts(): void
95    {
96        $semantics = new Semantics();
97        $declaration = $semantics->analyze('CREATE TABLE t (id INTEGER)');
98        $context = $semantics->context([$declaration]);
99
100        self::assertSame($declaration->declarations(), $context->tables);
101        self::assertSame($context, $semantics->context($context));
102    }
103
104    public function testSplitFindsTheStatementBoundaries(): void
105    {
106        $semantics = new Semantics();
107
108        self::assertSame(['SELECT 1;', ' SELECT 2;'], $semantics->split('SELECT 1; SELECT 2;'));
109    }
110
111    public function testInspectRetainsDiagnosticsForStatementValidation(): void
112    {
113        $operation = (new Semantics())->inspect('SELECT * FROM t', []);
114
115        self::assertSame('SELECT * FROM t', $operation->toString());
116        self::assertNotEmpty($operation->facts->diagnostics);
117    }
118}
119