packages/sql-semantics-postgres/tests/Unit/Rules/Invocation/CallChecksTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Platform;
15use SqlSemantics\Platform\PostgreSql\Rules\Invocation\CallChecks;
16use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FunctionCall;
17use SqlSemantics\Platform\PostgreSql\Statement\Invocation\NamedArgument;
18use SqlSemantics\Platform\PostgreSql\Statement\Invocation\PositionalArgument;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\ArgumentProblem;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\ArgumentProblemKind;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\CallMisuse;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\CallMisuseKind;
23use SqlSemantics\Platform\PostgreSql\Statement\Invocation\WindowSpecification;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\BooleanLiteral;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
27use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
28
29#[CoversClass(CallChecks::class)]
30#[Small]
31final class CallChecksTest extends TestCase
32{
33    public function testArgumentsReportsNamingMistakes(): void
34    {
35        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
36        (new CallChecks())->arguments($derivation, [new NamedArgument(new Name('a'), new Constant(new IntegerConstant('1'))), new NamedArgument(new Name('a'), new Constant(new IntegerConstant('1'))), new PositionalArgument(new Constant(new IntegerConstant('1')))]);
37        self::assertEquals([new ArgumentProblem(ArgumentProblemKind::RepeatedName, new Name('a')), new ArgumentProblem(ArgumentProblemKind::PositionalAfterNamed, new Name('a'))], $derivation->facts()->diagnostics);
38    }
39
40    public function testMisuseReportsEveryClauseAPlainFunctionRejects(): void
41    {
42        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
43        $first = (new CallChecks())->misuse($derivation, new FunctionCall(new DottedName([new Name('upper')]), [new PositionalArgument(new Constant(new IntegerConstant('1')))], false, true, false, [], false, new BooleanLiteral(true)), 's');
44        self::assertEquals(new CallMisuse(CallMisuseKind::DistinctOnPlainFunction, new Name('upper')), $first);
45        self::assertCount(2, $derivation->facts()->diagnostics);
46    }
47
48    public function testMisuseReportsAParameterlessAggregateWithoutStar(): void
49    {
50        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
51        $first = (new CallChecks())->misuse($derivation, new FunctionCall(new DottedName([new Name('count')])), 'a');
52        self::assertSame('count(*) must be used to call a parameterless aggregate function', $first?->message());
53    }
54
55    public function testMisuseAcceptsAWindowFunctionWithOver(): void
56    {
57        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
58        self::assertNull((new CallChecks())->misuse($derivation, new FunctionCall(new DottedName([new Name('rank')]), over: new WindowSpecification()), 'w'));
59    }
60}
61