packages/sql-semantics-mysql/tests/Unit/Statement/Call/Window/WindowSpecTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Call\Window;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Call\Problem\UnsupportedWindowing;
18use SqlSemantics\Platform\MySql\Statement\Call\Problem\WindowingLimit;
19use SqlSemantics\Platform\MySql\Statement\Call\Window\Frame;
20use SqlSemantics\Platform\MySql\Statement\Call\Window\FrameBound;
21use SqlSemantics\Platform\MySql\Statement\Call\Window\FrameBoundKind;
22use SqlSemantics\Platform\MySql\Statement\Call\Window\FrameExclusion;
23use SqlSemantics\Platform\MySql\Statement\Call\Window\FrameUnit;
24use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowSpec;
25use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
26use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
27use SqlSemantics\Platform\MySql\Statement\Query\Direction;
28use SqlSemantics\Platform\MySql\Statement\Query\OrderItem;
29
30#[CoversClass(WindowSpec::class)]
31#[Small]
32final class WindowSpecTest extends TestCase
33{
34 public function testDeriveWindowReportsWhatTheServerDoesNotSupport(): void
35 {
36 $platform = new Platform();
37 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], false));
38 $offset = new NumberLiteral('1');
39 (new WindowSpec(null, [], [], new Frame(FrameUnit::Groups, new FrameBound(FrameBoundKind::Preceding, $offset), null, FrameExclusion::Ties)))->deriveWindow($derivation, $derivation->environment());
40
41 self::assertSame(Nullability::NotNull, $derivation->facts()->scalar($offset)->nullability);
42 self::assertEquals([new UnsupportedWindowing(WindowingLimit::GroupsUnit), new UnsupportedWindowing(WindowingLimit::Exclusion)], $derivation->facts()->diagnostics);
43 }
44
45 public function testRenderWritesTheParts(): void
46 {
47 $platform = new Platform();
48 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
49 $out = new Output($platform->codec($profile));
50 (new WindowSpec(new Name('w'), [new OrderItem(new ColumnUse(new Name('a')))], [new OrderItem(new ColumnUse(new Name('a')), Direction::Ascending)], new Frame(FrameUnit::Rows, new FrameBound(FrameBoundKind::UnboundedPreceding))))->render($out);
51
52 self::assertSame('(w PARTITION BY a ORDER BY a ASC ROWS UNBOUNDED PRECEDING)', (new Lexical())->join($out->pieces()));
53 }
54}
55