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 159 160 161 162 163 164 165
|
<?php
namespace Doctrine\DBAL\Tests\Functional\Schema\MySQL;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\AbstractPlatform;
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\Types;
final class ComparatorTest extends FunctionalTestCase
{
private AbstractPlatform $platform;
private AbstractSchemaManager $schemaManager;
private Comparator $comparator;
protected function setUp(): void
{
$this->platform = $this->connection->getDatabasePlatform();
if (! $this->platform instanceof AbstractMySQLPlatform) {
self::markTestSkipped();
}
$this->schemaManager = $this->connection->createSchemaManager();
$this->comparator = $this->schemaManager->createComparator();
}
/** @dataProvider lobColumnProvider */
public function testLobLengthIncrementWithinLimit(string $type, int $length): void
{
$table = $this->createLobTable($type, $length - 1);
$this->increaseLobLength($table);
self::assertFalse(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
));
self::assertFalse(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
));
}
/** @dataProvider lobColumnProvider */
public function testLobLengthIncrementOverLimit(string $type, int $length): void
{
$table = $this->createLobTable($type, $length);
$this->increaseLobLength($table);
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
/** @return iterable<array{string,int}> */
public static function lobColumnProvider(): iterable
{
yield [Types::BLOB, AbstractMySQLPlatform::LENGTH_LIMIT_TINYBLOB];
yield [Types::BLOB, AbstractMySQLPlatform::LENGTH_LIMIT_BLOB];
yield [Types::BLOB, AbstractMySQLPlatform::LENGTH_LIMIT_MEDIUMBLOB];
yield [Types::TEXT, AbstractMySQLPlatform::LENGTH_LIMIT_TINYTEXT];
yield [Types::TEXT, AbstractMySQLPlatform::LENGTH_LIMIT_TEXT];
yield [Types::TEXT, AbstractMySQLPlatform::LENGTH_LIMIT_MEDIUMTEXT];
}
/** @throws Exception */
private function createLobTable(string $type, int $length): Table
{
$table = new Table('comparator_test');
$table->addColumn('lob', $type)->setLength($length);
$this->dropAndCreateTable($table);
return $table;
}
/** @throws Exception */
private function increaseLobLength(Table $table): void
{
$column = $table->getColumn('lob');
$column->setLength($column->getLength() + 1);
}
public function testExplicitDefaultCollation(): void
{
[$table, $column] = $this->createCollationTable();
$column->setPlatformOption('collation', 'utf8mb4_general_ci');
self::assertFalse(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
));
self::assertFalse(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
));
}
public function testChangeColumnCharsetAndCollation(): void
{
[$table, $column] = $this->createCollationTable();
$column->setPlatformOption('charset', 'latin1');
$column->setPlatformOption('collation', 'latin1_bin');
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
public function testChangeColumnCollation(): void
{
[$table, $column] = $this->createCollationTable();
$column->setPlatformOption('collation', 'utf8mb4_bin');
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
public function testImplicitColumnCharset(): void
{
$table = new Table('comparator_test');
$table->addColumn('name', Types::STRING, [
'length' => 32,
'platformOptions' => ['collation' => 'utf8mb4_unicode_ci'],
]);
$this->dropAndCreateTable($table);
self::assertFalse(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
));
self::assertFalse(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
));
}
/**
* @return array{Table,Column}
*
* @throws Exception
*/
private function createCollationTable(): array
{
$table = new Table('comparator_test');
$table->addOption('charset', 'utf8mb4');
$table->addOption('collation', 'utf8mb4_general_ci');
$column = $table->addColumn('id', Types::STRING);
$this->dropAndCreateTable($table);
return [$table, $column];
}
}
|