packages/sql-semantics-sqlite/tests/Unit/Statement/Query/Problem/CallMisuseTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query\Problem;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\Medium;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\Sqlite\Semantics;
13use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\CallMisuse;
14use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\CallMisuseRule;
15
16#[CoversClass(CallMisuse::class)]
17#[Medium]
18final class CallMisuseTest extends TestCase
19{
20    /**
21     * @return array<string, array{CallMisuseRule, string}>
22     */
23    public static function providerMessages(): array
24    {
25        return [
26            'window without over' => [CallMisuseRule::WindowWithoutOver, 'misuse of window function f()'],
27            'scalar as window' => [CallMisuseRule::ScalarAsWindow, 'f() may not be used as a window function'],
28            'filter without aggregate' => [CallMisuseRule::FilterWithoutAggregate, 'FILTER may not be used with non-aggregate f()'],
29            'filter on window only' => [CallMisuseRule::FilterOnWindowOnly, 'FILTER clause may only be used with aggregate window functions'],
30            'order by without aggregate' => [CallMisuseRule::OrderByWithoutAggregate, 'ORDER BY may not be used with non-aggregate f()'],
31            'distinct in window' => [CallMisuseRule::DistinctInWindow, 'DISTINCT is not supported for window functions'],
32            'distinct arguments' => [CallMisuseRule::DistinctArguments, 'DISTINCT aggregates must have exactly one argument'],
33        ];
34    }
35
36    #[DataProvider('providerMessages')]
37    public function testMessageUsesTheWordsOfSqliteForEachRule(CallMisuseRule $rule, string $message): void
38    {
39        $problem = new CallMisuse($rule, new Name('f'));
40
41        self::assertSame($message, $problem->message());
42        self::assertSame($rule, $problem->rule);
43        self::assertSame('f', $problem->function->value);
44    }
45
46    public function testMessageNamesTheFunctionAsWritten(): void
47    {
48        $query = (new Semantics())->inspect('SELECT ABS(1) OVER ()');
49
50        self::assertInstanceOf(CallMisuse::class, $query->facts->diagnostics[0]);
51        self::assertSame('ABS() may not be used as a window function', $query->facts->diagnostics[0]->message());
52    }
53}
54