packages/ztd-query-postgres/tests/Unit/Schema/Reflection/Catalog/PartitionKeysTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Schema\Reflection\Catalog;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Catalog\PartitionKeys::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Partition\PgSqlPartitionParser::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Partition\BoundPredicate::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Partition\ClauseTokens::class)]
15final class PartitionKeysTest extends TestCase
16{
17    public function testReflectIgnoresMalformedRowsAndKeepsThePartitionExpression(): void
18    {
19        $statement = self::createStub(\ZtdQuery\Connection\StatementInterface::class);
20        $statement->method('fetchAll')->willReturn([['table_name' => 'events', 'partition_key' => 'RANGE (created_at)'], ['table_name' => '', 'partition_key' => 'HASH (id)'], ['table_name' => 'invalid', 'partition_key' => null]]);
21        $queries = [];
22        $connection = self::createMock(\ZtdQuery\Connection\ConnectionInterface::class);
23        $connection->method('query')->willReturnCallback(static function (string $sql) use ($statement, &$queries): \ZtdQuery\Connection\StatementInterface {
24            $queries[] = $sql;
25            return $statement;
26        });
27        $keys = (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Catalog\PartitionKeys($connection))->reflect();
28        self::assertSame(['events'], array_keys($keys));
29        self::assertSame(\ZtdQuery\Schema\Partition\TablePartitionStrategy::Range, $keys['events']->strategy);
30        self::assertSame(['created_at'], $keys['events']->expressions);
31        $connection2 = self::createStub(\ZtdQuery\Connection\ConnectionInterface::class);
32        $connection2->method('query')->willReturn(false);
33        self::assertSame([], (new \ZtdQuery\Platform\Postgres\Schema\Reflection\Catalog\PartitionKeys($connection2))->reflect());
34    }
35}
36