packages/sql-semantics-sqlite/tests/Unit/Model/Statement/ScriptTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Rendering\Lexical;
11use SqlSemantics\Platform\Sqlite\Model\Rendering\Output;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\MissingColumn;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Table\MissingTable;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Script;
15use SqlSemantics\Platform\Sqlite\Platform;
16use SqlSemantics\Platform\Sqlite\Semantics;
17
18#[CoversClass(Script::class)]
19#[Medium]
20final class ScriptTest extends TestCase
21{
22 public function testDeriveStatementDerivesEveryMemberAgainstTheSameContext(): void
23 {
24 $operation = (new Semantics())->inspect('CREATE TABLE t (a INTEGER); INSERT INTO t (a) VALUES (1)', []);
25
26 self::assertInstanceOf(Script::class, $operation->statement);
27 self::assertCount(2, $operation->statement->statements);
28 self::assertCount(1, $operation->declarations());
29 self::assertCount(2, $operation->facts->diagnostics);
30 self::assertInstanceOf(MissingTable::class, $operation->facts->diagnostics[0]);
31 self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[1]);
32 }
33
34 public function testDeriveStatementCollectsTheDeclarationsOfEveryMember(): void
35 {
36 $operation = (new Semantics())->inspect('CREATE TABLE a (x INTEGER); CREATE TABLE b (y TEXT)');
37
38 self::assertSame(['a', 'b'], array_map(static fn ($table): string => $table->name->name->value, $operation->declarations()));
39 }
40
41 public function testDeriveStatementOfAnEmptyInputHasNoMembers(): void
42 {
43 $semantics = new Semantics();
44
45 $operation = $semantics->inspect('');
46
47 self::assertInstanceOf(Script::class, $operation->statement);
48 self::assertSame([], $operation->statement->statements);
49 self::assertSame('', $operation->toString());
50 self::assertNull($operation->shape());
51 }
52
53 public function testRenderSeparatesTheMembersWithTerminators(): void
54 {
55 $semantics = new Semantics();
56 $output = new Output(Platform::shared()->codec($semantics->profile()));
57 $operation = $semantics->inspect('select 1 ; delete from t ;');
58
59 $operation->statement->render($output);
60
61 self::assertSame('SELECT 1; DELETE FROM t', (new Lexical())->join($output->pieces()));
62 self::assertSame('SELECT 1; DELETE FROM t', $operation->toString());
63 }
64
65 public function testRenderIsNotReachedForAScriptOfExactlyOneStatement(): void
66 {
67 $statement = (new Semantics())->inspect('SELECT 1')->statement;
68
69 $this->expectExceptionMessage('A script holds no statement or several; one statement is its own root.');
70
71 new Script([$statement]);
72 }
73
74 public function testRenderIsNotReachedForANestedScript(): void
75 {
76 $script = (new Semantics())->inspect('DELETE FROM t; DELETE FROM u')->statement;
77
78 self::assertInstanceOf(Script::class, $script);
79 $this->expectExceptionMessage('A script does not nest.');
80
81 new Script([$script, $script]);
82 }
83}
84