packages/sql-semantics-mysql/tests/Unit/Statement/Call/Aggregate/GroupConcatTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Call\Aggregate;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
18use SqlSemantics\Platform\MySql\Statement\Call\Aggregate\GroupConcat;
19use SqlSemantics\Platform\MySql\Statement\Call\Problem\UnsupportedWindowing;
20use SqlSemantics\Platform\MySql\Statement\Call\Problem\WindowingLimit;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
23use SqlSemantics\Platform\MySql\Statement\Literal\Text;
24use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
25use SqlSemantics\Platform\MySql\Statement\Query\Direction;
26use SqlSemantics\Platform\MySql\Statement\Query\OrderItem;
27
28#[CoversClass(GroupConcat::class)]
29#[Small]
30final class GroupConcatTest extends TestCase
31{
32    public function testAggregatesTellsWhetherTheCallHasNoWindow(): void
33    {
34        self::assertTrue((new GroupConcat([new NumberLiteral('1')]))->aggregates());
35    }
36
37    public function testDeriveScalarReportsAWindow(): void
38    {
39        $platform = new Platform();
40        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], false));
41        $fact = $derivation->scalar(new GroupConcat([new StringLiteral(['x'])], false, [], null, new Name('w')), $derivation->environment());
42
43        self::assertEquals(new Known(TypeClass::Character->descriptor()), $fact->type);
44        self::assertEquals([new UnsupportedWindowing(WindowingLimit::GroupConcat)], $derivation->facts()->diagnostics);
45    }
46
47    public function testRenderWritesTheOrderingAndTheSeparator(): void
48    {
49        $platform = new Platform();
50        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
51        $out = new Output($platform->codec($profile));
52        (new GroupConcat([new ColumnUse(new Name('a'))], true, [new OrderItem(new ColumnUse(new Name('a')), Direction::Descending)], new Text(';')))->render($out);
53
54        self::assertSame("GROUP_CONCAT(DISTINCT a ORDER BY a DESC SEPARATOR ';')", (new Lexical())->join($out->pieces()));
55    }
56}
57