packages/sql-semantics-mysql/tests/Unit/Statement/Call/Window/WindowFunctionTest.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\Dependent;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Statement\Call\Window\CountingEdge;
19use SqlSemantics\Platform\MySql\Statement\Call\Window\NullTreatment;
20use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowFunction;
21use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowFunctionKind;
22use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowSpec;
23use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
24use SqlSemantics\Platform\MySql\Statement\Literal\Parameter;
25use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
26
27#[CoversClass(WindowFunction::class)]
28#[Small]
29final class WindowFunctionTest extends TestCase
30{
31 public function testDeriveScalarDerivesTheArgumentsAndTheWindow(): void
32 {
33 $platform = new Platform();
34 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
35 $derivation = new Derivation($platform->context($profile, null, [], false));
36 $fact = $derivation->scalar(new WindowFunction(WindowFunctionKind::Lag, [new Parameter('?'), new Parameter('?')], new WindowSpec()), $derivation->environment());
37
38 self::assertInstanceOf(Dependent::class, $fact->type);
39 self::assertSame(Nullability::Nullable, $fact->nullability);
40 }
41
42 public function testRenderWritesTheOptionsBeforeTheWindow(): void
43 {
44 $platform = new Platform();
45 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
46 $out = new Output($platform->codec($profile));
47 (new WindowFunction(WindowFunctionKind::NthValue, [new ColumnUse(new Name('a')), new NumberLiteral('2')], new Name('w'), NullTreatment::Respect, CountingEdge::First))->render($out);
48
49 self::assertSame('NTH_VALUE(a, 2) FROM FIRST RESPECT NULLS OVER w', (new Lexical())->join($out->pieces()));
50 }
51
52
53}
54