File: ComparatorTest.php

package info (click to toggle)
php-doctrine-dbal 4.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,092 kB
  • sloc: php: 60,293; xml: 618; makefile: 23
file content (137 lines) | stat: -rw-r--r-- 4,559 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?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\ColumnEditor;
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\Types;

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 (ColumnEditor $editor): void {
            $editor->setTypeName(Types::BINARY);
        }, static function (ColumnEditor $editor): void {
            $editor->setTypeName(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 (ColumnEditor $editor): void {
            $editor->setTypeName(Types::BINARY);
        }, static function (ColumnEditor $editor): void {
            $editor->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 (ColumnEditor $editor): void {
            $editor
                ->setTypeName(Types::BINARY)
                ->setLength(16);
        }, static function (ColumnEditor $editor): void {
            $editor->setLength(32);
        });
    }

    public function testPlatformOptionsChangedColumnComparison(): void
    {
        $table = Table::editor()
            ->setUnquotedName('update_json_to_jsonb_table')
            ->setColumns(
                Column::editor()
                    ->setUnquotedName('test')
                    ->setTypeName(Types::JSON)
                    ->create(),
            )
            ->create();

        $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());
    }

    /**
     * @param callable(ColumnEditor): void $initializeColumn
     * @param callable(ColumnEditor): void $modifyColumn
     */
    private function testColumnModification(callable $initializeColumn, callable $modifyColumn): void
    {
        $editor = Column::editor()
            ->setUnquotedName('id');

        $initializeColumn($editor);

        $table = Table::editor()
            ->setUnquotedName('comparator_test')
            ->setColumns($editor->create())
            ->create();

        $this->dropAndCreateTable($table);

        $table = $table->edit()
            ->modifyColumnByUnquotedName('id', $modifyColumn)
            ->create();

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

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