packages/sql-semantics-sqlite/tests/Unit/Model/Statement/NodeTest.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\Rendering\Piece;
13use SqlSemantics\Platform\Sqlite\Model\Rendering\PieceKind;
14use SqlSemantics\Platform\Sqlite\Model\Statement\Node;
15use SqlSemantics\Platform\Sqlite\Platform;
16use SqlSemantics\Platform\Sqlite\Semantics;
17use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
18
19#[CoversClass(Node::class)]
20#[Medium]
21final class NodeTest extends TestCase
22{
23    public function testRenderWritesTypedPiecesOnly(): void
24    {
25        $semantics = new Semantics();
26        $output = new Output(Platform::shared()->codec($semantics->profile()));
27
28        $semantics->analyze("select a, 'x' from t")->statement->render($output);
29
30        self::assertSame([PieceKind::Keyword, PieceKind::Name, PieceKind::Symbol, PieceKind::Literal, PieceKind::Keyword, PieceKind::Name], array_map(static fn (Piece $piece): PieceKind => $piece->kind, $output->pieces()));
31        self::assertSame("SELECT a, 'x' FROM t", (new Lexical())->join($output->pieces()));
32    }
33
34    public function testRenderOfAChildIsReachedThroughTheOutput(): void
35    {
36        $semantics = new Semantics();
37        $output = new Output(Platform::shared()->codec($semantics->profile()));
38
39        $statement = $semantics->analyze('SELECT a FROM t WHERE a > 1')->statement;
40
41        self::assertInstanceOf(Select::class, $statement);
42        $output->node($statement->where);
43
44        self::assertSame('a > 1', (new Lexical())->join($output->pieces()));
45    }
46}
47