File: JsonCollationTest.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 (155 lines) | stat: -rw-r--r-- 5,516 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php

declare(strict_types=1);

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

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MariaDBPlatform;
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\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;
use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;

use function array_filter;

/**
 * Tests that character set and collation are ignored for columns declared as native JSON in MySQL and
 * MariaDb and cannot be changed.
 */
final class JsonCollationTest extends FunctionalTestCase
{
    private AbstractPlatform $platform;

    private AbstractSchemaManager $schemaManager;

    private Comparator $comparator;

    protected function setUp(): void
    {
        $this->platform = $this->connection->getDatabasePlatform();

        if (! $this->platform instanceof MariaDBPlatform) {
            self::markTestSkipped();
        }

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

    /**
     * Generates a number of tables comprising only json columns. The tables are identical but for character
     * set and collation.
     *
     * @return Iterator<array{Table}>
     */
    public static function tableProvider(): iterable
    {
        $tables = [
            [
                'name' => 'mariadb_json_column_comparator_test',
                'columns' => [
                    ['name' => 'json_1', 'charset' => 'latin1', 'collation' => 'latin1_swedish_ci'],
                    ['name' => 'json_2', 'charset' => 'utf8', 'collation' => 'utf8_general_ci'],
                    ['name' => 'json_3'],
                ],
                'charset' => 'latin1',
                'collation' => 'latin1_swedish_ci',
            ],
            [
                'name' => 'mariadb_json_column_comparator_test',
                'columns' => [
                    ['name' => 'json_1', 'charset' => 'latin1', 'collation' => 'latin1_swedish_ci'],
                    ['name' => 'json_2', 'charset' => 'utf8', 'collation' => 'utf8_general_ci'],
                    ['name' => 'json_3'],
                ],
            ],
            [
                'name' => 'mariadb_json_column_comparator_test',
                'columns' => [
                    ['name' => 'json_1', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_bin'],
                    ['name' => 'json_2', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_bin'],
                    ['name' => 'json_3', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci'],
                ],
            ],
            [
                'name' => 'mariadb_json_column_comparator_test',
                'columns' => [
                    ['name' => 'json_1'],
                    ['name' => 'json_2'],
                    ['name' => 'json_3'],
                ],
            ],
        ];

        foreach ($tables as $table) {
            yield [self::setUpTable(
                $table['name'],
                $table['columns'],
                $table['charset'] ?? null,
                $table['collation'] ?? null,
            ),
            ];
        }
    }

    /**
     * @param non-empty-string                                                                         $name
     * @param array{name: non-empty-string, non-empty-string?: string, collation?: non-empty-string}[] $columns
     */
    private static function setUpTable(
        string $name,
        array $columns,
        ?string $charset = null,
        ?string $collation = null,
    ): Table {
        $tableOptions = array_filter(['charset' => $charset, 'collation' => $collation]);

        $editor = Table::editor()
            ->setUnquotedName($name)
            ->setOptions($tableOptions);

        foreach ($columns as $column) {
            $editor->addColumn(
                Column::editor()
                    ->setUnquotedName($column['name'])
                    ->setTypeName(Types::JSON)
                    ->setCharset($column['charset'] ?? null)
                    ->setCollation($column['collation'] ?? null)
                    ->create(),
            );
        }

        return $editor->create();
    }

    #[DataProvider('tableProvider')]
    public function testJsonColumnComparison(Table $originalTable): void
    {
        $this->dropAndCreateTable($originalTable);

        $onlineTable = $this->schemaManager->introspectTable('mariadb_json_column_comparator_test');
        $diff        = $this->comparator->compareTables($originalTable, $onlineTable);

        self::assertTrue($diff->isEmpty(), 'Tables should be identical.');

        $table = $originalTable->edit()
            ->modifyColumnByUnquotedName('json_1', static function (ColumnEditor $editor): void {
                $editor
                    ->setCharset('utf8')
                    ->setCollation('utf8_general_ci');
            })
            ->create();

        $diff = $this->comparator->compareTables($table, $onlineTable);
        self::assertTrue($diff->isEmpty(), 'Tables should be unchanged after attempted collation change.');

        $diff = $this->comparator->compareTables($table, $originalTable);
        self::assertTrue($diff->isEmpty(), 'Tables should be unchanged after attempted collation change.');
    }
}