packages/sql-semantics-mysql/tests/Unit/Statement/Query/Clause/LateOrderingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query\Clause;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\ResolvedColumn;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Expression\Subquery\ScalarSubquery;
17use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
18use SqlSemantics\Platform\MySql\Statement\Query\Clause\LateOrdering;
19use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
20use SqlSemantics\Platform\MySql\Statement\Query\Select;
21use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
22use SqlSemantics\Platform\MySql\Statement\Type\Integral;
23use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
24
25#[CoversClass(LateOrdering::class)]
26#[Medium]
27final class LateOrderingTest extends TestCase
28{
29 public function testRenderWritesTheOrderingAfterTheLockingClausesOfTheBlock(): void
30 {
31 $semantics = new Semantics('mysql-5.6.51');
32 $table = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [new Column(new Name('a'), new Integral(IntegralKind::Int)), new Column(new Name('b'), new Integral(IntegralKind::Int))]);
33 $operation = $semantics->analyze('select (select a from t for update order by b limit 1) as v', [$table]);
34 $select = $operation->statement;
35 self::assertInstanceOf(Select::class, $select);
36 $item = $select->items[0];
37 self::assertInstanceOf(SelectExpression::class, $item);
38 $subquery = $item->expression;
39 self::assertInstanceOf(ScalarSubquery::class, $subquery);
40 $block = $subquery->query;
41 self::assertInstanceOf(Select::class, $block);
42 $late = $block->late;
43 self::assertNotNull($late);
44 $resolution = $operation->facts->scalar($late->orderBy[0]->expression)->resolution;
45
46 self::assertSame('SELECT (SELECT a FROM t FOR UPDATE ORDER BY b LIMIT 1) AS v', $operation->toString());
47 self::assertSame([], $block->orderBy);
48 self::assertNull($block->limit);
49 self::assertInstanceOf(ResolvedColumn::class, $resolution);
50 self::assertSame($table->columns[1], $resolution->declaration());
51 self::assertSame([], $operation->facts->diagnostics);
52 }
53
54 public function testRenderWritesALimitThatReplacesTheLimitOfTheBlock(): void
55 {
56 $operation = (new Semantics('mysql-5.6.51'))->analyze('SELECT (SELECT 1 LIMIT 1 LIMIT 2)', []);
57
58 self::assertSame('SELECT (SELECT 1 LIMIT 1 LIMIT 2)', $operation->toString());
59 self::assertNotNull((new LateOrdering([], new RowLimit(new NumberLiteral('2'))))->limit);
60 }
61
62 public function testRejectsALateOrderingWithoutClauses(): void
63 {
64 $this->expectExceptionMessage('A late ordering holds an ORDER BY or a LIMIT.');
65
66 new LateOrdering([]);
67 }
68}
69