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 156 157 158
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Platforms\MySQL;
use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider;
use Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider;
use Doctrine\DBAL\Platforms\MySQL\Comparator;
use Doctrine\DBAL\Platforms\MySQL\DefaultTableOptions;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ComparatorConfig;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use function sprintf;
class MariaDBJsonComparatorTest extends TestCase
{
protected Comparator $comparator;
/** @var Table[] */
private array $tables = [];
protected function setUp(): void
{
$this->comparator = new Comparator(
new MariaDBPlatform(),
new class implements CharsetMetadataProvider {
public function getDefaultCharsetCollation(string $charset): ?string
{
return null;
}
},
new class implements CollationMetadataProvider {
public function getCollationCharset(string $collation): ?string
{
return null;
}
},
new DefaultTableOptions('utf8mb4', 'utf8mb4_general_ci'),
new ComparatorConfig(),
);
// TableA has collation set at table level and various column collations
$this->tables['A'] = Table::editor()
->setUnquotedName('foo')
->setColumns(
Column::editor()
->setUnquotedName('json_1')
->setTypeName(Types::JSON)
->setCollation('latin1_swedish_ci')
->create(),
Column::editor()
->setUnquotedName('json_2')
->setTypeName(Types::JSON)
->setCollation('utf8_general_ci')
->create(),
Column::editor()
->setUnquotedName('json_3')
->setTypeName(Types::JSON)
->create(),
)
->setOptions([
'charset' => 'latin1',
'collation' => 'latin1_swedish_ci',
])
->create();
// TableB has no table-level collation and various column collations
$this->tables['B'] = Table::editor()
->setUnquotedName('foo')
->setColumns(
Column::editor()
->setUnquotedName('json_1')
->setTypeName(Types::JSON)
->setCollation('latin1_swedish_ci')
->create(),
Column::editor()
->setUnquotedName('json_2')
->setTypeName(Types::JSON)
->setCollation('utf8_general_ci')
->create(),
Column::editor()
->setUnquotedName('json_3')
->setTypeName(Types::JSON)
->create(),
)
->create();
// Table C has no table-level collation and column collations as MariaDb would return for columns declared
// as JSON
$this->tables['C'] = Table::editor()
->setUnquotedName('foo')
->setColumns(
Column::editor()
->setUnquotedName('json_1')
->setTypeName(Types::JSON)
->setCollation('utf8mb4_bin')
->create(),
Column::editor()
->setUnquotedName('json_2')
->setTypeName(Types::JSON)
->setCollation('utf8mb4_bin')
->create(),
Column::editor()
->setUnquotedName('json_3')
->setTypeName(Types::JSON)
->setCollation('utf8mb4_bin')
->create(),
)
->create();
// Table D has no table or column collations set
$this->tables['D'] = Table::editor()
->setUnquotedName('foo')
->setColumns(
Column::editor()
->setUnquotedName('json_1')
->setTypeName(Types::JSON)
->create(),
Column::editor()
->setUnquotedName('json_2')
->setTypeName(Types::JSON)
->create(),
Column::editor()
->setUnquotedName('json_3')
->setTypeName(Types::JSON)
->create(),
)
->create();
}
/** @return array{string, string}[] */
public static function providerTableComparisons(): iterable
{
return [
['A', 'B'],
['A', 'C'],
['A', 'D'],
['B', 'C'],
['B', 'D'],
['C', 'D'],
];
}
#[DataProvider('providerTableComparisons')]
public function testJsonColumnComparison(string $table1, string $table2): void
{
self::assertTrue(
$this->comparator->compareTables($this->tables[$table1], $this->tables[$table2])->isEmpty(),
sprintf('Tables %s and %s should be identical', $table1, $table2),
);
}
}
|