packages/sql-semantics-mysql/tests/Unit/Statement/Query/Clause/OutputOrdinalTest.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\AliasTarget;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Semantics;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
19use SqlSemantics\Platform\MySql\Statement\Query\Clause\OutputOrdinal;
20use SqlSemantics\Platform\MySql\Statement\Query\Problem\OrdinalOutOfRange;
21use SqlSemantics\Platform\MySql\Statement\Query\Select;
22use SqlSemantics\Platform\MySql\Statement\Type\Integral;
23use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
24
25#[CoversClass(OutputOrdinal::class)]
26#[Medium]
27final class OutputOrdinalTest extends TestCase
28{
29 public function testPositionAnswersTheWrittenPosition(): void
30 {
31 self::assertSame(2, (new OutputOrdinal(new NumberLiteral('2')))->position());
32 self::assertSame(3, (new OutputOrdinal(new NumberLiteral('003')))->position());
33 self::assertSame(PHP_INT_MAX, (new OutputOrdinal(new NumberLiteral('9999999999999999999')))->position());
34 }
35
36 public function testDeriveScalarTakesTheFactsOfTheField(): void
37 {
38 $semantics = new Semantics();
39 $table = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [
40 new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::Nullable),
41 ]);
42 $operation = $semantics->inspect('SELECT 1, a FROM t GROUP BY 2 ORDER BY 2', [$table]);
43
44 self::assertInstanceOf(Select::class, $operation->statement);
45 $fact = $operation->facts->scalar($operation->statement->orderBy[0]->expression);
46 self::assertInstanceOf(AliasTarget::class, $fact->resolution);
47 self::assertSame($operation->field(1), $fact->resolution->field);
48 self::assertSame(Nullability::Nullable, $fact->nullability);
49 self::assertSame([], $operation->facts->diagnostics);
50 }
51
52 public function testDeriveScalarReportsAPositionOutsideTheSelectList(): void
53 {
54 $operation = (new Semantics())->inspect('SELECT 1 ORDER BY 3');
55
56 self::assertCount(1, $operation->facts->diagnostics);
57 self::assertInstanceOf(OrdinalOutOfRange::class, $operation->facts->diagnostics[0]);
58 }
59
60 public function testDeriveScalarDependsOnAnOpenStar(): void
61 {
62 $operation = (new Semantics())->inspect('SELECT * FROM t ORDER BY 2');
63
64 self::assertInstanceOf(Select::class, $operation->statement);
65 self::assertInstanceOf(Dependent::class, $operation->facts->scalar($operation->statement->orderBy[0]->expression)->type);
66 self::assertSame([], $operation->facts->diagnostics);
67 }
68
69 public function testRenderWritesTheInteger(): void
70 {
71 self::assertSame('SELECT a FROM t ORDER BY 1 DESC', (new Semantics())->inspect('select a from t order by 1 desc')->toString());
72 }
73
74 public function testADecimalIsRejected(): void
75 {
76 $this->expectExceptionMessage('A select list position is an unsigned integer.');
77
78 new OutputOrdinal(new NumberLiteral('1.5'));
79 }
80}
81