packages/sql-semantics-postgres/tests/Unit/Statement/Routine/Signature/AggregateArgumentsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Signature;
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\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
19use SqlSemantics\Platform\PostgreSql\Statement\Routine\FunctionParameter;
20use SqlSemantics\Platform\PostgreSql\Statement\Routine\ParameterMode;
21use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblem;
22use SqlSemantics\Platform\PostgreSql\Statement\Routine\Problem\RoutineProblemKind;
23use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\AggregateArguments;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
26
27#[CoversClass(AggregateArguments::class)]
28#[Small]
29final class AggregateArgumentsTest extends TestCase
30{
31 public function testStarIsTheEmptyListWithoutOrderBy(): void
32 {
33 self::assertSame([true, false], [(new AggregateArguments([]))->star(), (new AggregateArguments([], [new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))))]))->star()]);
34 }
35
36 public function testDeriveClauseReportsOutputArguments(): void
37 {
38 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
39 (new AggregateArguments([new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))), null, ParameterMode::Out)]))->deriveClause($derivation, $derivation->environment());
40 self::assertEquals([new RoutineProblem(RoutineProblemKind::AggregateOutput)], $derivation->facts()->diagnostics);
41 }
42
43 public function testRenderWritesTheFourForms(): void
44 {
45 $star = new Output(new Codec(GrammarRelease::PostgreSql172));
46 (new AggregateArguments([]))->render($star);
47 $ordered = new Output(new Codec(GrammarRelease::PostgreSql172));
48 (new AggregateArguments([], [new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))))]))->render($ordered);
49 $both = new Output(new Codec(GrammarRelease::PostgreSql172));
50 (new AggregateArguments([new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('text')]))))], [new FunctionParameter(new TypeName(new NamedDesignation(new DottedName([new Name('int4')]))))]))->render($both);
51 self::assertSame(['(*)', '(ORDER BY int4)', '(text ORDER BY int4)'], [(new Lexical())->join($star->pieces()), (new Lexical())->join($ordered->pieces()), (new Lexical())->join($both->pieces())]);
52 }
53
54 public function testRejectsAnEmptyOrderBy(): void
55 {
56 $this->expectExceptionMessage('ORDER BY is followed by at least one aggregate argument.');
57 new AggregateArguments([], []);
58 }
59}
60