packages/sql-semantics-mysql/tests/Unit/Rules/Query/QueryTailsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\MySql\Model\Rendering\Output;
12use SqlSemantics\Platform\MySql\Model\Rendering\Piece;
13use SqlSemantics\Platform\MySql\Rendering\Codec;
14use SqlSemantics\Platform\MySql\Rules\Query\QueryTails;
15use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
16use SqlSemantics\Platform\MySql\Statement\Query\Clause\OutputOrdinal;
17use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
18use SqlSemantics\Platform\MySql\Statement\Query\OrderItem;
19use SqlSemantics\Platform\MySql\Statement\Query\ParenthesizedQuery;
20use SqlSemantics\Platform\MySql\Statement\Query\QueryExpression;
21use SqlSemantics\Platform\MySql\Statement\Query\Select;
22use SqlSemantics\Platform\MySql\Statement\Query\SelectExpression;
23use SqlSemantics\Platform\MySql\Statement\Query\Set\LeadingUnion;
24use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperation;
25use SqlSemantics\Platform\MySql\Statement\Query\Set\SetOperator;
26use SqlSemantics\Platform\MySql\Statement\Query\Set\SetQuantifier;
27
28#[CoversClass(QueryTails::class)]
29#[Small]
30final class QueryTailsTest extends TestCase
31{
32 public function testOperationWritesTheOperatorTheQuantifierAndTheRightOperand(): void
33 {
34 $left = new Select([], [new SelectExpression(new NumberLiteral('1'))]);
35 $right = new Select([], [new SelectExpression(new NumberLiteral('2'))]);
36 $out = new Output(new Codec(GrammarRelease::MySql847));
37 (new QueryTails())->operation(new SetOperation($left, SetOperator::Union, SetQuantifier::All, $right), $out);
38
39 self::assertSame(['UNION', 'ALL', 'SELECT', '2'], array_map(static fn (Piece $piece): string => $piece->text, $out->pieces()));
40 }
41
42 public function testExpressionWritesTheOrderingAndTheLimitOnly(): void
43 {
44 $body = new ParenthesizedQuery(new Select([], [new SelectExpression(new NumberLiteral('1'))]));
45 $out = new Output(new Codec(GrammarRelease::MySql847));
46 (new QueryTails())->expression(new QueryExpression(null, $body, [new OrderItem(new OutputOrdinal(new NumberLiteral('1')))], new RowLimit(new NumberLiteral('3'))), $out);
47
48 self::assertSame(['ORDER', 'BY', '1', 'LIMIT', '3'], array_map(static fn (Piece $piece): string => $piece->text, $out->pieces()));
49 }
50
51 public function testLeadingWritesUnionTheQuantifierAndTheLastOperand(): void
52 {
53 $limited = new Select([], [new SelectExpression(new NumberLiteral('2'))], null, null, null, null, [], null, [], new RowLimit(new NumberLiteral('1')));
54 $out = new Output(new Codec(GrammarRelease::MySql5651));
55 (new QueryTails())->leading(new LeadingUnion(new Select([], [new SelectExpression(new NumberLiteral('1'))]), SetQuantifier::Distinct, $limited), $out);
56
57 self::assertSame(['UNION', 'DISTINCT', 'SELECT', '2', 'LIMIT', '1'], array_map(static fn (Piece $piece): string => $piece->text, $out->pieces()));
58 }
59}
60