packages/sql-semantics-mysql/tests/Unit/Statement/Query/Clause/RowLimitTest.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\Type\Known;
11use SqlSemantics\Platform\MySql\Semantics;
12use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
13use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
14use SqlSemantics\Platform\MySql\Statement\Query\Clause\OffsetSpelling;
15use SqlSemantics\Platform\MySql\Statement\Query\Clause\RowLimit;
16use SqlSemantics\Platform\MySql\Statement\Query\Select;
17
18#[CoversClass(RowLimit::class)]
19#[Medium]
20final class RowLimitTest extends TestCase
21{
22 public function testRenderWritesTheOperandsInTheirSpelling(): void
23 {
24 $semantics = new Semantics();
25
26 self::assertSame('SELECT a FROM t LIMIT 5, 10', $semantics->analyze('select a from t limit 5,10')->toString());
27 self::assertSame('SELECT a FROM t LIMIT 10 OFFSET 5', $semantics->analyze('select a from t limit 10 offset 5')->toString());
28 self::assertSame('SELECT a FROM t LIMIT ?', $semantics->analyze('select a from t limit ?')->toString());
29 }
30
31 public function testOperandsAreTheCountAndTheOffset(): void
32 {
33 $operation = (new Semantics())->analyze('SELECT a FROM t LIMIT 5, 10');
34
35 self::assertInstanceOf(Select::class, $operation->statement);
36 self::assertInstanceOf(RowLimit::class, $operation->statement->limit);
37 self::assertInstanceOf(NumberLiteral::class, $operation->statement->limit->count);
38 self::assertSame('10', $operation->statement->limit->count->text);
39 self::assertSame(OffsetSpelling::Comma, $operation->statement->limit->spelling);
40 self::assertInstanceOf(Known::class, $operation->facts->scalar($operation->statement->limit->count)->type);
41 }
42
43 public function testAnExpressionAsAnOperandIsRejected(): void
44 {
45 $this->expectExceptionMessage('A LIMIT operand is an unsigned integer, a parameter marker or a stored program variable.');
46
47 new RowLimit(new NullLiteral());
48 }
49
50 public function testAnOffsetWithoutSpellingIsRejected(): void
51 {
52 $this->expectExceptionMessage('An offset is written in exactly one spelling.');
53
54 new RowLimit(new NumberLiteral('1'), new NumberLiteral('2'));
55 }
56}
57