packages/sql-semantics-postgres/tests/Unit/Statement/Utility/Maintenance/ClusterTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Utility\Maintenance;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Table\UndeclaredTable;
12use SqlSemantics\Platform\PostgreSql\Semantics;
13use SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\Cluster;
14
15#[CoversClass(Cluster::class)]
16#[Medium]
17final class ClusterTest extends TestCase
18{
19    public function testDeriveStatementResolvesTheTable(): void
20    {
21        $operation = (new Semantics())->inspect('CLUSTER s.t USING i');
22        self::assertInstanceOf(Cluster::class, $operation->statement);
23        self::assertInstanceOf(UndeclaredTable::class, $operation->facts->relation($operation->statement)->table);
24    }
25
26    public function testDeriveStatementReportsAnOptionValueThatIsNotBoolean(): void
27    {
28        self::assertSame(['verbose requires a Boolean value'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('CLUSTER (VERBOSE 2)')->facts->diagnostics));
29    }
30
31    public function testRenderWritesEachForm(): void
32    {
33        $semantics = new Semantics();
34        self::assertSame(
35            ['CLUSTER', 'CLUSTER VERBOSE', 'CLUSTER VERBOSE i ON s.t', 'CLUSTER (verbose) t USING i', 'CLUSTER (verbose)', 'CLUSTER t'],
36            [
37                $semantics->inspect('CLUSTER')->toString(),
38                $semantics->inspect('CLUSTER VERBOSE')->toString(),
39                $semantics->inspect('CLUSTER VERBOSE i ON s.t')->toString(),
40                $semantics->inspect('CLUSTER (VERBOSE) t USING i')->toString(),
41                $semantics->inspect('CLUSTER (VERBOSE)')->toString(),
42                $semantics->inspect('CLUSTER t')->toString(),
43            ],
44        );
45    }
46
47    public function testDeriveStatementReportsAView(): void
48    {
49        $semantics = new Semantics();
50        $context = [$semantics->inspect('CREATE VIEW v AS SELECT 1 AS a')];
51        self::assertSame(['"v" is not a table or materialized view'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('CLUSTER v', $context)->facts->diagnostics));
52    }
53}
54