packages/sql-semantics-postgres/tests/Unit/Statement/Expression/GroupingFunctionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
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\Type\Known;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\GroupingFunction;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
23use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
24
25#[CoversClass(GroupingFunction::class)]
26#[Small]
27final class GroupingFunctionTest extends TestCase
28{
29    public function testOutputNameIsGrouping(): void
30    {
31        self::assertSame('grouping', (new GroupingFunction([new NullLiteral()]))->outputName()->value);
32    }
33
34    public function testDeriveScalarIsANonNullInteger(): void
35    {
36        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
37        $fact = $derivation->scalar(new GroupingFunction([new NullLiteral()]), $derivation->environment());
38        self::assertEquals(new Known(Builtin::Int4), $fact->type);
39        self::assertSame(Nullability::NotNull, $fact->nullability);
40    }
41
42    public function testRenderWritesTheArguments(): void
43    {
44        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
45        (new GroupingFunction([new Constant(new IntegerConstant('1')), new NullLiteral()]))->render($out);
46        self::assertSame('GROUPING (1, NULL)', (new Lexical())->join($out->pieces()));
47    }
48
49    public function testRejectsNoArgument(): void
50    {
51        $this->expectExceptionMessage('GROUPING takes at least one expression.');
52        new GroupingFunction([]);
53    }
54}
55