packages/sql-semantics-mysql/tests/Unit/Statement/Call/Aggregate/AggregateTest.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\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
19use SqlSemantics\Platform\MySql\Statement\Call\Aggregate\Aggregate;
20use SqlSemantics\Platform\MySql\Statement\Call\Aggregate\AggregateFunction;
21use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowSpec;
22use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
23use SqlSemantics\Platform\MySql\Statement\Query\OrderItem;
24
25#[CoversClass(Aggregate::class)]
26#[Small]
27final class AggregateTest extends TestCase
28{
29    public function testStarTellsCountOfAllRows(): void
30    {
31        self::assertTrue((new Aggregate(AggregateFunction::Count, []))->star());
32        self::assertFalse((new Aggregate(AggregateFunction::Count, [new NumberLiteral('1')]))->star());
33    }
34
35    public function testAggregatesTellsWhetherTheCallHasNoWindow(): void
36    {
37        self::assertTrue((new Aggregate(AggregateFunction::Sum, [new NumberLiteral('1')]))->aggregates());
38        self::assertFalse((new Aggregate(AggregateFunction::Sum, [new NumberLiteral('1')], over: new WindowSpec()))->aggregates());
39    }
40
41    public function testDeriveScalarDerivesTheWindow(): void
42    {
43        $platform = new Platform();
44        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], false));
45        $order = new NumberLiteral('1');
46        $fact = $derivation->scalar(new Aggregate(AggregateFunction::Average, [new NumberLiteral('1')], over: new WindowSpec(null, [], [new OrderItem($order)])), $derivation->environment());
47
48        self::assertEquals(new Known(TypeClass::Decimal->descriptor()), $fact->type);
49        self::assertSame(Nullability::NotNull, $derivation->facts()->scalar($order)->nullability);
50    }
51
52    public function testRenderWritesTheQuantifiersAndTheWindow(): void
53    {
54        $platform = new Platform();
55        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
56        $out = new Output($platform->codec($profile));
57        (new Aggregate(AggregateFunction::Count, [], false, true, new Name('w')))->render($out);
58
59        self::assertSame('COUNT(ALL *) OVER w', (new Lexical())->join($out->pieces()));
60    }
61
62}
63