packages/sql-semantics-mysql/tests/Unit/Statement/Query/Clause/WindowDefinitionTest.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\Semantics;
11use SqlSemantics\Platform\MySql\Statement\Query\Clause\WindowDefinition;
12use SqlSemantics\Platform\MySql\Statement\Query\Problem\Misuse;
13use SqlSemantics\Platform\MySql\Statement\Query\Problem\MisuseRule;
14use SqlSemantics\Platform\MySql\Statement\Query\Select;
15
16#[CoversClass(WindowDefinition::class)]
17#[Medium]
18final class WindowDefinitionTest extends TestCase
19{
20    public function testRenderWritesTheNameAndTheWindow(): void
21    {
22        $operation = (new Semantics())->inspect('select a from t window w as (), v as (w)');
23
24        self::assertSame('SELECT a FROM t WINDOW w AS (), v AS (w)', $operation->toString());
25        self::assertInstanceOf(Select::class, $operation->statement);
26        self::assertSame('v', $operation->statement->windows[1]->name->value);
27    }
28
29    public function testANameDefinedTwiceIsReported(): void
30    {
31        $operation = (new Semantics())->inspect('SELECT a FROM t WINDOW w AS (), W AS ()');
32
33        self::assertCount(1, $operation->facts->diagnostics);
34        self::assertInstanceOf(Misuse::class, $operation->facts->diagnostics[0]);
35        self::assertSame(MisuseRule::DuplicateWindow, $operation->facts->diagnostics[0]->rule);
36    }
37}
38