packages/sql-semantics-mysql/tests/Unit/Lowering/Call/FrameRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Call\FrameRule;
11use SqlSemantics\Platform\MySql\Lowering\Lowering;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Call\Window\FrameBoundKind;
16use SqlSemantics\Platform\MySql\Statement\Call\Window\FrameExclusion;
17use SqlSemantics\Platform\MySql\Statement\Call\Window\FrameUnit;
18use SqlSemantics\Platform\MySql\Statement\Expression\IntervalUnit;
19use SqlSemantics\Platform\MySql\Statement\Literal\Parameter;
20
21#[CoversClass(FrameRule::class)]
22#[Small]
23final class FrameRuleTest extends TestCase
24{
25    public function testSpecificationLowersTheParts(): void
26    {
27        $platform = new Platform();
28        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
29        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
30        $node = $platform->parser($profile)->parse('SELECT RANK() OVER (w RANGE BETWEEN INTERVAL 1 DAY PRECEDING AND 2 FOLLOWING EXCLUDE TIES)')->find('window_spec')[0];
31        $rule = new FrameRule($lowering);
32        $specification = $rule->specification($node);
33
34        self::assertSame('w', $specification->base?->value);
35        self::assertSame(FrameUnit::Range, $specification->frame?->unit);
36        self::assertSame(IntervalUnit::Day, $specification->frame->start->unit);
37        self::assertSame(FrameBoundKind::Following, $specification->frame->end?->kind);
38        self::assertSame(FrameExclusion::Ties, $specification->frame->exclusion);
39    }
40
41    public function testBaseLowersNoBase(): void
42    {
43        $platform = new Platform();
44        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
45        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
46        $node = $platform->parser($profile)->parse('SELECT RANK() OVER ()')->find('opt_existing_window_name')[0];
47        $rule = new FrameRule($lowering);
48        self::assertNull($rule->base($node));
49    }
50
51    public function testOrderingLowersNoPartition(): void
52    {
53        $platform = new Platform();
54        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
55        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
56        $node = $platform->parser($profile)->parse('SELECT RANK() OVER ()')->find('opt_partition_clause')[0];
57        $rule = new FrameRule($lowering);
58        self::assertSame([], $rule->ordering($node));
59    }
60
61    public function testFrameLowersNoFrame(): void
62    {
63        $platform = new Platform();
64        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
65        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
66        $node = $platform->parser($profile)->parse('SELECT RANK() OVER ()')->find('opt_window_frame_clause')[0];
67        $rule = new FrameRule($lowering);
68        self::assertNull($rule->frame($node));
69    }
70
71    public function testBoundLowersAParameterOffset(): void
72    {
73        $platform = new Platform();
74        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
75        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
76        $node = $platform->parser($profile)->parse('SELECT RANK() OVER (ROWS ? PRECEDING)')->find('window_frame_start')[0];
77        $rule = new FrameRule($lowering);
78        self::assertInstanceOf(Parameter::class, $rule->bound($node)->offset);
79    }
80}
81