packages/ztd-query-postgres/tests/Unit/Schema/Definition/ColumnDefinitionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Schema\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Schema\Definition\ColumnDefinition::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\PgSqlColumnTypeMapper::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Definition\ColumnTypeDeclaration::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Schema\Definition\TableConstraint::class)]
15final class ColumnDefinitionTest extends TestCase
16{
17    public function testParseColumnDefinition(): void
18    {
19        self::assertEquals(['name' => 'id', 'type' => 'BIGINT', 'columnType' => new \ZtdQuery\Schema\ColumnDeclaration(family: \ZtdQuery\Schema\ColumnTypeFamily::INTEGER, nativeType: 'BIGINT'), 'notNull' => false, 'primaryKey' => true, 'unique' => false, 'default' => null, 'identity' => true, 'generatedExpression' => null], (new \ZtdQuery\Platform\Postgres\Schema\Definition\ColumnDefinition())->parseColumnDefinition('id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY'));
20    }
21
22    public function testIsSequenceDefault(): void
23    {
24        self::assertSame(true, \ZtdQuery\Platform\Postgres\Schema\Definition\ColumnDefinition::isSequenceDefault("nextval('users_id_seq'::regclass)"));
25    }
26
27    public function testIsSerialType(): void
28    {
29        self::assertSame(true, \ZtdQuery\Platform\Postgres\Schema\Definition\ColumnDefinition::isSerialType('BIGSERIAL'));
30    }
31
32    public function testHasGeneratedIdentity(): void
33    {
34        self::assertSame(true, \ZtdQuery\Platform\Postgres\Schema\Definition\ColumnDefinition::hasGeneratedIdentity('GENERATED BY DEFAULT AS IDENTITY NOT NULL'));
35    }
36    public function testGeneratedExpressionExcludesIdentityColumns(): void
37    {
38        $column = new \ZtdQuery\Platform\Postgres\Schema\Definition\ColumnDefinition();
39        self::assertSame('(a + b)', $column->generatedExpression('GENERATED ALWAYS AS (a + b) STORED', false));
40        self::assertNull($column->generatedExpression('GENERATED ALWAYS AS IDENTITY', true));
41    }
42}
43