packages/sql-semantics-sqlite/tests/Unit/Statement/Inspection/ExplainTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Inspection;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\MissingTable;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Inspection\Explain;
15use SqlSemantics\Platform\Sqlite\Statement\Inspection\ExplainMode;
16use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateView;
17use SqlSemantics\Platform\Sqlite\Statement\Schema\Limit\DefaultNotConstant;
18use SqlSemantics\Platform\Sqlite\Statement\Transaction\Begin;
19
20#[CoversClass(Explain::class)]
21#[Medium]
22final class ExplainTest extends TestCase
23{
24    public function testDeriveStatementReturnsTheRowsOfTheReportAndNotThoseOfTheQuery(): void
25    {
26        $operation = (new Semantics())->inspect('EXPLAIN SELECT 1 AS one');
27
28        self::assertCount(8, $operation->fields() ?? []);
29        self::assertSame('opcode', $operation->field(1)->name?->value);
30    }
31
32    public function testDeriveStatementDerivesTheWrappedStatement(): void
33    {
34        $operation = (new Semantics())->inspect('EXPLAIN QUERY PLAN SELECT a FROM missing', []);
35
36        self::assertCount(4, $operation->fields() ?? []);
37        self::assertInstanceOf(MissingTable::class, $operation->facts->diagnostics[0]);
38    }
39
40    public function testDeriveStatementReportsOnAStatementThatReturnsNoRows(): void
41    {
42        $operation = (new Semantics())->inspect('EXPLAIN BEGIN');
43
44        self::assertCount(8, $operation->fields() ?? []);
45    }
46
47    public function testDeriveStatementDiscardsTheRowsAndTheDeclarationOfTheInspectedStatement(): void
48    {
49        $semantics = new Semantics();
50        $table = $semantics->inspect('CREATE TABLE t (a INTEGER)');
51        $returning = $semantics->inspect('EXPLAIN INSERT INTO t (a) VALUES (1) RETURNING a, b', [$table]);
52        $create = $semantics->inspect('EXPLAIN QUERY PLAN CREATE TABLE u (a, b DEFAULT (?))');
53        $view = $semantics->inspect('EXPLAIN CREATE VIEW v AS SELECT a FROM t', [$table]);
54
55        $report = $returning->fields();
56        self::assertNotNull($report);
57        self::assertSame(['addr', 'opcode', 'p1', 'p2', 'p3', 'p4', 'p5', 'comment'], array_map(static fn (object $field): ?string => $field->name?->value, iterator_to_array($report)));
58        self::assertInstanceOf(MissingColumn::class, $returning->facts->diagnostics[0]);
59        self::assertSame([], $create->declarations());
60        $plan = $create->fields();
61        self::assertNotNull($plan);
62        self::assertSame(['id', 'parent', 'notused', 'detail'], array_map(static fn (object $field): ?string => $field->name?->value, iterator_to_array($plan)));
63        self::assertInstanceOf(DefaultNotConstant::class, $create->facts->diagnostics[0]);
64        self::assertSame([], $view->declarations());
65        $explain = $view->statement;
66        self::assertInstanceOf(Explain::class, $explain);
67        $definition = $explain->statement;
68        self::assertInstanceOf(CreateView::class, $definition);
69        $inner = $view->facts->query($definition->query)->fields();
70        self::assertNotNull($inner);
71        self::assertSame($table->declarations()[0]->columns[0], $inner->at(0)->column());
72    }
73
74    public function testRenderWritesThePrefixOfEachMode(): void
75    {
76        $semantics = new Semantics();
77
78        self::assertSame('EXPLAIN SELECT 1', $semantics->inspect('explain select 1')->toString());
79        self::assertSame('EXPLAIN QUERY PLAN SELECT 1', $semantics->inspect('explain query plan select 1')->toString());
80    }
81
82    public function testRenderWritesANewlyBuiltRequest(): void
83    {
84        $operation = new Operation((new Semantics())->context(), new Explain(ExplainMode::QueryPlan, new Begin()));
85
86        self::assertSame('EXPLAIN QUERY PLAN BEGIN', $operation->toString());
87    }
88}
89