packages/sql-semantics-mysql/tests/Unit/Lowering/Query/Shared/BlockTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Query\Shared;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Query\Shared\Block;
11use SqlSemantics\Platform\MySql\Lowering\Query\Shared\Trailer;
12use SqlSemantics\Platform\MySql\Model\Diagnostic\AnalysisException;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
15use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
16use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
17use SqlSemantics\Platform\MySql\Statement\Query\Into\IntoPosition;
18use SqlSemantics\Platform\MySql\Statement\Query\Into\IntoVariables;
19use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockingClause;
20use SqlSemantics\Platform\MySql\Statement\Query\Locking\LockStrength;
21use SqlSemantics\Platform\MySql\Statement\Query\OrderItem;
22use SqlSemantics\Platform\MySql\Statement\Query\QueryExpression;
23use SqlSemantics\Platform\MySql\Statement\Query\Select;
24use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
25use SqlSemantics\Platform\MySql\Statement\Relation\Dual;
26use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
27
28#[CoversClass(Block::class)]
29#[Small]
30final class BlockTest extends TestCase
31{
32 public function testThenAddsTheClausesWrittenAfterTheBlock(): void
33 {
34 $limit = new RowLimit(new NumberLiteral('1'));
35 $block = (new Block([], [new SelectExpression(new NumberLiteral('1'))]))->then(new Trailer([], $limit));
36
37 self::assertSame($limit, $block->trailer->limit);
38 }
39
40 public function testFinishOrdersTheRowsOfABlockThatLimitsItsRows(): void
41 {
42 $block = (new Block([], [new SelectExpression(new NumberLiteral('1'))]))->then(new Trailer([], new RowLimit(new NumberLiteral('1'))));
43 $query = $block->finish(new Trailer([new OrderItem(new ColumnUse(new Name('a')))]));
44
45 self::assertInstanceOf(QueryExpression::class, $query);
46 self::assertInstanceOf(Select::class, $query->body);
47 self::assertCount(1, $query->orderBy);
48 }
49
50 public function testFinishKeepsALaterOrderingAfterTheLockingClausesAsTheBlocksOwn(): void
51 {
52 $block = (new Block([], [new SelectExpression(new NumberLiteral('1'))], null, new Dual()))->then(new Trailer([], null, null, [new LockingClause(LockStrength::Update)]));
53 $query = $block->finish(new Trailer([new OrderItem(new ColumnUse(new Name('a')))]));
54
55 self::assertInstanceOf(Select::class, $query);
56 self::assertSame([], $query->orderBy);
57 self::assertNotNull($query->late);
58 self::assertCount(1, $query->late->orderBy);
59 }
60
61 public function testFinishKeepsASecondLimitAsTheLimitThatReplacesTheFirst(): void
62 {
63 $first = new RowLimit(new NumberLiteral('1'));
64 $second = new RowLimit(new NumberLiteral('2'));
65 $query = (new Block([], [new SelectExpression(new NumberLiteral('1'))]))->then(new Trailer([], $first))->finish(new Trailer([], $second));
66
67 self::assertInstanceOf(Select::class, $query);
68 self::assertSame($first, $query->limit);
69 self::assertSame($second, $query->late?->limit);
70 }
71
72 public function testFinishWritesClausesInPlaceWhenNothingOfTheBlockPrecedesThem(): void
73 {
74 $limit = new RowLimit(new NumberLiteral('2'));
75 $query = (new Block([], [new SelectExpression(new NumberLiteral('1'))]))->finish(new Trailer([], $limit));
76
77 self::assertInstanceOf(Select::class, $query);
78 self::assertSame($limit, $query->limit);
79 self::assertNull($query->late);
80 }
81
82 public function testBareDropsTheClausesWrittenAfterTheBlock(): void
83 {
84 $block = (new Block([], [new SelectExpression(new NumberLiteral('1'))]))->then(new Trailer([], new RowLimit(new NumberLiteral('1'))));
85
86 self::assertTrue($block->bare()->trailer->empty());
87 }
88
89 public function testSelectReadsATrailingIntoWithoutClausesAsTheIntoAfterTheSelectList(): void
90 {
91 $into = new IntoVariables([new UserVariable(new Name('x'))]);
92 $select = (new Block([], [new SelectExpression(new NumberLiteral('1'))]))->then(new Trailer([], null, null, [], $into, IntoPosition::AfterQuery))->select();
93
94 self::assertSame($into, $select->into);
95 self::assertSame(IntoPosition::AfterItems, $select->intoPosition);
96 }
97
98 public function testSelectRejectsTwoIntoClauses(): void
99 {
100 $block = new Block([], [new SelectExpression(new NumberLiteral('1'))], new IntoVariables([new UserVariable(new Name('x'))]), new Dual(), null, null, null, [], null, new Trailer([], null, null, [], new IntoVariables([new UserVariable(new Name('y'))]), IntoPosition::AfterQuery));
101
102 $this->expectException(AnalysisException::class);
103
104 $block->select();
105 }
106}
107