packages/sql-semantics-sqlite/tests/Unit/Lowering/Query/QueryCommandsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Parser\Node;
11use SqlSemantics\Platform\Sqlite\Lowering\Lowering;
12use SqlSemantics\Platform\Sqlite\Lowering\Query\QueryCommands;
13use SqlSemantics\Platform\Sqlite\Model\Contract\ParameterStyle;
14use SqlSemantics\Platform\Sqlite\Model\Lowering\Leaves;
15use SqlSemantics\Platform\Sqlite\Model\Lowering\Lists;
16use SqlSemantics\Platform\Sqlite\Platform;
17use SqlSemantics\Platform\Sqlite\Semantics;
18use SqlSemantics\Platform\Sqlite\Statement\Mutation\Delete;
19use SqlSemantics\Platform\Sqlite\Statement\Mutation\InsertRows;
20use SqlSemantics\Platform\Sqlite\Statement\Mutation\Update;
21use SqlSemantics\Platform\Sqlite\Statement\Query\Compound;
22use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
23use SqlSemantics\Platform\Sqlite\Statement\Query\ValuesClause;
24use SqlSemantics\Platform\Sqlite\Statement\Query\With\WithQuery;
25use SqlSemantics\Platform\Sqlite\Statement\Trigger\CreateTrigger;
26
27#[CoversClass(QueryCommands::class)]
28#[Medium]
29final class QueryCommandsTest extends TestCase
30{
31    public function testCommandLowersEveryFormOfASelectCommand(): void
32    {
33        $semantics = new Semantics();
34
35        self::assertInstanceOf(Select::class, $semantics->analyze('SELECT 1')->statement);
36        self::assertInstanceOf(ValuesClause::class, $semantics->analyze('VALUES (1)')->statement);
37        self::assertInstanceOf(Compound::class, $semantics->analyze('SELECT 1 UNION SELECT 2')->statement);
38        self::assertInstanceOf(WithQuery::class, $semantics->analyze('WITH c AS (SELECT 1) SELECT * FROM c')->statement);
39    }
40
41    public function testCommandLowersACreateTriggerCommand(): void
42    {
43        $operation = (new Semantics())->analyze('CREATE TRIGGER tr AFTER INSERT ON t BEGIN SELECT 1; END');
44
45        self::assertInstanceOf(CreateTrigger::class, $operation->statement);
46        self::assertSame('CREATE TRIGGER tr AFTER INSERT ON t BEGIN SELECT 1; END', $operation->toString());
47    }
48
49    public function testCommandPassesDataChangesToTheMutationRules(): void
50    {
51        $semantics = new Semantics();
52
53        self::assertInstanceOf(InsertRows::class, $semantics->analyze('INSERT INTO t VALUES (1)')->statement);
54        self::assertInstanceOf(Update::class, $semantics->analyze('UPDATE t SET a = 1')->statement);
55        self::assertInstanceOf(Delete::class, $semantics->analyze('DELETE FROM t')->statement);
56    }
57
58    public function testCommandAnswersNullForACommandOfAnotherFamily(): void
59    {
60        $platform = new Platform();
61        $profile = $platform->profile(null, null, ParameterStyle::Native);
62        $lowering = new Lowering($platform->productions($profile), new Leaves());
63        $tree = $platform->parser($profile)->parse('CREATE TABLE t (a); SELECT 1');
64        $list = $tree->children[0];
65
66        self::assertInstanceOf(Node::class, $list);
67        $commands = (new Lists())->items($list);
68        $definition = $commands[0]->children[0];
69        $query = $commands[1]->children[0];
70        self::assertInstanceOf(Node::class, $definition);
71        self::assertInstanceOf(Node::class, $query);
72        $lowering->trivia->index($commands[1]);
73        self::assertNull($lowering->queryCommands->command($lowering->productions->form($lowering->unwrapped($definition))));
74        self::assertInstanceOf(Select::class, $lowering->queryCommands->command($lowering->productions->form($lowering->unwrapped($query))));
75    }
76}
77