packages/sql-semantics-mysql/tests/Unit/Statement/Query/QueryStatementTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\MySql\Semantics;
12use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
13use SqlSemantics\Platform\MySql\Statement\Query\Into\IntoPosition;
14use SqlSemantics\Platform\MySql\Statement\Query\Into\IntoVariables;
15use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockingClause;
16use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockStrength;
17use SqlSemantics\Platform\MySql\Statement\Query\ParenthesizedQuery;
18use SqlSemantics\Platform\MySql\Statement\Query\Problem\CountedList;
19use SqlSemantics\Platform\MySql\Statement\Query\Problem\CountMismatch;
20use SqlSemantics\Platform\MySql\Statement\Query\QueryStatement;
21use SqlSemantics\Platform\MySql\Statement\Query\Select;
22use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
23use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
24
25#[CoversClass(QueryStatement::class)]
26#[Medium]
27final class QueryStatementTest extends TestCase
28{
29    public function testDeriveStatementRecordsTheRowsOfTheQuery(): void
30    {
31        $operation = (new Semantics())->inspect('SELECT 1 UNION SELECT 2 FROM t INTO @x');
32
33        self::assertInstanceOf(QueryStatement::class, $operation->statement);
34        self::assertSame($operation->facts->query($operation->statement->query), $operation->facts->output);
35    }
36
37    public function testDeriveQueryDerivesTheTargetsAndReportsTheirCount(): void
38    {
39        $operation = (new Semantics())->inspect('SELECT 1 UNION SELECT 2 FROM t FOR UPDATE INTO @x, @y');
40
41        self::assertInstanceOf(QueryStatement::class, $operation->statement);
42        self::assertSame(IntoPosition::AfterLocking, $operation->statement->intoPosition);
43        self::assertCount(1, $operation->facts->diagnostics);
44        self::assertInstanceOf(CountMismatch::class, $operation->facts->diagnostics[0]);
45        self::assertSame(CountedList::IntoVariables, $operation->facts->diagnostics[0]->list);
46    }
47
48    public function testRenderWritesTheClausesInWrittenOrder(): void
49    {
50        $semantics = new Semantics();
51
52        self::assertSame('SELECT 1 UNION SELECT 2 FROM t INTO @x FOR UPDATE', $semantics->inspect('select 1 union select 2 from t into @x for update')->toString());
53        self::assertInstanceOf(QueryStatement::class, $semantics->inspect('select 1 union select 2 from t into @x for update')->statement);
54        self::assertSame('(SELECT 1) FOR SHARE INTO @x', $semantics->inspect('(select 1) for share into @x')->toString());
55    }
56
57    public function testTheClausesOfASingleBlockAreRejected(): void
58    {
59        $this->expectExceptionMessage('The INTO and locking clauses of a single query block belong to the block.');
60
61        new QueryStatement(new Select([], [new SelectExpression(new NumberLiteral('1'))]), [new LockingClause(LockStrength::Update)]);
62    }
63
64    public function testAnIntoAfterTheSelectListIsRejected(): void
65    {
66        $this->expectExceptionMessage('An INTO after a whole query is written after the query or after its locking clauses.');
67
68        new QueryStatement(new ParenthesizedQuery(new Select([], [new SelectExpression(new NumberLiteral('1'))])), [], new IntoVariables([new UserVariable(new Name('x'))]), IntoPosition::AfterItems);
69    }
70}
71