packages/ztd-query-postgres/tests/Unit/Session/SchemaInitializerTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Session;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Session\SchemaInitializer::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Relation\FromClause::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Relation\RelationReference::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\PgSqlColumnTypeMapper::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Key\PgSqlForeignKeyDefinitionParser::class)]
15#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\PgSqlIdentifierQuoter::class)]
16#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile::class)]
17#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Partition\PgSqlPartitionParser::class)]
18#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Partition\PgSqlPartitionReflector::class)]
19#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\PgSqlSchemaParser::class)]
20#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\PgSqlSchemaReflector::class)]
21#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Relation\PgSqlSelectRelationParser::class)]
22#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\View\PgSqlViewDefinitionParser::class)]
23#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Catalog\PartitionKeys::class)]
24#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Catalog\TableQueries::class)]
25#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Column\ColumnDefinitionSql::class)]
26#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Column\NativeTypeSql::class)]
27#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\ForeignKeyRow::class)]
28#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\ForeignKeys::class)]
29#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\IndexDefinitions::class)]
30#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\IndexRows::class)]
31#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\PrimaryColumns::class)]
32#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Reflection\Key\UniqueIndexes::class)]
33#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Definition\ColumnDefinition::class)]
34#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Definition\ColumnTypeDeclaration::class)]
35#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Definition\TableBody::class)]
36#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Definition\TableConstraint::class)]
37#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Definition\TableFields::class)]
38#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Key\DefinitionEntry::class)]
39#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Key\DefinitionTokens::class)]
40#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Partition\BoundPredicate::class)]
41#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Partition\ClauseTokens::class)]
42#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::class)]
43final class SchemaInitializerTest extends TestCase
44{
45 public function testPopulateRegistersColumnsPartialIndexesAndPartitionKeys(): void
46 {
47 $statement = self::createStub(\ZtdQuery\Connection\StatementInterface::class);
48 $statement->method('fetchAll')->willReturn([['table_name' => 'users']]);
49 $statement2 = self::createStub(\ZtdQuery\Connection\StatementInterface::class);
50 $statement2->method('fetchAll')->willReturn([['column_name' => 'id', 'data_type' => 'integer', 'is_nullable' => 'NO']]);
51 $statement3 = self::createStub(\ZtdQuery\Connection\StatementInterface::class);
52 $statement3->method('fetchAll')->willReturn([['column_name' => 'id']]);
53 $statement4 = self::createStub(\ZtdQuery\Connection\StatementInterface::class);
54 $statement4->method('fetchAll')->willReturn([['constraint_name' => 'positive_id', 'column_name' => 'id', 'predicate' => 'id > 0']]);
55 $statement5 = self::createStub(\ZtdQuery\Connection\StatementInterface::class);
56 $statement5->method('fetchAll')->willReturn([]);
57 $statement6 = self::createStub(\ZtdQuery\Connection\StatementInterface::class);
58 $statement6->method('fetchAll')->willReturn([['table_name' => 'users', 'partition_key' => 'RANGE (id)']]);
59 $statement7 = self::createStub(\ZtdQuery\Connection\StatementInterface::class);
60 $statement7->method('fetchAll')->willReturn([]);
61 $statementResults = [$statement, $statement2, $statement3, $statement4, $statement5, $statement6, $statement7];
62 $connection = self::createStub(\ZtdQuery\Connection\ConnectionInterface::class);
63 $connection->method('query')->willReturnCallback(static function () use (&$statementResults): \ZtdQuery\Connection\StatementInterface|false {
64 return array_shift($statementResults) ?? false;
65 });
66 $registry = new \ZtdQuery\Schema\TableDefinitionRegistry();
67 (new \ZtdQuery\Platform\Postgres\Session\SchemaInitializer())->populate($connection, new \ZtdQuery\Platform\Postgres\Schema\PgSqlSchemaReflector($connection), new \ZtdQuery\Platform\Postgres\Schema\PgSqlSchemaParser(), $registry);
68 $definition = $registry->get('users');
69 self::assertNotNull($definition);
70 self::assertSame(['id'], $definition->primaryKeys);
71 self::assertSame('id > 0', $definition->partialUniqueIndexes['positive_id']->predicate);
72 self::assertSame(['id'], $definition->partitionKey?->expressions);
73 }
74}
75