File: ComparatorTest.php

package info (click to toggle)
php-doctrine-dbal 4.2.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 4,644 kB
  • sloc: php: 46,471; xml: 460; makefile: 22
file content (116 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Schema\PostgreSQL;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\RequiresPhpunit;

#[RequiresPhpunit('< 11.5.18')]
final class ComparatorTest extends FunctionalTestCase
{
    private AbstractSchemaManager $schemaManager;

    private Comparator $comparator;

    protected function setUp(): void
    {
        if (! $this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
            self::markTestSkipped('This test covers PostgreSQL-specific schema comparison scenarios.');
        }

        $this->schemaManager = $this->connection->createSchemaManager();
        $this->comparator    = $this->schemaManager->createComparator();
    }

    /**
     * The PostgreSQL platform maps both BLOB and BINARY columns to the BYTEA column type.
     *
     * @see PostgreSQLPlatform::getBlobTypeDeclarationSQL()
     */
    public function testCompareBinaryAndBlob(): void
    {
        $this->testColumnModification(static function (Table $table, string $name): Column {
            return $table->addColumn($name, Types::BINARY);
        }, static function (Column $column): void {
            $column->setType(Type::getType(Types::BLOB));
        });
    }

    /**
     * The PostgreSQL platform maps both BINARY and VARBINARY columns to the BYTEA column type.
     *
     * @see PostgreSQLPlatform::getVarbinaryTypeDeclarationSQLSnippet()
     */
    public function testCompareBinaryAndVarbinary(): void
    {
        $this->testColumnModification(static function (Table $table, string $name): Column {
            return $table->addColumn($name, Types::BINARY);
        }, static function (Column $column): void {
            $column->setFixed(true);
        });
    }

    /**
     * The PostgreSQL platform disregards the "length" attribute of BINARY and VARBINARY columns.
     *
     * @see PostgreSQLPlatform::getBinaryTypeDeclarationSQLSnippet()
     */
    public function testCompareBinariesOfDifferentLength(): void
    {
        $this->testColumnModification(static function (Table $table, string $name): Column {
            return $table->addColumn($name, Types::BINARY, ['length' => 16]);
        }, static function (Column $column): void {
            $column->setLength(32);
        });
    }

    public function testPlatformOptionsChangedColumnComparison(): void
    {
        $table = new Table('update_json_to_jsonb_table');
        $table->addColumn('test', Types::JSON);

        $onlineTable = clone $table;
        $table->getColumn('test')
            ->setPlatformOption('jsonb', true);

        $compareResult = $this->comparator->compareTables($onlineTable, $table);
        self::assertCount(1, $compareResult->getChangedColumns());
        self::assertCount(1, $compareResult->getModifiedColumns());

        $changedColumn = $compareResult->getChangedColumns()['test'];

        self::assertTrue($changedColumn->hasPlatformOptionsChanged());
        self::assertEquals(1, $changedColumn->countChangedProperties());
    }

    private function testColumnModification(callable $addColumn, callable $modifyColumn): void
    {
        $table  = new Table('comparator_test');
        $column = $addColumn($table, 'id');
        $this->dropAndCreateTable($table);

        $modifyColumn($column);

        self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable(
            $this->schemaManager,
            $this->comparator,
            $table,
        )->isEmpty());

        self::assertTrue(ComparatorTestUtils::diffFromDesiredToActualTable(
            $this->schemaManager,
            $this->comparator,
            $table,
        )->isEmpty());
    }
}