diff --git a/UPGRADE.md b/UPGRADE.md index 1f1f72c130b..61f1470cef5 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -340,6 +340,19 @@ all drivers and middleware. # Upgrade to 4.3 +## Deprecated `Column` methods + +The following `Column` methods have been deprecated: + +- `Column::getPlatformOptions()`, `Column::hasPlatformOption()`, `Column::getPlatformOption()` – use + `Column::getCharset()`, `Column::getCollation()`, `Column::getMinimumValue()` and `Column::getMaximumValue()` + instead. + +Additionally, +1. Extending the `Column` class has been deprecated. Use the `Column` class directly. +2. The `Column` constructor has been marked as internal. Use `Column::editor()` to instantiate an + editor and `ColumnEditor::create()` to create a column. + ## The `jsonb` column platform option has been deprecated The `jsonb` column platform option has been deprecated. To define a `JSONB` column, use the `JSONB` type instead. diff --git a/src/Platforms/MySQL/CharsetMetadataProvider.php b/src/Platforms/MySQL/CharsetMetadataProvider.php index 665543e0d0a..305a7e36314 100644 --- a/src/Platforms/MySQL/CharsetMetadataProvider.php +++ b/src/Platforms/MySQL/CharsetMetadataProvider.php @@ -7,5 +7,6 @@ /** @internal */ interface CharsetMetadataProvider { + /** @return ?non-empty-string */ public function getDefaultCharsetCollation(string $charset): ?string; } diff --git a/src/Platforms/MySQL/CharsetMetadataProvider/CachingCharsetMetadataProvider.php b/src/Platforms/MySQL/CharsetMetadataProvider/CachingCharsetMetadataProvider.php index dadc8419d54..07d4f9cac5a 100644 --- a/src/Platforms/MySQL/CharsetMetadataProvider/CachingCharsetMetadataProvider.php +++ b/src/Platforms/MySQL/CharsetMetadataProvider/CachingCharsetMetadataProvider.php @@ -11,7 +11,7 @@ /** @internal */ final class CachingCharsetMetadataProvider implements CharsetMetadataProvider { - /** @var array */ + /** @var array */ private array $cache = []; public function __construct(private readonly CharsetMetadataProvider $charsetMetadataProvider) diff --git a/src/Platforms/MySQL/CollationMetadataProvider.php b/src/Platforms/MySQL/CollationMetadataProvider.php index d52ca74a2b2..028edf9dbcf 100644 --- a/src/Platforms/MySQL/CollationMetadataProvider.php +++ b/src/Platforms/MySQL/CollationMetadataProvider.php @@ -7,5 +7,10 @@ /** @internal */ interface CollationMetadataProvider { + /** + * @param non-empty-string $collation + * + * @return ?non-empty-string + */ public function getCollationCharset(string $collation): ?string; } diff --git a/src/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProvider.php b/src/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProvider.php index 0c99aa31366..4b23894e3ac 100644 --- a/src/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProvider.php +++ b/src/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProvider.php @@ -11,7 +11,7 @@ /** @internal */ final class CachingCollationMetadataProvider implements CollationMetadataProvider { - /** @var array */ + /** @var array */ private array $cache = []; public function __construct(private readonly CollationMetadataProvider $collationMetadataProvider) diff --git a/src/Schema/Column.php b/src/Schema/Column.php index ef78eaf6e3a..2335f014f39 100644 --- a/src/Schema/Column.php +++ b/src/Schema/Column.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Schema; +use Doctrine\DBAL\Platforms\SQLServerPlatform; use Doctrine\DBAL\Schema\Exception\UnknownColumnOption; use Doctrine\DBAL\Schema\Name\Parser\UnqualifiedNameParser; use Doctrine\DBAL\Schema\Name\Parsers; @@ -16,6 +17,7 @@ /** * Object representation of a database column. * + * @final * @extends AbstractNamedObject * @phpstan-type ColumnProperties = array{ * name: UnqualifiedName, @@ -23,15 +25,15 @@ * default: mixed, * notnull?: bool, * autoincrement: bool, - * columnDefinition: ?string, + * columnDefinition: ?non-empty-string, * comment: string, - * charset?: ?string, - * collation?: ?string, + * charset?: ?non-empty-string, + * collation?: ?non-empty-string, * } * @phpstan-type PlatformOptions = array{ - * charset?: ?string, - * collation?: ?string, - * default_constraint_name?: string, + * charset?: ?non-empty-string, + * collation?: ?non-empty-string, + * default_constraint_name?: non-empty-string, * } */ class Column extends AbstractNamedObject @@ -60,12 +62,14 @@ class Column extends AbstractNamedObject /** @var PlatformOptions */ protected array $_platformOptions = []; + /** @var ?non-empty-string */ protected ?string $_columnDefinition = null; protected string $_comment = ''; /** - * Creates a new Column. + * @internal Use {@link Column::editor()} to instantiate an editor and {@link ColumnEditor::create()} to create a + * column. * * @param array $options */ @@ -170,6 +174,7 @@ public function setPlatformOption(string $name, mixed $value): self return $this; } + /** @param ?non-empty-string $value */ public function setColumnDefinition(?string $value): self { $this->_columnDefinition = $value; @@ -217,19 +222,82 @@ public function getDefault(): mixed return $this->_default; } - /** @return PlatformOptions */ + /** + * Returns the name of the character set to use with the column. + * + * @return ?non-empty-string + */ + public function getCharset(): ?string + { + return $this->_platformOptions['charset'] ?? null; + } + + /** + * Returns the name of the collation to use with the column. + * + * @return ?non-empty-string + */ + public function getCollation(): ?string + { + return $this->_platformOptions['collation'] ?? null; + } + + /** + * Returns the minimum value to enforce on the column. + */ + public function getMinimumValue(): mixed + { + return $this->_platformOptions['min'] ?? null; + } + + /** + * Returns the maximum value to enforce on the column. + */ + public function getMaximumValue(): mixed + { + return $this->_platformOptions['max'] ?? null; + } + + /** + * @internal Should be used only from within the {@see AbstractSchemaManager} class hierarchy. + * + * Returns the name of the DEFAULT constraint that implements the default value for the column on SQL Server. + * + * @return ?non-empty-string + */ + public function getDefaultConstraintName(): ?string + { + return $this->_platformOptions[SQLServerPlatform::OPTION_DEFAULT_CONSTRAINT_NAME] ?? null; + } + + /** + * @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()} + * instead. + * + * @return PlatformOptions + */ public function getPlatformOptions(): array { return $this->_platformOptions; } - /** @param key-of $name */ + /** + * @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()} + * instead. + * + * @param key-of $name + */ public function hasPlatformOption(string $name): bool { return isset($this->_platformOptions[$name]); } - /** @param key-of $name */ + /** + * @deprecated Use {@see getCharset()}, {@see getCollation()}, {@see getMinimumValue()} or {@see getMaximumValue()} + * instead. + * + * @param key-of $name + */ public function getPlatformOption(string $name): mixed { /** @phpstan-ignore offsetAccess.notFound */ @@ -302,4 +370,32 @@ public function toArray(): array 'values' => $this->_values, ], $this->_platformOptions); } + + public static function editor(): ColumnEditor + { + return new ColumnEditor(); + } + + public function edit(): ColumnEditor + { + return self::editor() + ->setName($this->getObjectName()) + ->setType($this->_type) + ->setLength($this->_length) + ->setPrecision($this->_precision) + ->setScale($this->_scale) + ->setUnsigned($this->_unsigned) + ->setFixed($this->_fixed) + ->setNotNull($this->_notnull) + ->setDefaultValue($this->_default) + ->setAutoincrement($this->_autoincrement) + ->setComment($this->_comment) + ->setValues($this->_values) + ->setColumnDefinition($this->_columnDefinition) + ->setCharset($this->getCharset()) + ->setCollation($this->getCollation()) + ->setMinimumValue($this->getMinimumValue()) + ->setMaximumValue($this->getMaximumValue()) + ->setDefaultConstraintName($this->getDefaultConstraintName()); + } } diff --git a/src/Schema/ColumnEditor.php b/src/Schema/ColumnEditor.php new file mode 100644 index 00000000000..0e2e9e0e411 --- /dev/null +++ b/src/Schema/ColumnEditor.php @@ -0,0 +1,271 @@ + */ + private array $values = []; + + /** @var ?non-empty-string */ + private ?string $charset = null; + + /** @var ?non-empty-string */ + private ?string $collation = null; + + /** @var ?non-empty-string */ + private ?string $defaultConstraintName = null; + + /** @var ?non-empty-string */ + private ?string $columnDefinition = null; + + /** @internal Use {@link Column::editor()} or {@link Column::edit()} to create an instance */ + public function __construct() + { + } + + public function setName(UnqualifiedName $name): self + { + $this->name = $name; + + return $this; + } + + /** @param non-empty-string $name */ + public function setUnquotedName(string $name): self + { + $this->name = UnqualifiedName::unquoted($name); + + return $this; + } + + /** @param non-empty-string $name */ + public function setQuotedName(string $name): self + { + $this->name = UnqualifiedName::quoted($name); + + return $this; + } + + public function setType(Type $type): self + { + $this->type = $type; + + return $this; + } + + /** @throws TypesException */ + public function setTypeName(string $typeName): self + { + $this->type = Type::getType($typeName); + + return $this; + } + + public function setLength(?int $length): self + { + $this->length = $length; + + return $this; + } + + public function setPrecision(?int $precision): self + { + $this->precision = $precision; + + return $this; + } + + public function setScale(int $scale): self + { + $this->scale = $scale; + + return $this; + } + + public function setUnsigned(bool $unsigned): self + { + $this->unsigned = $unsigned; + + return $this; + } + + public function setFixed(bool $fixed): self + { + $this->fixed = $fixed; + + return $this; + } + + public function setNotNull(bool $notNull): self + { + $this->notNull = $notNull; + + return $this; + } + + public function setDefaultValue(mixed $defaultValue): self + { + $this->defaultValue = $defaultValue; + + return $this; + } + + public function setMinimumValue(mixed $minimumValue): self + { + $this->minimumValue = $minimumValue; + + return $this; + } + + public function setMaximumValue(mixed $maximumValue): self + { + $this->maximumValue = $maximumValue; + + return $this; + } + + public function setAutoincrement(bool $flag): self + { + $this->autoincrement = $flag; + + return $this; + } + + public function setComment(string $comment): self + { + $this->comment = $comment; + + return $this; + } + + /** @param list $values */ + public function setValues(array $values): self + { + $this->values = $values; + + return $this; + } + + /** @param ?non-empty-string $charset */ + public function setCharset(?string $charset): self + { + $this->charset = $charset; + + return $this; + } + + /** @param ?non-empty-string $collation */ + public function setCollation(?string $collation): self + { + $this->collation = $collation; + + return $this; + } + + /** + * @internal Should be used only from within the {@see AbstractSchemaManager} class hierarchy. + * + * @param ?non-empty-string $defaultConstraintName + */ + public function setDefaultConstraintName(?string $defaultConstraintName): self + { + $this->defaultConstraintName = $defaultConstraintName; + + return $this; + } + + /** @param ?non-empty-string $columnDefinition */ + public function setColumnDefinition(?string $columnDefinition): self + { + $this->columnDefinition = $columnDefinition; + + return $this; + } + + public function create(): Column + { + if ($this->name === null) { + throw InvalidColumnDefinition::nameNotSpecified(); + } + + if ($this->type === null) { + throw InvalidColumnDefinition::dataTypeNotSpecified($this->name); + } + + $platformOptions = []; + + if ($this->charset !== null) { + $platformOptions['charset'] = $this->charset; + } + + if ($this->collation !== null) { + $platformOptions['collation'] = $this->collation; + } + + if ($this->minimumValue !== null) { + $platformOptions['min'] = $this->minimumValue; + } + + if ($this->maximumValue !== null) { + $platformOptions['max'] = $this->maximumValue; + } + + if ($this->defaultConstraintName !== null) { + $platformOptions[SQLServerPlatform::OPTION_DEFAULT_CONSTRAINT_NAME] = $this->defaultConstraintName; + } + + return new Column( + $this->name->toString(), + $this->type, + [ + 'length' => $this->length, + 'precision' => $this->precision, + 'scale' => $this->scale, + 'unsigned' => $this->unsigned, + 'fixed' => $this->fixed, + 'notnull' => $this->notNull, + 'default' => $this->defaultValue, + 'autoincrement' => $this->autoincrement, + 'comment' => $this->comment, + 'values' => $this->values, + 'platformOptions' => $platformOptions, + 'columnDefinition' => $this->columnDefinition, + ], + ); + } +} diff --git a/src/Schema/Exception/InvalidColumnDefinition.php b/src/Schema/Exception/InvalidColumnDefinition.php new file mode 100644 index 00000000000..61fc224fd96 --- /dev/null +++ b/src/Schema/Exception/InvalidColumnDefinition.php @@ -0,0 +1,25 @@ +toString())); + } +} diff --git a/tests/Functional/AutoIncrementColumnTest.php b/tests/Functional/AutoIncrementColumnTest.php index d533ac8ddd1..7cfe6530356 100644 --- a/tests/Functional/AutoIncrementColumnTest.php +++ b/tests/Functional/AutoIncrementColumnTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Platforms\DB2Platform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -18,9 +19,17 @@ class AutoIncrementColumnTest extends FunctionalTestCase protected function setUp(): void { - $table = new Table('auto_increment_table'); - $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('val', Types::INTEGER); + $table = new Table('auto_increment_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/BinaryDataAccessTest.php b/tests/Functional/BinaryDataAccessTest.php index 51464ba28e3..7aba59f099d 100644 --- a/tests/Functional/BinaryDataAccessTest.php +++ b/tests/Functional/BinaryDataAccessTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -29,9 +30,18 @@ protected function setUp(): void self::markTestSkipped("PDO_OCI doesn't support binding binary values"); } - $table = new Table('binary_fetch_table'); - $table->addColumn('test_int', 'integer'); - $table->addColumn('test_binary', 'binary', ['notnull' => false, 'length' => 4]); + $table = new Table('binary_fetch_table', [ + Column::editor() + ->setUnquotedName('test_int') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('test_binary') + ->setTypeName(Types::BINARY) + ->setNotNull(false) + ->setLength(4) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('test_int') diff --git a/tests/Functional/BlobTest.php b/tests/Functional/BlobTest.php index a2e6688130c..f396938a3c7 100644 --- a/tests/Functional/BlobTest.php +++ b/tests/Functional/BlobTest.php @@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests\Functional; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -26,10 +27,22 @@ protected function setUp(): void self::markTestSkipped("DBAL doesn't support storing LOBs represented as streams using PDO_OCI"); } - $table = new Table('blob_table'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('clobcolumn', Types::TEXT, ['notnull' => false]); - $table->addColumn('blobcolumn', Types::BLOB, ['notnull' => false]); + $table = new Table('blob_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('clobcolumn') + ->setTypeName(Types::TEXT) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('blobcolumn') + ->setTypeName(Types::BLOB) + ->setNotNull(false) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -179,10 +192,22 @@ public function testBindParamProcessesStream(): void public function testBlobBindingDoesNotOverwritePrevious(): void { - $table = new Table('blob_table'); - $table->addColumn('id', 'integer'); - $table->addColumn('blobcolumn1', 'blob', ['notnull' => false]); - $table->addColumn('blobcolumn2', 'blob', ['notnull' => false]); + $table = new Table('blob_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('blobcolumn1') + ->setTypeName(Types::BLOB) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('blobcolumn2') + ->setTypeName(Types::BLOB) + ->setNotNull(false) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/ConnectionTest.php b/tests/Functional/ConnectionTest.php index 0050746ab5b..af20a414668 100644 --- a/tests/Functional/ConnectionTest.php +++ b/tests/Functional/ConnectionTest.php @@ -13,6 +13,7 @@ use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -365,8 +366,12 @@ public function testExceptionOnPrepareAndExecute(): void private function createTestTable(): void { - $table = new Table(self::TABLE); - $table->addColumn('id', Types::INTEGER); + $table = new Table(self::TABLE, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/DataAccessTest.php b/tests/Functional/DataAccessTest.php index f325584fc3e..b02bbcc6b91 100644 --- a/tests/Functional/DataAccessTest.php +++ b/tests/Functional/DataAccessTest.php @@ -10,6 +10,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Platforms\TrimMode; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Statement; @@ -28,10 +29,22 @@ class DataAccessTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('fetch_table'); - $table->addColumn('test_int', Types::INTEGER); - $table->addColumn('test_string', Types::STRING, ['length' => 32]); - $table->addColumn('test_datetime', Types::DATETIME_MUTABLE, ['notnull' => false]); + $table = new Table('fetch_table', [ + Column::editor() + ->setUnquotedName('test_int') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('test_string') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + Column::editor() + ->setUnquotedName('test_datetime') + ->setTypeName(Types::DATETIME_MUTABLE) + ->setNotNull(false) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('test_int') @@ -644,9 +657,16 @@ public function testSqliteDateArithmeticWithDynamicInterval(): void self::markTestSkipped('test is for sqlite only'); } - $table = new Table('fetch_table_date_math'); - $table->addColumn('test_date', Types::DATE_MUTABLE); - $table->addColumn('test_days', Types::INTEGER); + $table = new Table('fetch_table_date_math', [ + Column::editor() + ->setUnquotedName('test_date') + ->setTypeName(Types::DATE_MUTABLE) + ->create(), + Column::editor() + ->setUnquotedName('test_days') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('test_date') diff --git a/tests/Functional/Driver/DBAL6024Test.php b/tests/Functional/Driver/DBAL6024Test.php index 1bc7196b1af..8628fefbfb9 100644 --- a/tests/Functional/Driver/DBAL6024Test.php +++ b/tests/Functional/Driver/DBAL6024Test.php @@ -4,10 +4,12 @@ namespace Doctrine\DBAL\Tests\Functional\Driver; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\TestUtil; +use Doctrine\DBAL\Types\Types; class DBAL6024Test extends FunctionalTestCase { @@ -22,8 +24,12 @@ protected function setUp(): void public function testDropPrimaryKey(): void { - $table = new Table('mytable'); - $table->addColumn('id', 'integer'); + $table = new Table('mytable', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/Driver/DBAL6044Test.php b/tests/Functional/Driver/DBAL6044Test.php index 40713290be4..e21488fa8fc 100644 --- a/tests/Functional/Driver/DBAL6044Test.php +++ b/tests/Functional/Driver/DBAL6044Test.php @@ -4,9 +4,11 @@ namespace Doctrine\DBAL\Tests\Functional\Driver; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\TestUtil; +use Doctrine\DBAL\Types\Types; class DBAL6044Test extends FunctionalTestCase { @@ -23,13 +25,21 @@ protected function setUp(): void public function testUnloggedTables(): void { - $unloggedTable = new Table('my_unlogged'); + $unloggedTable = new Table('my_unlogged', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::STRING) + ->create(), + ]); $unloggedTable->addOption('unlogged', true); - $unloggedTable->addColumn('foo', 'string'); $this->dropAndCreateTable($unloggedTable); - $loggedTable = new Table('my_logged'); - $loggedTable->addColumn('foo', 'string'); + $loggedTable = new Table('my_logged', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::STRING) + ->create(), + ]); $this->dropAndCreateTable($loggedTable); $schemaManager = $this->connection->createSchemaManager(); diff --git a/tests/Functional/Driver/Mysqli/ResultTest.php b/tests/Functional/Driver/Mysqli/ResultTest.php index 0692e73972a..b8445bd705e 100644 --- a/tests/Functional/Driver/Mysqli/ResultTest.php +++ b/tests/Functional/Driver/Mysqli/ResultTest.php @@ -5,9 +5,11 @@ namespace Doctrine\DBAL\Tests\Functional\Driver\Mysqli; use Doctrine\DBAL\Driver\Mysqli\Result; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\TestUtil; +use Doctrine\DBAL\Types\Types; use mysqli; use mysqli_driver; use mysqli_sql_exception; @@ -38,8 +40,12 @@ protected function setUp(): void $this->nativeConnection = $nativeConnection; - $table = new Table(self::TABLE_NAME); - $table->addColumn('my_col_1', 'integer', ['notnull' => true]); + $table = new Table(self::TABLE_NAME, [ + Column::editor() + ->setUnquotedName('my_col_1') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->dropAndCreateTable($table); } diff --git a/tests/Functional/ExceptionTest.php b/tests/Functional/ExceptionTest.php index 460bb093882..2b18c8f4c30 100644 --- a/tests/Functional/ExceptionTest.php +++ b/tests/Functional/ExceptionTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Exception; use Doctrine\DBAL\Platforms\SQLitePlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; @@ -37,8 +38,12 @@ class ExceptionTest extends FunctionalTestCase { public function testPrimaryConstraintViolationException(): void { - $table = new Table('duplicatekey_table'); - $table->addColumn('id', Types::INTEGER, []); + $table = new Table('duplicatekey_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -63,8 +68,12 @@ public function testTableNotFoundException(): void public function testTableExistsException(): void { $schemaManager = $this->connection->createSchemaManager(); - $table = new Table('alreadyexist_table'); - $table->addColumn('id', Types::INTEGER, []); + $table = new Table('alreadyexist_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -78,9 +87,16 @@ public function testTableExistsException(): void public function testNotNullConstraintViolationException(): void { - $table = new Table('notnull_table'); - $table->addColumn('id', Types::INTEGER, []); - $table->addColumn('val', Types::INTEGER, ['notnull' => true]); + $table = new Table('notnull_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -94,8 +110,12 @@ public function testNotNullConstraintViolationException(): void public function testInvalidFieldNameException(): void { - $table = new Table('bad_columnname_table'); - $table->addColumn('id', Types::INTEGER, []); + $table = new Table('bad_columnname_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->dropAndCreateTable($table); $this->expectException(Exception\InvalidFieldNameException::class); @@ -120,12 +140,20 @@ public function testInvalidFieldNameException(): void public function testNonUniqueFieldNameException(): void { - $table1 = new Table('ambiguous_list_table_1'); - $table1->addColumn('id', Types::INTEGER); + $table1 = new Table('ambiguous_list_table_1', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->dropAndCreateTable($table1); - $table2 = new Table('ambiguous_list_table_2'); - $table2->addColumn('id', Types::INTEGER); + $table2 = new Table('ambiguous_list_table_2', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->dropAndCreateTable($table2); $sql = 'SELECT id FROM ambiguous_list_table_1, ambiguous_list_table_2'; @@ -135,8 +163,12 @@ public function testNonUniqueFieldNameException(): void public function testUniqueConstraintViolationException(): void { - $table = new Table('unique_column_table'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('unique_column_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addUniqueIndex(['id']); $this->dropAndCreateTable($table); @@ -148,8 +180,12 @@ public function testUniqueConstraintViolationException(): void public function testSyntaxErrorException(): void { - $table = new Table('syntax_error_table'); - $table->addColumn('id', Types::INTEGER, []); + $table = new Table('syntax_error_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/ForeignKeyConstraintViolationsTest.php b/tests/Functional/ForeignKeyConstraintViolationsTest.php index a790af52598..4d1fdd83744 100644 --- a/tests/Functional/ForeignKeyConstraintViolationsTest.php +++ b/tests/Functional/ForeignKeyConstraintViolationsTest.php @@ -12,10 +12,12 @@ use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; +use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\Assert; use Throwable; @@ -41,12 +43,20 @@ protected function setUp(): void $schemaManager = $this->connection->createSchemaManager(); - $table = new Table('test_t1'); - $table->addColumn('ref_id', 'integer', ['notnull' => true]); + $table = new Table('test_t1', [ + Column::editor() + ->setUnquotedName('ref_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $schemaManager->createTable($table); - $table2 = new Table('test_t2'); - $table2->addColumn('id', 'integer', ['notnull' => true]); + $table2 = new Table('test_t2', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table2->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/ForeignKeyExceptionTest.php b/tests/Functional/ForeignKeyExceptionTest.php index 5df9e7113df..344fc21f4f0 100644 --- a/tests/Functional/ForeignKeyExceptionTest.php +++ b/tests/Functional/ForeignKeyExceptionTest.php @@ -7,9 +7,11 @@ use Doctrine\DBAL\Driver\AbstractSQLServerDriver; use Doctrine\DBAL\Driver\IBMDB2; use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; +use Doctrine\DBAL\Types\Types; class ForeignKeyExceptionTest extends FunctionalTestCase { @@ -25,17 +27,28 @@ protected function setUp(): void $schemaManager = $this->connection->createSchemaManager(); - $table = new Table('constraint_error_table'); - $table->addColumn('id', 'integer', []); + $table = new Table('constraint_error_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') ->create(), ); - $owningTable = new Table('owning_table'); - $owningTable->addColumn('id', 'integer', []); - $owningTable->addColumn('constraint_id', 'integer', []); + $owningTable = new Table('owning_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('constraint_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $owningTable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/LockMode/NoneTest.php b/tests/Functional/LockMode/NoneTest.php index a554562e26c..37869a576bd 100644 --- a/tests/Functional/LockMode/NoneTest.php +++ b/tests/Functional/LockMode/NoneTest.php @@ -10,6 +10,7 @@ use Doctrine\DBAL\LockMode; use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -34,8 +35,12 @@ public function setUp(): void $this->connection->executeStatement('ALTER DATABASE ' . $db . ' SET MULTI_USER'); } - $table = new Table('users'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('users', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/ModifyLimitQueryTest.php b/tests/Functional/ModifyLimitQueryTest.php index 35e2a0a6d7c..81f239da268 100644 --- a/tests/Functional/ModifyLimitQueryTest.php +++ b/tests/Functional/ModifyLimitQueryTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Platforms\DB2Platform; use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -21,17 +22,29 @@ class ModifyLimitQueryTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('modify_limit_table'); - $table->addColumn('test_int', Types::INTEGER); + $table = new Table('modify_limit_table', [ + Column::editor() + ->setUnquotedName('test_int') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('test_int') ->create(), ); - $table2 = new Table('modify_limit_table2'); - $table2->addColumn('id', Types::INTEGER, ['autoincrement' => true]); - $table2->addColumn('test_int', Types::INTEGER); + $table2 = new Table('modify_limit_table2', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('test_int') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table2->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/NamedParametersTest.php b/tests/Functional/NamedParametersTest.php index 4f6d75f8dc2..f6f38e18038 100644 --- a/tests/Functional/NamedParametersTest.php +++ b/tests/Functional/NamedParametersTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Connection; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -167,10 +168,22 @@ protected function setUp(): void } try { - $table = new Table('ddc1372_foobar'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('foo', Types::STRING, ['length' => 1]); - $table->addColumn('bar', Types::STRING, ['length' => 1]); + $table = new Table('ddc1372_foobar', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::STRING) + ->setLength(1) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setLength(1) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/Platform/AddColumnWithDefaultTest.php b/tests/Functional/Platform/AddColumnWithDefaultTest.php index 5834e03405d..9d3920e1365 100644 --- a/tests/Functional/Platform/AddColumnWithDefaultTest.php +++ b/tests/Functional/Platform/AddColumnWithDefaultTest.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tests\Functional\Platform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; @@ -14,9 +15,13 @@ public function testAddColumnWithDefault(): void { $schemaManager = $this->connection->createSchemaManager(); - $table = new Table('add_default_test'); - - $table->addColumn('original_field', Types::STRING, ['length' => 8]); + $table = new Table('add_default_test', [ + Column::editor() + ->setUnquotedName('original_field') + ->setTypeName(Types::STRING) + ->setLength(8) + ->create(), + ]); $this->dropAndCreateTable($table); $this->connection->executeStatement("INSERT INTO add_default_test (original_field) VALUES ('one')"); diff --git a/tests/Functional/Platform/AlterColumnLengthChangeTest.php b/tests/Functional/Platform/AlterColumnLengthChangeTest.php index 984aacc0f27..c0c0637254a 100644 --- a/tests/Functional/Platform/AlterColumnLengthChangeTest.php +++ b/tests/Functional/Platform/AlterColumnLengthChangeTest.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tests\Functional\Platform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; @@ -12,8 +13,13 @@ class AlterColumnLengthChangeTest extends FunctionalTestCase { public function testColumnLengthIsChanged(): void { - $table = new Table('test_alter_length'); - $table->addColumn('c1', Types::STRING)->setLength(50); + $table = new Table('test_alter_length', [ + Column::editor() + ->setUnquotedName('c1') + ->setTypeName(Types::STRING) + ->setLength(50) + ->create(), + ]); $this->dropAndCreateTable($table); diff --git a/tests/Functional/Platform/AlterColumnTest.php b/tests/Functional/Platform/AlterColumnTest.php index 29b3d9e7a6c..fe7e7eabeda 100644 --- a/tests/Functional/Platform/AlterColumnTest.php +++ b/tests/Functional/Platform/AlterColumnTest.php @@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests\Functional\Platform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Type; @@ -14,9 +15,16 @@ class AlterColumnTest extends FunctionalTestCase { public function testColumnPositionRetainedAfterAltering(): void { - $table = new Table('test_alter'); - $table->addColumn('c1', Types::INTEGER); - $table->addColumn('c2', Types::INTEGER); + $table = new Table('test_alter', [ + Column::editor() + ->setUnquotedName('c1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('c2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->dropAndCreateTable($table); diff --git a/tests/Functional/Platform/AlterDecimalColumnTest.php b/tests/Functional/Platform/AlterDecimalColumnTest.php index bc0ddf435e8..db3ad5ece74 100644 --- a/tests/Functional/Platform/AlterDecimalColumnTest.php +++ b/tests/Functional/Platform/AlterDecimalColumnTest.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tests\Functional\Platform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; @@ -14,15 +15,22 @@ class AlterDecimalColumnTest extends FunctionalTestCase { #[DataProvider('scaleAndPrecisionProvider')] - public function testAlterPrecisionAndScale(int $newPrecision, int $newScale, string $type): void + public function testAlterPrecisionAndScale(int $newPrecision, int $newScale, string $typeName): void { - $table = new Table('decimal_table'); - $column = $table->addColumn('val', $type, ['precision' => 16, 'scale' => 6]); + $table = new Table('decimal_table', [ + Column::editor() + ->setUnquotedName('val') + ->setTypeName($typeName) + ->setPrecision(16) + ->setScale(6) + ->create(), + ]); $this->dropAndCreateTable($table); - $column->setPrecision($newPrecision); - $column->setScale($newScale); + $table->getColumn('val') + ->setPrecision($newPrecision) + ->setScale($newScale); $schemaManager = $this->connection->createSchemaManager(); diff --git a/tests/Functional/Platform/DateExpressionTest.php b/tests/Functional/Platform/DateExpressionTest.php index 6fe03961387..36ec13ada04 100644 --- a/tests/Functional/Platform/DateExpressionTest.php +++ b/tests/Functional/Platform/DateExpressionTest.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tests\Functional\Platform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; @@ -16,9 +17,16 @@ class DateExpressionTest extends FunctionalTestCase #[DataProvider('differenceProvider')] public function testDifference(string $date1, string $date2, int $expected): void { - $table = new Table('date_expr_test'); - $table->addColumn('date1', Types::DATETIME_MUTABLE); - $table->addColumn('date2', Types::DATETIME_MUTABLE); + $table = new Table('date_expr_test', [ + Column::editor() + ->setUnquotedName('date1') + ->setTypeName(Types::DATETIME_MUTABLE) + ->create(), + Column::editor() + ->setUnquotedName('date2') + ->setTypeName(Types::DATETIME_MUTABLE) + ->create(), + ]); $this->dropAndCreateTable($table); $this->connection->insert('date_expr_test', [ 'date1' => $date1, diff --git a/tests/Functional/Platform/DefaultExpressionTest.php b/tests/Functional/Platform/DefaultExpressionTest.php index edc955ca3a5..62961e2e134 100644 --- a/tests/Functional/Platform/DefaultExpressionTest.php +++ b/tests/Functional/Platform/DefaultExpressionTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\OraclePlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; @@ -52,14 +53,22 @@ public function testCurrentTimestamp(): void }); } - private function assertDefaultExpression(string $type, callable $expression): void + private function assertDefaultExpression(string $typeName, callable $expression): void { $platform = $this->connection->getDatabasePlatform(); $defaultSql = $expression($platform, $this); - $table = new Table('default_expr_test'); - $table->addColumn('actual_value', $type); - $table->addColumn('default_value', $type, ['default' => $defaultSql]); + $table = new Table('default_expr_test', [ + Column::editor() + ->setUnquotedName('actual_value') + ->setTypeName($typeName) + ->create(), + Column::editor() + ->setUnquotedName('default_value') + ->setTypeName($typeName) + ->setDefaultValue($defaultSql) + ->create(), + ]); $this->dropAndCreateTable($table); $this->connection->executeStatement( diff --git a/tests/Functional/Platform/OtherSchemaTest.php b/tests/Functional/Platform/OtherSchemaTest.php index 38dbe6b2763..ab75813119d 100644 --- a/tests/Functional/Platform/OtherSchemaTest.php +++ b/tests/Functional/Platform/OtherSchemaTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Platforms\SQLitePlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tools\DsnParser; @@ -22,8 +23,12 @@ public function testATableCanBeCreatedInAnotherSchema(): void $this->connection->executeStatement("ATTACH DATABASE '/tmp/test_other_schema.sqlite' AS other"); - $table = new Table('other.test_other_schema'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('other.test_other_schema', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['id']); $this->dropAndCreateTable($table); diff --git a/tests/Functional/Platform/PlatformRestrictionsTest.php b/tests/Functional/Platform/PlatformRestrictionsTest.php index c7cfd14a523..2b9b43c4943 100644 --- a/tests/Functional/Platform/PlatformRestrictionsTest.php +++ b/tests/Functional/Platform/PlatformRestrictionsTest.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tests\Functional\Platform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -26,8 +27,13 @@ public function testMaxIdentifierLengthLimitWithAutoIncrement(): void $platform = $this->connection->getDatabasePlatform(); $tableName = str_repeat('x', $platform->getMaxIdentifierLength()); $columnName = str_repeat('y', $platform->getMaxIdentifierLength()); - $table = new Table($tableName); - $table->addColumn($columnName, Types::INTEGER, ['autoincrement' => true]); + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName($columnName) + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $primaryKeyConstraint = PrimaryKeyConstraint::editor() ->setUnquotedColumnNames($columnName) diff --git a/tests/Functional/Platform/RenameColumnTest.php b/tests/Functional/Platform/RenameColumnTest.php index 133775b2786..4016a5a2155 100644 --- a/tests/Functional/Platform/RenameColumnTest.php +++ b/tests/Functional/Platform/RenameColumnTest.php @@ -21,9 +21,17 @@ class RenameColumnTest extends FunctionalTestCase #[DataProvider('columnNameProvider')] public function testColumnPositionRetainedAfterImplicitRenaming(string $columnName, string $newColumnName): void { - $table = new Table('test_rename'); - $table->addColumn($columnName, Types::STRING, ['length' => 16]); - $table->addColumn('c2', Types::INTEGER); + $table = new Table('test_rename', [ + Column::editor() + ->setUnquotedName($columnName) + ->setTypeName(Types::STRING) + ->setLength(16) + ->create(), + Column::editor() + ->setUnquotedName('c2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->dropAndCreateTable($table); diff --git a/tests/Functional/PortabilityTest.php b/tests/Functional/PortabilityTest.php index 070dc676bf2..bb7b7684657 100644 --- a/tests/Functional/PortabilityTest.php +++ b/tests/Functional/PortabilityTest.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Portability\Connection; use Doctrine\DBAL\Portability\Middleware; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -172,15 +173,23 @@ private function connectWithPortability(int $mode, ?ColumnCase $case): void private function createTable(): void { - $table = new Table('portability_table'); - $table->addColumn('Test_Int', Types::INTEGER); - $table->addColumn('Test_String', Types::STRING, [ - 'fixed' => true, - 'length' => 8, - ]); - $table->addColumn('Test_Null', Types::STRING, [ - 'length' => 1, - 'notnull' => false, + $table = new Table('portability_table', [ + Column::editor() + ->setUnquotedName('Test_Int') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('Test_String') + ->setTypeName(Types::STRING) + ->setFixed(true) + ->setLength(8) + ->create(), + Column::editor() + ->setUnquotedName('Test_Null') + ->setTypeName(Types::STRING) + ->setLength(1) + ->setNotNull(false) + ->create(), ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() diff --git a/tests/Functional/PrimaryReadReplicaConnectionTest.php b/tests/Functional/PrimaryReadReplicaConnectionTest.php index f46d45d57c9..931670d050d 100644 --- a/tests/Functional/PrimaryReadReplicaConnectionTest.php +++ b/tests/Functional/PrimaryReadReplicaConnectionTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -27,8 +28,12 @@ protected function setUp(): void } try { - $table = new Table('primary_replica_table'); - $table->addColumn('test_int', Types::INTEGER); + $table = new Table('primary_replica_table', [ + Column::editor() + ->setUnquotedName('test_int') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('test_int') diff --git a/tests/Functional/Query/QueryBuilderTest.php b/tests/Functional/Query/QueryBuilderTest.php index c0381d56e6f..483840085bf 100644 --- a/tests/Functional/Query/QueryBuilderTest.php +++ b/tests/Functional/Query/QueryBuilderTest.php @@ -16,6 +16,7 @@ use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Query\ForUpdate\ConflictResolutionMode; use Doctrine\DBAL\Query\UnionType; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -30,8 +31,12 @@ final class QueryBuilderTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('for_update'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('for_update', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/ResultMetadataTest.php b/tests/Functional/ResultMetadataTest.php index 482bcac5fa3..148bbd5a097 100644 --- a/tests/Functional/ResultMetadataTest.php +++ b/tests/Functional/ResultMetadataTest.php @@ -5,9 +5,11 @@ namespace Doctrine\DBAL\Tests\Functional; use Doctrine\DBAL\Exception\InvalidColumnIndex; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; +use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\Attributes\TestWith; use function strtolower; @@ -16,8 +18,12 @@ class ResultMetadataTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('result_metadata_table'); - $table->addColumn('test_int', 'integer'); + $table = new Table('result_metadata_table', [ + Column::editor() + ->setUnquotedName('test_int') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('test_int') diff --git a/tests/Functional/Schema/AlterTableTest.php b/tests/Functional/Schema/AlterTableTest.php index 4511048f602..699ffd8ea9e 100644 --- a/tests/Functional/Schema/AlterTableTest.php +++ b/tests/Functional/Schema/AlterTableTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Platforms\DB2Platform; use Doctrine\DBAL\Platforms\SQLitePlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -16,9 +17,16 @@ class AlterTableTest extends FunctionalTestCase { public function testAddPrimaryKeyOnExistingColumn(): void { - $table = new Table('alter_pk'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('val', Types::INTEGER); + $table = new Table('alter_pk', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->testMigration($table, static function (Table $table): void { $table->addPrimaryKeyConstraint( @@ -37,8 +45,12 @@ public function testAddPrimaryKeyOnNewAutoIncrementColumn(): void ); } - $table = new Table('alter_pk'); - $table->addColumn('val', Types::INTEGER); + $table = new Table('alter_pk', [ + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->testMigration($table, static function (Table $table): void { $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); @@ -66,9 +78,17 @@ public function testAlterPrimaryKeyFromAutoincrementToNonAutoincrementColumn(): ); } - $table = new Table('alter_pk'); - $table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('id2', Types::INTEGER); + $table = new Table('alter_pk', [ + Column::editor() + ->setUnquotedName('id1') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('id2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id1') @@ -101,9 +121,17 @@ public function testDropPrimaryKeyWithAutoincrementColumn(): void ); } - $table = new Table('alter_pk'); - $table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('id2', Types::INTEGER); + $table = new Table('alter_pk', [ + Column::editor() + ->setUnquotedName('id1') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('id2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id1', 'id2') @@ -125,9 +153,17 @@ public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoinc ); } - $table = new Table('alter_pk'); - $table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('id2', Types::INTEGER); + $table = new Table('alter_pk', [ + Column::editor() + ->setUnquotedName('id1') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('id2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id1', 'id2') @@ -154,9 +190,17 @@ public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn ); } - $table = new Table('alter_pk'); - $table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('id2', Types::INTEGER); + $table = new Table('alter_pk', [ + Column::editor() + ->setUnquotedName('id1') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('id2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id1') @@ -179,8 +223,12 @@ public function testAddNewColumnToPrimaryKey(): void self::markTestIncomplete('This test fails on IBM Db2 for an unrelated reason.'); } - $table = new Table('alter_pk'); - $table->addColumn('id1', Types::INTEGER); + $table = new Table('alter_pk', [ + Column::editor() + ->setUnquotedName('id1') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id1') @@ -200,9 +248,16 @@ public function testAddNewColumnToPrimaryKey(): void public function testReplaceForeignKeyConstraint(): void { - $articles = new Table('articles'); - $articles->addColumn('id', Types::INTEGER); - $articles->addColumn('sku', Types::INTEGER); + $articles = new Table('articles', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('sku') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $articles->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -210,10 +265,20 @@ public function testReplaceForeignKeyConstraint(): void ); $articles->addUniqueConstraint(['sku']); - $orders = new Table('orders'); - $orders->addColumn('id', Types::INTEGER); - $orders->addColumn('article_id', Types::INTEGER); - $orders->addColumn('article_sku', Types::INTEGER); + $orders = new Table('orders', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('article_id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('article_sku') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $orders->addForeignKeyConstraint( 'articles', ['article_id'], diff --git a/tests/Functional/Schema/ColumnCommentTest.php b/tests/Functional/Schema/ColumnCommentTest.php index d489ef6a284..417586bb334 100644 --- a/tests/Functional/Schema/ColumnCommentTest.php +++ b/tests/Functional/Schema/ColumnCommentTest.php @@ -4,8 +4,10 @@ namespace Doctrine\DBAL\Tests\Functional\Schema; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; +use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\Attributes\DataProvider; use function array_merge; @@ -25,8 +27,12 @@ protected function setUp(): void self::$initialized = true; - $table = new Table('column_comments'); - $table->addColumn('id', 'integer'); + $table = new Table('column_comments', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); foreach (self::columnProvider() as [$name, $type, $options]) { $table->addColumn($name, $type, $options); @@ -80,8 +86,13 @@ public static function columnProvider(): iterable #[DataProvider('alterColumnCommentProvider')] public function testAlterColumnComment(string $comment1, string $comment2): void { - $table1 = new Table('column_comments'); - $table1->addColumn('id', 'integer', ['comment' => $comment1]); + $table1 = new Table('column_comments', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setComment($comment1) + ->create(), + ]); $this->dropAndCreateTable($table1); diff --git a/tests/Functional/Schema/ColumnRenameTest.php b/tests/Functional/Schema/ColumnRenameTest.php index 613855e14cb..d14adc4def4 100644 --- a/tests/Functional/Schema/ColumnRenameTest.php +++ b/tests/Functional/Schema/ColumnRenameTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Exception; use Doctrine\DBAL\Schema\AbstractSchemaManager; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -37,9 +38,16 @@ public function testRenameColumnInForeignKeyConstraint(): void { $this->dropTableIfExists('rename_column_referenced'); - $referencedTable = new Table('rename_column_referenced'); - $referencedTable->addColumn('c1', Types::INTEGER); - $referencedTable->addColumn('c2', Types::INTEGER); + $referencedTable = new Table('rename_column_referenced', [ + Column::editor() + ->setUnquotedName('c1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('c2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); // PostgreSQL requires a unique constraint on the referenced table columns $referencedTable->addUniqueConstraint(['c1', 'c2']); @@ -60,9 +68,16 @@ private function testRenameColumn(callable $modifier): void { $this->dropTableIfExists('rename_column'); - $table = new Table('rename_column'); - $table->addColumn('c1', Types::INTEGER); - $table->addColumn('c2', Types::INTEGER); + $table = new Table('rename_column', [ + Column::editor() + ->setUnquotedName('c1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('c2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $modifier($table); $table->renameColumn('c1', 'c1a'); diff --git a/tests/Functional/Schema/ComparatorTest.php b/tests/Functional/Schema/ComparatorTest.php index 17bd048e4ea..6857726ccbd 100644 --- a/tests/Functional/Schema/ComparatorTest.php +++ b/tests/Functional/Schema/ComparatorTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Platforms\MariaDBPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\ComparatorConfig; use Doctrine\DBAL\Schema\Table; @@ -26,19 +27,24 @@ protected function setUp(): void } #[DataProvider('defaultValueProvider')] - public function testDefaultValueComparison(string $type, mixed $value): void + public function testDefaultValueComparison(string $typeName, mixed $value): void { $platform = $this->connection->getDatabasePlatform(); if ( - $type === Types::TEXT && $platform instanceof AbstractMySQLPlatform + $typeName === Types::TEXT && $platform instanceof AbstractMySQLPlatform && ! $platform instanceof MariaDBPlatform ) { // See https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-13.html#mysqld-8-0-13-data-types self::markTestSkipped('Oracle MySQL does not support default values on TEXT/BLOB columns until 8.0.13.'); } - $table = new Table('default_value'); - $table->addColumn('test', $type, ['default' => $value]); + $table = new Table('default_value', [ + Column::editor() + ->setUnquotedName('test') + ->setTypeName($typeName) + ->setDefaultValue($value) + ->create(), + ]); $this->dropAndCreateTable($table); diff --git a/tests/Functional/Schema/CustomIntrospectionTest.php b/tests/Functional/Schema/CustomIntrospectionTest.php index a65cbb4cecd..e4bda93978e 100644 --- a/tests/Functional/Schema/CustomIntrospectionTest.php +++ b/tests/Functional/Schema/CustomIntrospectionTest.php @@ -4,12 +4,15 @@ namespace Doctrine\DBAL\Tests\Functional\Schema; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ColumnDiff; use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\Functional\Schema\Types\MoneyType; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\TestUtil; use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Types\Types; use function array_map; use function implode; @@ -37,18 +40,25 @@ public function testCustomColumnIntrospection(): void $tableName = 'test_custom_column_introspection'; $schemaManager = $this->connection->createSchemaManager(); $schema = new Schema([], [], $schemaManager->createSchemaConfig()); - $table = $schema->createTable($tableName); - - $table->addColumn('id', 'integer'); - $table->addColumn('quantity', 'decimal', [ - 'notnull' => false, - 'scale' => 2, - 'precision' => 10, - ]); - $table->addColumn('amount', 'money', [ - 'notnull' => false, - 'scale' => 2, - 'precision' => 10, + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('quantity') + ->setTypeName(Types::DECIMAL) + ->setPrecision(10) + ->setScale(2) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('amount') + ->setTypeName('money') + ->setPrecision(10) + ->setScale(2) + ->setNotNull(false) + ->create(), ]); $this->dropAndCreateTable($table); diff --git a/tests/Functional/Schema/DefaultValueTest.php b/tests/Functional/Schema/DefaultValueTest.php index 07413e89682..8bfe352b4ad 100644 --- a/tests/Functional/Schema/DefaultValueTest.php +++ b/tests/Functional/Schema/DefaultValueTest.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tests\Functional\Schema; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; @@ -15,17 +16,25 @@ class DefaultValueTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('default_value'); - $table->addColumn('id', Types::INTEGER); + $columns = [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]; foreach (self::columnProvider() as [$name, $default]) { - $table->addColumn($name, Types::STRING, [ - 'length' => 32, - 'default' => $default, - 'notnull' => false, - ]); + $columns[] = Column::editor() + ->setUnquotedName($name) + ->setTypeName(Types::STRING) + ->setLength(32) + ->setDefaultValue($default) + ->setNotNull(false) + ->create(); } + $table = new Table('default_value', $columns); + $this->dropAndCreateTable($table); $this->connection->insert('default_value', ['id' => 1]); diff --git a/tests/Functional/Schema/ForeignKeyConstraintTest.php b/tests/Functional/Schema/ForeignKeyConstraintTest.php index 75fd6e131df..69026aa3321 100644 --- a/tests/Functional/Schema/ForeignKeyConstraintTest.php +++ b/tests/Functional/Schema/ForeignKeyConstraintTest.php @@ -21,7 +21,6 @@ use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\Attributes\DataProvider; @@ -37,16 +36,24 @@ public function testUnnamedForeignKeyConstraint(): void $this->dropTableIfExists('roles'); $this->dropTableIfExists('teams'); - $roles = new Table('roles'); - $roles->addColumn('id', Types::INTEGER); + $roles = new Table('roles', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $roles->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') ->create(), ); - $teams = new Table('teams'); - $teams->addColumn('id', Types::INTEGER); + $teams = new Table('teams', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $teams->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -57,9 +64,18 @@ public function testUnnamedForeignKeyConstraint(): void ->setUnquotedReferencedColumnNames('id'); $users = new Table('users', [ - new Column('id', Type::getType(Types::INTEGER)), - new Column('role_id', Type::getType(Types::INTEGER)), - new Column('team_id', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('role_id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('team_id') + ->setTypeName(Types::INTEGER) + ->create(), ], [], [], [ $editor ->setUnquotedReferencedTableName('roles') @@ -96,18 +112,32 @@ public function testColumnIntrospection(): void $rolesName = OptionallyQualifiedName::unquoted('roles'); $teamsName = OptionallyQualifiedName::unquoted('teams'); - $roles = new Table($rolesName->toString()); - $roles->addColumn('r_id1', Types::INTEGER); - $roles->addColumn('r_id2', Types::INTEGER); + $roles = new Table($rolesName->toString(), [ + Column::editor() + ->setUnquotedName('r_id1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('r_id2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $roles->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('r_id1', 'r_id2') ->create(), ); - $teams = new Table($teamsName->toString()); - $teams->addColumn('t_id1', Types::INTEGER); - $teams->addColumn('t_id2', Types::INTEGER); + $teams = new Table($teamsName->toString(), [ + Column::editor() + ->setUnquotedName('t_id1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('t_id2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $teams->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('t_id1', 't_id2') @@ -130,12 +160,30 @@ public function testColumnIntrospection(): void ]; $users = new Table('users', [ - new Column('u_id1', Type::getType(Types::INTEGER)), - new Column('u_id2', Type::getType(Types::INTEGER)), - new Column('role_id1', Type::getType(Types::INTEGER)), - new Column('role_id2', Type::getType(Types::INTEGER)), - new Column('team_id1', Type::getType(Types::INTEGER)), - new Column('team_id2', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('u_id1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('u_id2') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('role_id1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('role_id2') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('team_id1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('team_id2') + ->setTypeName(Types::INTEGER) + ->create(), ], [], [], $foreignKeyConstraints); $users->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() @@ -213,8 +261,12 @@ private function testReferentialActionIntrospection( $this->dropTableIfExists('users'); $this->dropTableIfExists('roles'); - $roles = new Table('roles'); - $roles->addColumn('id', Types::INTEGER); + $roles = new Table('roles', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $roles->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -228,8 +280,15 @@ private function testReferentialActionIntrospection( $setter($editor, $action); $users = new Table('users', [ - new Column('id', Type::getType(Types::INTEGER)), - new Column('role_id', Type::getType(Types::INTEGER), ['notnull' => false]), + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('role_id') + ->setTypeName(Types::INTEGER) + ->setNotNull(false) + ->create(), ], [], [], [$editor->create()]); $users->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() @@ -328,8 +387,12 @@ public function testDeferrabilityIntrospection(Deferrability $deferrability): vo $this->dropTableIfExists('users'); $this->dropTableIfExists('roles'); - $roles = new Table('roles'); - $roles->addColumn('id', Types::INTEGER); + $roles = new Table('roles', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $roles->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -337,8 +400,14 @@ public function testDeferrabilityIntrospection(Deferrability $deferrability): vo ); $users = new Table('users', [ - new Column('id', Type::getType(Types::INTEGER)), - new Column('role_id', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('role_id') + ->setTypeName(Types::INTEGER) + ->create(), ], [], [], [ ForeignKeyConstraint::editor() ->setUnquotedReferencingColumnNames('role_id') diff --git a/tests/Functional/Schema/MySQL/ComparatorTest.php b/tests/Functional/Schema/MySQL/ComparatorTest.php index e5d1543c33f..01f15b6f161 100644 --- a/tests/Functional/Schema/MySQL/ComparatorTest.php +++ b/tests/Functional/Schema/MySQL/ComparatorTest.php @@ -76,10 +76,15 @@ public static function lobColumnProvider(): iterable } /** @throws Exception */ - private function createLobTable(string $type, int $length): Table + private function createLobTable(string $typeName, int $length): Table { - $table = new Table('comparator_test'); - $table->addColumn('lob', $type)->setLength($length); + $table = new Table('comparator_test', [ + Column::editor() + ->setUnquotedName('lob') + ->setTypeName($typeName) + ->setLength($length) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -94,8 +99,9 @@ private function setBlobLength(Table $table, int $length): void public function testExplicitDefaultCollation(): void { - [$table, $column] = $this->createCollationTable(); - $column->setPlatformOption('collation', 'utf8mb4_general_ci'); + $table = $this->createCollationTable(); + $table->getColumn('id') + ->setPlatformOption('collation', 'utf8mb4_general_ci'); self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable( $this->schemaManager, @@ -112,33 +118,43 @@ public function testExplicitDefaultCollation(): void public function testChangeColumnCharsetAndCollation(): void { - [$table, $column] = $this->createCollationTable(); - $column->setPlatformOption('charset', 'latin1'); - $column->setPlatformOption('collation', 'latin1_bin'); + $table = $this->createCollationTable(); + $table->getColumn('id') + ->setPlatformOption('charset', 'latin1') + ->setPlatformOption('collation', 'latin1_bin'); ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table); } public function testChangeColumnCollation(): void { - [$table, $column] = $this->createCollationTable(); - $column->setPlatformOption('collation', 'utf8mb4_bin'); + $table = $this->createCollationTable(); + $table->getColumn('id') + ->setPlatformOption('collation', 'utf8mb4_bin'); ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table); } /** * @param array $tableOptions - * @param array $columnOptions + * @param ?non-empty-string $columnCharset + * @param ?non-empty-string $columnCollation */ #[DataProvider('tableAndColumnOptionsProvider')] - public function testTableAndColumnOptions(array $tableOptions, array $columnOptions): void - { - $table = new Table('comparator_test', [], [], [], [], $tableOptions); - $table->addColumn('name', Types::STRING, [ - 'length' => 32, - 'platformOptions' => $columnOptions, - ]); + public function testTableAndColumnOptions( + array $tableOptions, + ?string $columnCharset, + ?string $columnCollation, + ): void { + $table = new Table('comparator_test', [ + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::STRING) + ->setLength(32) + ->setCharset($columnCharset) + ->setCollation($columnCollation) + ->create(), + ], [], [], [], $tableOptions); $this->dropAndCreateTable($table); @@ -157,9 +173,13 @@ public function testTableAndColumnOptions(array $tableOptions, array $columnOpti public function testSimpleArrayTypeNonChangeNotDetected(): void { - $table = new Table('comparator_test'); - - $table->addColumn('simple_array_col', Types::SIMPLE_ARRAY, ['length' => 255]); + $table = new Table('comparator_test', [ + Column::editor() + ->setUnquotedName('simple_array_col') + ->setTypeName(Types::SIMPLE_ARRAY) + ->setLength(255) + ->create(), + ]); $this->dropAndCreateTable($table); self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable( @@ -175,22 +195,25 @@ public function testSimpleArrayTypeNonChangeNotDetected(): void )->isEmpty()); } - /** @return iterable,array}> */ + /** @return iterable,?non-empty-string,?non-empty-string}> */ public static function tableAndColumnOptionsProvider(): iterable { yield "Column collation explicitly set to its table's default" => [ [], - ['collation' => 'utf8mb4_unicode_ci'], + null, + 'utf8mb4_general_ci', ]; yield "Column charset implicitly set to a value matching its table's charset" => [ ['charset' => 'utf8mb4'], - ['collation' => 'utf8mb4_unicode_ci'], + null, + 'utf8mb4_general_ci', ]; yield "Column collation reset to the collation's default matching its table's charset" => [ ['collation' => 'utf8mb4_unicode_ci'], - ['charset' => 'utf8mb4'], + 'utf8mb4', + null, ]; } @@ -200,9 +223,12 @@ public function testMariaDb1043NativeJsonUpgradeDetected(): void self::markTestSkipped(); } - $table = new Table('mariadb_json_upgrade'); - - $table->addColumn('json_col', Types::JSON); + $table = new Table('mariadb_json_upgrade', [ + Column::editor() + ->setUnquotedName('json_col') + ->setTypeName(Types::JSON) + ->create(), + ]); $this->dropAndCreateTable($table); // Revert column to old LONGTEXT declaration @@ -212,19 +238,19 @@ public function testMariaDb1043NativeJsonUpgradeDetected(): void ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table); } - /** - * @return array{Table,Column} - * - * @throws Exception - */ - private function createCollationTable(): array + private function createCollationTable(): Table { - $table = new Table('comparator_test'); + $table = new Table('comparator_test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]); $table->addOption('charset', 'utf8mb4'); $table->addOption('collation', 'utf8mb4_general_ci'); - $column = $table->addColumn('id', Types::STRING, ['length' => 32]); $this->dropAndCreateTable($table); - return [$table, $column]; + return $table; } } diff --git a/tests/Functional/Schema/MySQLSchemaManagerTest.php b/tests/Functional/Schema/MySQLSchemaManagerTest.php index 00f0d239d4e..523275dad8a 100644 --- a/tests/Functional/Schema/MySQLSchemaManagerTest.php +++ b/tests/Functional/Schema/MySQLSchemaManagerTest.php @@ -10,6 +10,7 @@ use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MariaDBPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Index\IndexedColumn; use Doctrine\DBAL\Schema\Index\IndexType; @@ -41,8 +42,12 @@ protected function supportsPlatform(AbstractPlatform $platform): bool public function testFulltextIndex(): void { - $table = new Table('fulltext_index'); - $table->addColumn('text', Types::TEXT); + $table = new Table('fulltext_index', [ + Column::editor() + ->setUnquotedName('text') + ->setTypeName(Types::TEXT) + ->create(), + ]); $table->addIndex(['text'], 'f_index', ['fulltext']); $table->addOption('engine', 'MyISAM'); @@ -62,8 +67,12 @@ public function testFulltextIndex(): void public function testSpatialIndex(): void { - $table = new Table('spatial_index'); - $table->addColumn('point', 'point'); + $table = new Table('spatial_index', [ + Column::editor() + ->setUnquotedName('point') + ->setTypeName('point') + ->create(), + ]); $table->addIndex(['point'], 's_index', ['spatial']); $table->addOption('engine', 'MyISAM'); @@ -86,8 +95,13 @@ public function testSpatialIndex(): void public function testIndexWithLength(): void { - $table = new Table('index_length'); - $table->addColumn('text', Types::STRING, ['length' => 255]); + $table = new Table('index_length', [ + Column::editor() + ->setUnquotedName('text') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), + ]); $table->addIndex(['text'], 'text_index', [], ['lengths' => [128]]); $this->dropAndCreateTable($table); @@ -113,11 +127,30 @@ public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes(): vo ); } - $table = new Table('text_blob_default_value'); - $table->addColumn('def_text', Types::TEXT, ['default' => 'def']); - $table->addColumn('def_text_null', Types::TEXT, ['notnull' => false, 'default' => 'def']); - $table->addColumn('def_blob', Types::BLOB, ['default' => 'def']); - $table->addColumn('def_blob_null', Types::BLOB, ['notnull' => false, 'default' => 'def']); + $table = new Table('text_blob_default_value', [ + Column::editor() + ->setUnquotedName('def_text') + ->setTypeName(Types::TEXT) + ->setDefaultValue('def') + ->create(), + Column::editor() + ->setUnquotedName('def_text_null') + ->setTypeName(Types::TEXT) + ->setNotNull(false) + ->setDefaultValue('def') + ->create(), + Column::editor() + ->setUnquotedName('def_blob') + ->setTypeName(Types::BLOB) + ->setDefaultValue('def') + ->create(), + Column::editor() + ->setUnquotedName('def_blob_null') + ->setTypeName(Types::BLOB) + ->setNotNull(false) + ->setDefaultValue('def') + ->create(), + ]); $this->dropAndCreateTable($table); @@ -147,9 +180,9 @@ public function testColumnCharset(): void $columns = $this->schemaManager->listTableColumns('test_column_charset'); - self::assertFalse($columns['id']->hasPlatformOption('charset')); - self::assertEquals('ascii', $columns['foo']->getPlatformOption('charset')); - self::assertEquals('latin1', $columns['bar']->getPlatformOption('charset')); + self::assertNull($columns['id']->getCharset()); + self::assertEquals('ascii', $columns['foo']->getCharset()); + self::assertEquals('latin1', $columns['bar']->getCharset()); } public function testAlterColumnCharset(): void @@ -171,16 +204,19 @@ public function testAlterColumnCharset(): void $table = $this->schemaManager->introspectTable($tableName); - self::assertEquals('ascii', $table->getColumn('col_text')->getPlatformOption('charset')); + self::assertEquals('ascii', $table->getColumn('col_text')->getCharset()); } public function testColumnCharsetChange(): void { - $table = new Table('test_column_charset_change'); - $table->addColumn('col_string', Types::STRING) - ->setLength(100) - ->setNotnull(true) - ->setPlatformOption('charset', 'utf8'); + $table = new Table('test_column_charset_change', [ + Column::editor() + ->setUnquotedName('col_string') + ->setTypeName(Types::STRING) + ->setCharset('utf8') + ->setLength(100) + ->create(), + ]); $this->dropAndCreateTable($table); $diffTable = clone $table; @@ -195,7 +231,7 @@ public function testColumnCharsetChange(): void 'ascii', $this->schemaManager->introspectTable('test_column_charset_change') ->getColumn('col_string') - ->getPlatformOption('charset'), + ->getCharset(), ); } @@ -213,27 +249,56 @@ public function testColumnCollation(): void $columns = $this->schemaManager->listTableColumns('test_collation'); - self::assertFalse($columns['id']->hasPlatformOption('collation')); - self::assertEquals('latin1_swedish_ci', $columns['text']->getPlatformOption('collation')); - self::assertEquals('latin1_swedish_ci', $columns['foo']->getPlatformOption('collation')); - self::assertEquals('utf8mb4_general_ci', $columns['bar']->getPlatformOption('collation')); + self::assertNull($columns['id']->getCollation()); + self::assertEquals('latin1_swedish_ci', $columns['text']->getCollation()); + self::assertEquals('latin1_swedish_ci', $columns['foo']->getCollation()); + self::assertEquals('utf8mb4_general_ci', $columns['bar']->getCollation()); self::assertInstanceOf(BlobType::class, $columns['baz']->getType()); } public function testListLobTypeColumns(): void { $tableName = 'lob_type_columns'; - $table = new Table($tableName); - - $table->addColumn('col_tinytext', Types::TEXT, ['length' => AbstractMySQLPlatform::LENGTH_LIMIT_TINYTEXT]); - $table->addColumn('col_text', Types::TEXT, ['length' => AbstractMySQLPlatform::LENGTH_LIMIT_TEXT]); - $table->addColumn('col_mediumtext', Types::TEXT, ['length' => AbstractMySQLPlatform::LENGTH_LIMIT_MEDIUMTEXT]); - $table->addColumn('col_longtext', Types::TEXT); - - $table->addColumn('col_tinyblob', Types::TEXT, ['length' => AbstractMySQLPlatform::LENGTH_LIMIT_TINYBLOB]); - $table->addColumn('col_blob', Types::BLOB, ['length' => AbstractMySQLPlatform::LENGTH_LIMIT_BLOB]); - $table->addColumn('col_mediumblob', Types::BLOB, ['length' => AbstractMySQLPlatform::LENGTH_LIMIT_MEDIUMBLOB]); - $table->addColumn('col_longblob', Types::BLOB); + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('col_tinytext') + ->setTypeName(Types::TEXT) + ->setLength(AbstractMySQLPlatform::LENGTH_LIMIT_TINYTEXT) + ->create(), + Column::editor() + ->setUnquotedName('col_text') + ->setTypeName(Types::TEXT) + ->setLength(AbstractMySQLPlatform::LENGTH_LIMIT_TEXT) + ->create(), + Column::editor() + ->setUnquotedName('col_mediumtext') + ->setTypeName(Types::TEXT) + ->setLength(AbstractMySQLPlatform::LENGTH_LIMIT_MEDIUMTEXT) + ->create(), + Column::editor() + ->setUnquotedName('col_longtext') + ->setTypeName(Types::TEXT) + ->create(), + Column::editor() + ->setUnquotedName('col_tinyblob') + ->setTypeName(Types::TEXT) + ->setLength(AbstractMySQLPlatform::LENGTH_LIMIT_TINYBLOB) + ->create(), + Column::editor() + ->setUnquotedName('col_blob') + ->setTypeName(Types::BLOB) + ->setLength(AbstractMySQLPlatform::LENGTH_LIMIT_BLOB) + ->create(), + Column::editor() + ->setUnquotedName('col_mediumblob') + ->setTypeName(Types::BLOB) + ->setLength(AbstractMySQLPlatform::LENGTH_LIMIT_MEDIUMBLOB) + ->create(), + Column::editor() + ->setUnquotedName('col_longblob') + ->setTypeName(Types::BLOB) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -277,8 +342,12 @@ public function testListLobTypeColumns(): void public function testDiffListGuidTableColumn(): void { - $offlineTable = new Table('list_guid_table_column'); - $offlineTable->addColumn('col_guid', Types::GUID); + $offlineTable = new Table('list_guid_table_column', [ + Column::editor() + ->setUnquotedName('col_guid') + ->setTypeName(Types::GUID) + ->create(), + ]); $this->dropAndCreateTable($offlineTable); @@ -296,17 +365,20 @@ public function testDiffListGuidTableColumn(): void public function testListDecimalTypeColumns(): void { $tableName = 'test_list_decimal_columns'; - $table = new Table($tableName); - - $table->addColumn('col', Types::DECIMAL, [ - 'precision' => 10, - 'scale' => 6, - ]); - - $table->addColumn('col_unsigned', Types::DECIMAL, [ - 'precision' => 10, - 'scale' => 6, - 'unsigned' => true, + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('col') + ->setTypeName(Types::DECIMAL) + ->setPrecision(10) + ->setScale(6) + ->create(), + Column::editor() + ->setUnquotedName('col_unsigned') + ->setTypeName(Types::DECIMAL) + ->setPrecision(10) + ->setScale(6) + ->setUnsigned(true) + ->create(), ]); $this->dropAndCreateTable($table); @@ -322,10 +394,18 @@ public function testListDecimalTypeColumns(): void public function testListUnsignedFloatTypeColumns(): void { $tableName = 'test_unsigned_float_columns'; - $table = new Table($tableName); - - $table->addColumn('col_unsigned', Types::FLOAT, ['unsigned' => true]); - $table->addColumn('col_smallfloat_unsigned', Types::SMALLFLOAT, ['unsigned' => true]); + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('col_unsigned') + ->setTypeName(Types::FLOAT) + ->setUnsigned(true) + ->create(), + Column::editor() + ->setUnquotedName('col_smallfloat_unsigned') + ->setTypeName(Types::SMALLFLOAT) + ->setUnsigned(true) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -339,8 +419,12 @@ public function testListUnsignedFloatTypeColumns(): void public function testJsonColumnType(): void { - $table = new Table('test_mysql_json'); - $table->addColumn('col_json', Types::JSON); + $table = new Table('test_mysql_json', [ + Column::editor() + ->setUnquotedName('col_json') + ->setTypeName(Types::JSON) + ->create(), + ]); $this->dropAndCreateTable($table); $columns = $this->schemaManager->listTableColumns('test_mysql_json'); @@ -352,16 +436,20 @@ public function testColumnDefaultCurrentTimestamp(): void { $platform = $this->connection->getDatabasePlatform(); - $table = new Table('test_column_defaults_current_timestamp'); - $currentTimeStampSql = $platform->getCurrentTimestampSQL(); - $table->addColumn( - 'col_datetime', - Types::DATETIME_MUTABLE, - ['notnull' => true, 'default' => $currentTimeStampSql], - ); - $table->addColumn('col_datetime_nullable', Types::DATETIME_MUTABLE, ['default' => $currentTimeStampSql]); + $table = new Table('test_column_defaults_current_timestamp', [ + Column::editor() + ->setUnquotedName('col_datetime') + ->setTypeName(Types::DATETIME_MUTABLE) + ->setDefaultValue($currentTimeStampSql) + ->create(), + Column::editor() + ->setUnquotedName('col_datetime_nullable') + ->setTypeName(Types::DATETIME_MUTABLE) + ->setDefaultValue($currentTimeStampSql) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -379,19 +467,48 @@ public function testColumnDefaultCurrentTimestamp(): void public function testColumnDefaultsAreValid(): void { - $table = new Table('test_column_defaults_are_valid'); - $currentTimeStampSql = $this->connection->getDatabasePlatform()->getCurrentTimestampSQL(); - $table->addColumn('col_datetime', Types::DATETIME_MUTABLE, ['default' => $currentTimeStampSql]); - $table->addColumn('col_datetime_null', Types::DATETIME_MUTABLE, ['notnull' => false, 'default' => null]); - $table->addColumn('col_int', Types::INTEGER, ['default' => 1]); - $table->addColumn('col_neg_int', Types::INTEGER, ['default' => -1]); - $table->addColumn('col_string', Types::STRING, [ - 'length' => 1, - 'default' => 'A', + + $table = new Table('test_column_defaults_are_valid', [ + Column::editor() + ->setUnquotedName('col_datetime') + ->setTypeName(Types::DATETIME_MUTABLE) + ->setDefaultValue($currentTimeStampSql) + ->create(), + Column::editor() + ->setUnquotedName('col_datetime_null') + ->setTypeName(Types::DATETIME_MUTABLE) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('col_int') + ->setTypeName(Types::INTEGER) + ->setDefaultValue(1) + ->create(), + Column::editor() + ->setUnquotedName('col_neg_int') + ->setTypeName(Types::INTEGER) + ->setDefaultValue(-1) + ->create(), + Column::editor() + ->setUnquotedName('col_string') + ->setTypeName(Types::STRING) + ->setLength(1) + ->setDefaultValue('A') + ->create(), + Column::editor() + ->setUnquotedName('col_decimal') + ->setTypeName(Types::DECIMAL) + ->setPrecision(6) + ->setScale(3) + ->setDefaultValue(-2.3) + ->create(), + Column::editor() + ->setUnquotedName('col_date') + ->setTypeName(Types::DATE_MUTABLE) + ->setDefaultValue('2012-12-12') + ->create(), ]); - $table->addColumn('col_decimal', Types::DECIMAL, ['scale' => 3, 'precision' => 6, 'default' => -2.3]); - $table->addColumn('col_date', Types::DATE_MUTABLE, ['default' => '2012-12-12']); $this->dropAndCreateTable($table); @@ -432,15 +549,27 @@ public function testColumnDefaultValuesCurrentTimeAndDate(): void $platform = $this->connection->getDatabasePlatform(); - $table = new Table('test_column_defaults_current_time_and_date'); - $currentTimestampSql = $platform->getCurrentTimestampSQL(); $currentTimeSql = $platform->getCurrentTimeSQL(); $currentDateSql = $platform->getCurrentDateSQL(); - $table->addColumn('col_datetime', Types::DATETIME_MUTABLE, ['default' => $currentTimestampSql]); - $table->addColumn('col_date', Types::DATE_MUTABLE, ['default' => $currentDateSql]); - $table->addColumn('col_time', Types::TIME_MUTABLE, ['default' => $currentTimeSql]); + $table = new Table('test_column_defaults_current_time_and_date', [ + Column::editor() + ->setUnquotedName('col_datetime') + ->setTypeName(Types::DATETIME_MUTABLE) + ->setDefaultValue($currentTimestampSql) + ->create(), + Column::editor() + ->setUnquotedName('col_date') + ->setTypeName(Types::DATE_MUTABLE) + ->setDefaultValue($currentDateSql) + ->create(), + Column::editor() + ->setUnquotedName('col_time') + ->setTypeName(Types::TIME_MUTABLE) + ->setDefaultValue($currentTimeSql) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -540,8 +669,12 @@ public function testSchemaDiffWithCustomColumnTypeWhileDatabaseTypeDiffers(): vo { Type::addType('custom_type', CustomType::class); - $metadataTable = new Table('table_with_custom_type'); - $metadataTable->addColumn('col1', 'custom_type'); + $metadataTable = new Table('table_with_custom_type', [ + Column::editor() + ->setUnquotedName('col1') + ->setTypeName('custom_type') + ->create(), + ]); $this->connection->executeStatement('DROP TABLE IF EXISTS table_with_custom_type'); @@ -551,9 +684,11 @@ public function testSchemaDiffWithCustomColumnTypeWhileDatabaseTypeDiffers(): vo $comparator = $this->schemaManager->createComparator(); $tablesDiff = $comparator->compareTables($onlineTable, $metadataTable); - $columns = $tablesDiff->getChangedColumns(); - self::assertArrayHasKey('col1', $columns); - self::assertInstanceOf(CustomType::class, $columns['col1']->getNewColumn()->getType()); + self::assertSame( + ['ALTER TABLE `table_with_custom_type` CHANGE `col1` `col1` INT NOT NULL'], + $this->connection->getDatabasePlatform()->getAlterTableSQL($tablesDiff), + ' The column should be changed from VARCHAR TO INT', + ); } public function getExpectedDefaultSchemaName(): ?string diff --git a/tests/Functional/Schema/Oracle/ComparatorTest.php b/tests/Functional/Schema/Oracle/ComparatorTest.php index c84d6a06e85..29a5a24a466 100644 --- a/tests/Functional/Schema/Oracle/ComparatorTest.php +++ b/tests/Functional/Schema/Oracle/ComparatorTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Platforms\OraclePlatform; 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; @@ -39,14 +40,18 @@ protected function setUp(): void */ public function testChangeBinaryColumnFixed(): void { - $table = new Table('comparator_test'); - $column = $table->addColumn('id', Types::BINARY, [ - 'length' => 32, - 'fixed' => true, + $table = new Table('comparator_test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::BINARY) + ->setLength(32) + ->setFixed(true) + ->create(), ]); $this->dropAndCreateTable($table); - $column->setFixed(false); + $table->getColumn('id') + ->setFixed(false); self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable( $this->schemaManager, diff --git a/tests/Functional/Schema/OracleSchemaManagerTest.php b/tests/Functional/Schema/OracleSchemaManagerTest.php index 5af01a89796..d3d099af592 100644 --- a/tests/Functional/Schema/OracleSchemaManagerTest.php +++ b/tests/Functional/Schema/OracleSchemaManagerTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\OraclePlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\TestUtil; @@ -36,11 +37,21 @@ protected function assertVarBinaryColumnIsValid(Table $table, string $columnName public function testAlterTableColumnNotNull(): void { $tableName = 'list_table_column_notnull'; - $table = new Table($tableName); - - $table->addColumn('id', Types::INTEGER); - $table->addColumn('foo', Types::INTEGER); - $table->addColumn('bar', Types::STRING, ['length' => 32]); + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -76,8 +87,13 @@ public function testListTableColumnsSameTableNamesInDifferentSchemas(): void $table = $this->createListTableColumns(); $this->dropAndCreateTable($table); - $otherTable = new Table($table->getName()); - $otherTable->addColumn('id', Types::STRING, ['length' => 32]); + $otherTable = new Table($table->getName(), [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]); $connection = TestUtil::getPrivilegedConnection(); $schemaManager = $connection->createSchemaManager(); @@ -96,10 +112,20 @@ public function testListTableColumnsSameTableNamesInDifferentSchemas(): void public function testListTableDateTypeColumns(): void { - $table = new Table('tbl_date'); - $table->addColumn('col_date', Types::DATE_MUTABLE); - $table->addColumn('col_datetime', Types::DATETIME_MUTABLE); - $table->addColumn('col_datetimetz', Types::DATETIMETZ_MUTABLE); + $table = new Table('tbl_date', [ + Column::editor() + ->setUnquotedName('col_date') + ->setTypeName(Types::DATE_MUTABLE) + ->create(), + Column::editor() + ->setUnquotedName('col_datetime') + ->setTypeName(Types::DATETIME_MUTABLE) + ->create(), + Column::editor() + ->setUnquotedName('col_datetimetz') + ->setTypeName(Types::DATETIMETZ_MUTABLE) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -119,9 +145,17 @@ public function testCreateAndListSequences(): void public function testQuotedTableNameRemainsQuotedInSchema(): void { - $table = new Table('"tester"'); - $table->addColumn('"id"', Types::INTEGER); - $table->addColumn('"name"', Types::STRING, ['length' => 32]); + $table = new Table('"tester"', [ + Column::editor() + ->setQuotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setQuotedName('name') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]); $this->dropAndCreateTable($table); diff --git a/tests/Functional/Schema/PostgreSQL/ComparatorTest.php b/tests/Functional/Schema/PostgreSQL/ComparatorTest.php index 01d3cbbe9e8..b41fdec6700 100644 --- a/tests/Functional/Schema/PostgreSQL/ComparatorTest.php +++ b/tests/Functional/Schema/PostgreSQL/ComparatorTest.php @@ -74,8 +74,12 @@ public function testCompareBinariesOfDifferentLength(): void public function testPlatformOptionsChangedColumnComparison(): void { - $table = new Table('update_json_to_jsonb_table'); - $table->addColumn('test', Types::JSON); + $table = new Table('update_json_to_jsonb_table', [ + Column::editor() + ->setUnquotedName('test') + ->setTypeName(Types::JSON) + ->create(), + ]); $onlineTable = clone $table; $table->getColumn('test') diff --git a/tests/Functional/Schema/PostgreSQL/SchemaTest.php b/tests/Functional/Schema/PostgreSQL/SchemaTest.php index 934e19ec4bb..01c3e34790b 100644 --- a/tests/Functional/Schema/PostgreSQL/SchemaTest.php +++ b/tests/Functional/Schema/PostgreSQL/SchemaTest.php @@ -10,7 +10,7 @@ use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; -use Doctrine\DBAL\Types\IntegerType; +use Doctrine\DBAL\Types\Types; final class SchemaTest extends FunctionalTestCase { @@ -24,8 +24,14 @@ public function testCreateTableWithSequenceInColumnDefinition(): void $this->dropTableIfExists('my_table'); - $options = ['default' => 'nextval(\'my_table_id_seq\'::regclass)']; - $table = new Table('my_table', [new Column('id', new IntegerType(), $options)]); + $table = new Table('my_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setDefaultValue("nextval('my_table_id_seq'::regclass)") + ->create(), + ]); + $sequence = new Sequence('my_table_id_seq'); $schema = new Schema([$table], [$sequence]); diff --git a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php index ba240931362..3bdd994d496 100644 --- a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php +++ b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Exception\TableDoesNotExist; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Schema; @@ -58,15 +59,23 @@ public function testSupportDomainTypeFallback(): void public function testAlterTableAutoIncrementAdd(): void { - $tableFrom = new Table('autoinc_table_add'); - $tableFrom->addColumn('id', Types::INTEGER); + $tableFrom = new Table('autoinc_table_add', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->dropAndCreateTable($tableFrom); $tableFrom = $this->schemaManager->introspectTable('autoinc_table_add'); self::assertFalse($tableFrom->getColumn('id')->getAutoincrement()); - $tableTo = new Table('autoinc_table_add'); - $column = $tableTo->addColumn('id', Types::INTEGER); - $column->setAutoincrement(true); + $tableTo = new Table('autoinc_table_add', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $platform = $this->connection->getDatabasePlatform(); $diff = $this->schemaManager->createComparator() @@ -79,15 +88,23 @@ public function testAlterTableAutoIncrementAdd(): void public function testAlterTableAutoIncrementDrop(): void { - $tableFrom = new Table('autoinc_table_drop'); - $column = $tableFrom->addColumn('id', Types::INTEGER); - $column->setAutoincrement(true); + $tableFrom = new Table('autoinc_table_drop', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $this->dropAndCreateTable($tableFrom); $tableFrom = $this->schemaManager->introspectTable('autoinc_table_drop'); self::assertTrue($tableFrom->getColumn('id')->getAutoincrement()); - $tableTo = new Table('autoinc_table_drop'); - $tableTo->addColumn('id', Types::INTEGER); + $tableTo = new Table('autoinc_table_drop', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $platform = $this->connection->getDatabasePlatform(); $diff = $this->schemaManager->createComparator() @@ -101,14 +118,28 @@ public function testAlterTableAutoIncrementDrop(): void public function testListSameTableNameColumnsWithDifferentSchema(): void { $this->connection->executeStatement('CREATE SCHEMA another'); - $table = new Table('table'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('name', Types::TEXT); + $table = new Table('table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::TEXT) + ->create(), + ]); $this->schemaManager->createTable($table); - $anotherSchemaTable = new Table('another.table'); - $anotherSchemaTable->addColumn('id', Types::TEXT); - $anotherSchemaTable->addColumn('email', Types::TEXT); + $anotherSchemaTable = new Table('another.table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::TEXT) + ->create(), + Column::editor() + ->setUnquotedName('email') + ->setTypeName(Types::TEXT) + ->create(), + ]); $this->schemaManager->createTable($anotherSchemaTable); $table = $this->schemaManager->introspectTable('table'); @@ -144,9 +175,17 @@ public function testReturnQuotedAssets(): void public function testDefaultValueCharacterVarying(): void { - $testTable = new Table('dbal511_default'); - $testTable->addColumn('id', Types::INTEGER); - $testTable->addColumn('def', Types::STRING, ['default' => 'foo']); + $testTable = new Table('dbal511_default', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('def') + ->setTypeName(Types::STRING) + ->setDefaultValue('foo') + ->create(), + ]); $testTable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -161,10 +200,13 @@ public function testDefaultValueCharacterVarying(): void public function testJsonDefaultValue(): void { - $testTable = new Table('test_json'); - $testTable - ->addColumn('foo', Types::JSON) - ->setDefault('{"key": "value with a single quote \' in string value"}'); + $testTable = new Table('test_json', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::JSON) + ->setDefaultValue('{"key": "value with a single quote \' in string value"}') + ->create(), + ]); $this->dropAndCreateTable($testTable); $columns = $this->schemaManager->listTableColumns('test_json'); @@ -175,9 +217,17 @@ public function testJsonDefaultValue(): void public function testBooleanDefault(): void { - $table = new Table('ddc2843_bools'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('checked', Types::BOOLEAN, ['default' => false]); + $table = new Table('ddc2843_bools', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('checked') + ->setTypeName(Types::BOOLEAN) + ->setDefaultValue(false) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -192,13 +242,18 @@ public function testBooleanDefault(): void public function testGeneratedColumn(): void { - $table = new Table('ddc6198_generated_always_as'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn( - 'idIsOdd', - Types::BOOLEAN, - ['columnDefinition' => 'boolean GENERATED ALWAYS AS (id % 2 = 1) STORED', 'notNull' => false], - ); + $table = new Table('ddc6198_generated_always_as', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('idIsOdd') + ->setTypeName(Types::BOOLEAN) + ->setColumnDefinition('boolean GENERATED ALWAYS AS (id % 2 = 1) STORED') + ->setNotNull(false) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -301,10 +356,20 @@ public function testListTablesExcludesViews(): void public function testPartialIndexes(): void { - $offlineTable = new Table('person'); - $offlineTable->addColumn('id', Types::INTEGER); - $offlineTable->addColumn('name', Types::STRING); - $offlineTable->addColumn('email', Types::STRING); + $offlineTable = new Table('person', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('email') + ->setTypeName(Types::STRING) + ->create(), + ]); $offlineTable->addUniqueIndex(['id', 'name'], 'simple_partial_index', ['where' => '(id IS NULL)']); $this->dropAndCreateTable($offlineTable); @@ -333,19 +398,45 @@ public function testJsonbColumn(): void public function testListNegativeColumnDefaultValue(): void { - $table = new Table('test_default_negative'); - $table->addColumn('col_smallint', Types::SMALLINT, ['default' => -1]); - $table->addColumn('col_integer', Types::INTEGER, ['default' => -1]); - $table->addColumn('col_bigint', Types::BIGINT, ['default' => -1]); - $table->addColumn('col_float', Types::FLOAT, ['default' => -1.1]); - $table->addColumn('col_smallfloat', Types::SMALLFLOAT, ['default' => -1.1]); - $table->addColumn('col_decimal', Types::DECIMAL, [ - 'precision' => 2, - 'scale' => 1, - 'default' => -1.1, + $table = new Table('test_default_negative', [ + Column::editor() + ->setUnquotedName('col_smallint') + ->setTypeName(Types::SMALLINT) + ->setDefaultValue(-1) + ->create(), + Column::editor() + ->setUnquotedName('col_integer') + ->setTypeName(Types::INTEGER) + ->setDefaultValue(-1) + ->create(), + Column::editor() + ->setUnquotedName('col_bigint') + ->setTypeName(Types::BIGINT) + ->setDefaultValue(-1) + ->create(), + Column::editor() + ->setUnquotedName('col_float') + ->setTypeName(Types::FLOAT) + ->setDefaultValue(-1.1) + ->create(), + Column::editor() + ->setUnquotedName('col_smallfloat') + ->setTypeName(Types::SMALLFLOAT) + ->setDefaultValue(-1.1) + ->create(), + Column::editor() + ->setUnquotedName('col_decimal') + ->setTypeName(Types::DECIMAL) + ->setPrecision(2) + ->setScale(1) + ->setDefaultValue(-1.1) + ->create(), + Column::editor() + ->setUnquotedName('col_string') + ->setTypeName(Types::STRING) + ->setDefaultValue('(-1)') + ->create(), ]); - $table->addColumn('col_string', Types::STRING, ['default' => '(-1)']); - $this->dropAndCreateTable($table); $columns = $this->schemaManager->listTableColumns('test_default_negative'); @@ -369,12 +460,18 @@ public static function serialTypes(): iterable } #[DataProvider('serialTypes')] - public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValue(string $type): void + public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValue(string $typeName): void { - $tableName = 'test_serial_type_' . $type; - - $table = new Table($tableName); - $table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false]); + $tableName = 'test_serial_type_' . $typeName; + + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName($typeName) + ->setAutoincrement(true) + ->setNotNull(false) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -388,8 +485,15 @@ public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValueEvenW { $tableName = 'test_serial_type_with_default_' . $type; - $table = new Table($tableName); - $table->addColumn('id', $type, ['autoincrement' => true, 'notnull' => false, 'default' => 1]); + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName($type) + ->setAutoincrement(true) + ->setNotNull(false) + ->setDefaultValue(1) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -401,17 +505,25 @@ public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValueEvenW #[DataProvider('autoIncrementTypeMigrations')] public function testAlterTableAutoIncrementIntToBigInt(string $from, string $to, string $expected): void { - $table = new Table('autoinc_type_modification'); - $column = $table->addColumn('id', $from); - $column->setAutoincrement(true); + $table = new Table('autoinc_type_modification', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName($from) + ->setAutoincrement(true) + ->create(), + ]); $this->dropAndCreateTable($table); $oldTable = $this->schemaManager->introspectTable('autoinc_type_modification'); self::assertTrue($oldTable->getColumn('id')->getAutoincrement()); - $newTable = new Table('autoinc_type_modification'); - $column = $newTable->addColumn('id', $to); - $column->setAutoincrement(true); + $newTable = new Table('autoinc_type_modification', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName($to) + ->setAutoincrement(true) + ->create(), + ]); $diff = $this->schemaManager->createComparator() ->compareTables($oldTable, $newTable); diff --git a/tests/Functional/Schema/SQLServerSchemaManagerTest.php b/tests/Functional/Schema/SQLServerSchemaManagerTest.php index a2579512927..b0b2a060c74 100644 --- a/tests/Functional/Schema/SQLServerSchemaManagerTest.php +++ b/tests/Functional/Schema/SQLServerSchemaManagerTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Types\Types; @@ -18,45 +19,72 @@ protected function supportsPlatform(AbstractPlatform $platform): bool public function testColumnCollation(): void { - $table = new Table($tableName = 'test_collation'); - $column = $table->addColumn('test', Types::STRING, ['length' => 32]); + $table = new Table('test_collation', [ + Column::editor() + ->setUnquotedName('test') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]); $this->dropAndCreateTable($table); - $columns = $this->schemaManager->listTableColumns($tableName); + $columns = $this->schemaManager->listTableColumns('test_collation'); // SQL Server should report a default collation on the column - self::assertTrue($columns['test']->hasPlatformOption('collation')); + self::assertNotNull($columns['test']->getCollation()); - $column->setPlatformOption('collation', $collation = 'Icelandic_CS_AS'); + $table->getColumn('test') + ->setPlatformOption('collation', 'Icelandic_CS_AS'); $this->dropAndCreateTable($table); - $columns = $this->schemaManager->listTableColumns($tableName); + $columns = $this->schemaManager->listTableColumns('test_collation'); - self::assertEquals($collation, $columns['test']->getPlatformOption('collation')); + self::assertEquals('Icelandic_CS_AS', $columns['test']->getCollation()); } public function testDefaultConstraints(): void { - $oldTable = new Table('sqlsrv_default_constraints'); - $oldTable->addColumn('no_default', Types::STRING, ['length' => 32]); - $oldTable->addColumn('df_integer', Types::INTEGER, ['default' => 666]); - $oldTable->addColumn('df_string_1', Types::STRING, [ - 'length' => 32, - 'default' => 'foobar', - ]); - $oldTable->addColumn('df_string_2', Types::STRING, [ - 'length' => 32, - 'default' => 'Doctrine rocks!!!', - ]); - $oldTable->addColumn('df_string_3', Types::STRING, [ - 'length' => 32, - 'default' => 'another default value', - ]); - $oldTable->addColumn('df_string_4', Types::STRING, [ - 'length' => 32, - 'default' => 'column to rename', + $oldTable = new Table('sqlsrv_default_constraints', [ + Column::editor() + ->setUnquotedName('no_default') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + Column::editor() + ->setUnquotedName('df_integer') + ->setTypeName(Types::INTEGER) + ->setDefaultValue(666) + ->create(), + Column::editor() + ->setUnquotedName('df_string_1') + ->setTypeName(Types::STRING) + ->setLength(32) + ->setDefaultValue('foobar') + ->create(), + Column::editor() + ->setUnquotedName('df_string_2') + ->setTypeName(Types::STRING) + ->setLength(32) + ->setDefaultValue('Doctrine rocks!!!') + ->create(), + Column::editor() + ->setUnquotedName('df_string_3') + ->setTypeName(Types::STRING) + ->setLength(32) + ->setDefaultValue('another default value') + ->create(), + Column::editor() + ->setUnquotedName('df_string_4') + ->setTypeName(Types::STRING) + ->setLength(32) + ->setDefaultValue('column to rename') + ->create(), + Column::editor() + ->setUnquotedName('df_boolean') + ->setTypeName(Types::BOOLEAN) + ->setDefaultValue(true) + ->create(), ]); - $oldTable->addColumn('df_boolean', Types::BOOLEAN, ['default' => true]); $newTable = clone $oldTable; diff --git a/tests/Functional/Schema/SQLiteSchemaManagerTest.php b/tests/Functional/Schema/SQLiteSchemaManagerTest.php index 26edb55a733..51be29ed087 100644 --- a/tests/Functional/Schema/SQLiteSchemaManagerTest.php +++ b/tests/Functional/Schema/SQLiteSchemaManagerTest.php @@ -111,10 +111,10 @@ public function testColumnCollation(): void $columns = $this->schemaManager->listTableColumns('test_collation'); - self::assertArrayNotHasKey('collation', $columns['id']->getPlatformOptions()); - self::assertEquals('BINARY', $columns['text']->getPlatformOption('collation')); - self::assertEquals('BINARY', $columns['foo']->getPlatformOption('collation')); - self::assertEquals('NOCASE', $columns['bar']->getPlatformOption('collation')); + self::assertNull($columns['id']->getCollation()); + self::assertEquals('BINARY', $columns['text']->getCollation()); + self::assertEquals('BINARY', $columns['foo']->getCollation()); + self::assertEquals('NOCASE', $columns['bar']->getCollation()); } /** @@ -158,11 +158,18 @@ public function testListTableColumnsWithWhitespacesInTypeDeclarations(): void self::assertSame(100, $columns['bar']->getLength()); } - public function testPrimaryKeyNoAutoIncrement(): void + public function testPrimaryKeyAutoIncrement(): void { - $table = new Table('test_pk_auto_increment'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('text', Types::TEXT); + $table = new Table('test_pk_auto_increment', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('text') + ->setTypeName(Types::TEXT) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -186,10 +193,24 @@ public function testPrimaryKeyNoAutoIncrement(): void public function testOnlyOwnCommentIsParsed(): void { - $table = new Table('own_column_comment'); - $table->addColumn('col1', Types::STRING, ['length' => 16]); - $table->addColumn('col2', Types::STRING, ['length' => 16, 'comment' => 'Column #2']); - $table->addColumn('col3', Types::STRING, ['length' => 16]); + $table = new Table('own_column_comment', [ + Column::editor() + ->setUnquotedName('col1') + ->setTypeName(Types::STRING) + ->setLength(16) + ->create(), + Column::editor() + ->setUnquotedName('col2') + ->setTypeName(Types::STRING) + ->setLength(16) + ->setComment('Column #2') + ->create(), + Column::editor() + ->setUnquotedName('col3') + ->setTypeName(Types::STRING) + ->setLength(16) + ->create(), + ]); $sm = $this->connection->createSchemaManager(); $sm->createTable($table); @@ -238,16 +259,26 @@ public function testAlterTableWithSchema(): void { $this->dropTableIfExists('t'); - $table = new Table('main.t'); - $table->addColumn('a', Types::INTEGER); + $table = new Table('main.t', [ + Column::editor() + ->setUnquotedName('a') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->schemaManager->createTable($table); self::assertSame(['a'], array_keys($this->schemaManager->listTableColumns('t'))); $tableDiff = new TableDiff($table, changedColumns: [ 'a' => new ColumnDiff( - new Column('a', Type::getType(Types::INTEGER)), - new Column('b', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('a') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('b') + ->setTypeName(Types::INTEGER) + ->create(), ), ]); $this->schemaManager->alterTable($tableDiff); diff --git a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php index f64d550fdac..0de733798c3 100644 --- a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -13,6 +13,7 @@ use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Schema\AbstractAsset; use Doctrine\DBAL\Schema\AbstractSchemaManager; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ComparatorConfig; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\Index; @@ -262,18 +263,42 @@ public function testRenameTable(): void public function createListTableColumns(): Table { - $table = new Table('list_table_columns'); - $table->addColumn('id', Types::INTEGER, ['notnull' => true]); - $table->addColumn( - 'test', - Types::STRING, - ['length' => 255, 'notnull' => false, 'default' => 'expected default'], - ); - $table->addColumn('foo', Types::TEXT, ['notnull' => true]); - $table->addColumn('bar', Types::DECIMAL, ['precision' => 10, 'scale' => 4, 'notnull' => false]); - $table->addColumn('baz1', Types::DATETIME_MUTABLE); - $table->addColumn('baz2', Types::TIME_MUTABLE); - $table->addColumn('baz3', Types::DATE_MUTABLE); + $table = new Table('list_table_columns', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('test') + ->setTypeName(Types::STRING) + ->setLength(255) + ->setNotNull(false) + ->setDefaultValue('expected default') + ->create(), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::TEXT) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::DECIMAL) + ->setPrecision(10) + ->setScale(4) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('baz1') + ->setTypeName(Types::DATETIME_MUTABLE) + ->create(), + Column::editor() + ->setUnquotedName('baz2') + ->setTypeName(Types::TIME_MUTABLE) + ->create(), + Column::editor() + ->setUnquotedName('baz3') + ->setTypeName(Types::DATE_MUTABLE) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -357,8 +382,14 @@ public function testListTableColumnsWithFixedStringColumn(): void { $tableName = 'test_list_table_fixed_string'; - $table = new Table($tableName); - $table->addColumn('column_char', Types::STRING, ['fixed' => true, 'length' => 2]); + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('column_char') + ->setTypeName(Types::STRING) + ->setFixed(true) + ->setLength(2) + ->create(), + ]); $this->schemaManager->createTable($table); @@ -436,8 +467,12 @@ public function testDropAndCreateUniqueConstraint(): void self::markTestSkipped('SQLite does not support adding constraints to a table'); } - $table = new Table('test_unique_constraint'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('test_unique_constraint', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->dropAndCreateTable($table); $uniqueConstraint = UniqueConstraint::editor() @@ -676,25 +711,29 @@ public function testCreateAndListViews(): void public function testUpdateSchemaWithForeignKeyRenaming(): void { - $table = new Table('test_fk_base'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('test_fk_base', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); + $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') ->create(), ); - $tableFK = new Table( - 'test_fk_rename', - [], - [], - [], - [], - [], - $this->schemaManager->createSchemaConfig()->toTableConfiguration(), - ); - $tableFK->addColumn('id', Types::INTEGER); - $tableFK->addColumn('fk_id', Types::INTEGER); + $tableFK = new Table('test_fk_rename', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('fk_id') + ->setTypeName(Types::INTEGER) + ->create(), + ], [], [], [], [], $this->schemaManager->createSchemaConfig()->toTableConfiguration()); $tableFK->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -709,17 +748,16 @@ public function testUpdateSchemaWithForeignKeyRenaming(): void $this->schemaManager->createTable($table); $this->schemaManager->createTable($tableFK); - $tableFKNew = new Table( - 'test_fk_rename', - [], - [], - [], - [], - [], - $this->schemaManager->createSchemaConfig()->toTableConfiguration(), - ); - $tableFKNew->addColumn('id', Types::INTEGER); - $tableFKNew->addColumn('rename_fk_id', Types::INTEGER); + $tableFKNew = new Table('test_fk_rename', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('rename_fk_id') + ->setTypeName(Types::INTEGER) + ->create(), + ], [], [], [], [], $this->schemaManager->createSchemaConfig()->toTableConfiguration()); $tableFKNew->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -748,16 +786,24 @@ public function testUpdateSchemaWithForeignKeyRenaming(): void public function testRenameIndexUsedInForeignKeyConstraint(): void { - $primaryTable = new Table('test_rename_index_primary'); - $primaryTable->addColumn('id', Types::INTEGER); + $primaryTable = new Table('test_rename_index_primary', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $primaryTable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') ->create(), ); - $foreignTable = new Table('test_rename_index_foreign'); - $foreignTable->addColumn('fk', Types::INTEGER); + $foreignTable = new Table('test_rename_index_foreign', [ + Column::editor() + ->setUnquotedName('fk') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $foreignTable->addIndex(['fk'], 'rename_index_fk_idx'); $foreignTable->addForeignKeyConstraint( 'test_rename_index_primary', @@ -790,12 +836,18 @@ public function testRenameIndexUsedInForeignKeyConstraint(): void public function testChangeColumnsTypeWithDefaultValue(): void { - $oldTable = new Table('column_def_change_type'); - - $oldTable->addColumn('col_int', 'smallint', ['default' => 666]); - $oldTable->addColumn('col_string', 'string', [ - 'length' => 3, - 'default' => 'foo', + $oldTable = new Table('column_def_change_type', [ + Column::editor() + ->setUnquotedName('col_int') + ->setTypeName('smallint') + ->setDefaultValue(666) + ->create(), + Column::editor() + ->setUnquotedName('col_string') + ->setTypeName('string') + ->setLength(3) + ->setDefaultValue('foo') + ->create(), ]); $this->dropAndCreateTable($oldTable); @@ -827,8 +879,12 @@ public function testChangeColumnsTypeWithDefaultValue(): void public function testListTableWithBlob(): void { - $table = new Table('test_blob_table'); - $table->addColumn('binarydata', Types::BLOB, []); + $table = new Table('test_blob_table', [ + Column::editor() + ->setUnquotedName('binarydata') + ->setTypeName(Types::BLOB) + ->create(), + ]); $this->schemaManager->createTable($table); @@ -841,10 +897,16 @@ public function testListTableWithBlob(): void public function testListTableFloatTypeColumns(): void { $tableName = 'test_float_columns'; - $table = new Table($tableName); - - $table->addColumn('col_float', Types::FLOAT); - $table->addColumn('col_smallfloat', Types::SMALLFLOAT); + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('col_float') + ->setTypeName(Types::FLOAT) + ->create(), + Column::editor() + ->setUnquotedName('col_smallfloat') + ->setTypeName(Types::SMALLFLOAT) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -871,69 +933,88 @@ protected function createTestTable(string $name = 'test_table', array $data = [] /** @param mixed[] $options */ protected function getTestTable(string $name, array $options = []): Table { - $table = new Table( - $name, - [], - [], - [], - [], - $options, - $this->schemaManager->createSchemaConfig()->toTableConfiguration(), - ); - $table->addColumn('id', Types::INTEGER, ['notnull' => true]); + $table = new Table($name, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('test') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), + Column::editor() + ->setUnquotedName('foreign_key_test') + ->setTypeName(Types::INTEGER) + ->setNotNull(false) + ->create(), + ], [], [], [], $options, $this->schemaManager->createSchemaConfig()->toTableConfiguration()); + $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') ->create(), ); - $table->addColumn('test', Types::STRING, ['length' => 255]); - $table->addColumn('foreign_key_test', Types::INTEGER); return $table; } protected function getTestCompositeTable(string $name): Table { - $table = new Table( - $name, - [], - [], - [], - [], - [], - $this->schemaManager->createSchemaConfig()->toTableConfiguration(), - ); - $table->addColumn('id', Types::INTEGER, ['notnull' => true]); - $table->addColumn('other_id', Types::INTEGER, ['notnull' => true]); + $table = new Table($name, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('other_id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('test') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), + ], [], [], [], [], $this->schemaManager->createSchemaConfig()->toTableConfiguration()); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id', 'other_id') ->create(), ); - $table->addColumn('test', Types::STRING, ['length' => 255]); return $table; } public function testColumnDefaultLifecycle(): void { - $oldTable = new Table('col_def_lifecycle'); - $oldTable->addColumn('id', Types::INTEGER, ['autoincrement' => true]); - $oldTable->addColumn('column1', Types::STRING, [ - 'length' => 1, - 'default' => null, - ]); - $oldTable->addColumn('column2', Types::STRING, [ - 'length' => 1, - 'default' => '', - ]); - $oldTable->addColumn('column3', Types::STRING, [ - 'length' => 8, - 'default' => 'default1', - ]); - $oldTable->addColumn('column4', Types::INTEGER, [ - 'length' => 1, - 'default' => 0, + $oldTable = new Table('col_def_lifecycle', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('column1') + ->setTypeName(Types::STRING) + ->setLength(1) + ->create(), + Column::editor() + ->setUnquotedName('column2') + ->setTypeName(Types::STRING) + ->setLength(1) + ->setDefaultValue('') + ->create(), + Column::editor() + ->setUnquotedName('column3') + ->setTypeName(Types::STRING) + ->setLength(8) + ->setDefaultValue('default1') + ->create(), + Column::editor() + ->setUnquotedName('column4') + ->setTypeName(Types::INTEGER) + ->setDefaultValue(0) + ->create(), ]); $oldTable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() @@ -978,9 +1059,19 @@ public function testListTableWithBinary(): void { $tableName = 'test_binary_table'; - $table = new Table($tableName); - $table->addColumn('column_binary', Types::BINARY, ['length' => 16, 'fixed' => true]); - $table->addColumn('column_varbinary', Types::BINARY, ['length' => 32]); + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName('column_binary') + ->setTypeName(Types::BINARY) + ->setLength(16) + ->setFixed(true) + ->create(), + Column::editor() + ->setUnquotedName('column_varbinary') + ->setTypeName(Types::BINARY) + ->setLength(32) + ->create(), + ]); $this->schemaManager->createTable($table); @@ -1026,8 +1117,13 @@ public function testListTableDetailsWithFullQualifiedTableName(): void $primaryTableName = 'primary_table'; $foreignTableName = 'foreign_table'; - $table = new Table($foreignTableName); - $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); + $table = new Table($foreignTableName, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -1036,10 +1132,22 @@ public function testListTableDetailsWithFullQualifiedTableName(): void $this->dropAndCreateTable($table); - $table = new Table($primaryTableName); - $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('foo', Types::INTEGER); - $table->addColumn('bar', Types::STRING, ['length' => 32]); + $table = new Table($primaryTableName, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]); $table->addForeignKeyConstraint($foreignTableName, ['foo'], ['id']); $table->addIndex(['bar']); $table->addPrimaryKeyConstraint( @@ -1066,17 +1174,28 @@ public function testListTableDetailsWithFullQualifiedTableName(): void public function testDoesNotListIndexesImplicitlyCreatedByForeignKeys(): void { - $primaryTable = new Table('test_list_index_impl_primary'); - $primaryTable->addColumn('id', Types::INTEGER); + $primaryTable = new Table('test_list_index_impl_primary', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $primaryTable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') ->create(), ); - $foreignTable = new Table('test_list_index_impl_foreign'); - $foreignTable->addColumn('fk1', Types::INTEGER); - $foreignTable->addColumn('fk2', Types::INTEGER); + $foreignTable = new Table('test_list_index_impl_foreign', [ + Column::editor() + ->setUnquotedName('fk1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('fk2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $foreignTable->addIndex(['fk1'], 'explicit_fk1_idx'); $foreignTable->addForeignKeyConstraint('test_list_index_impl_primary', ['fk1'], ['id']); $foreignTable->addForeignKeyConstraint('test_list_index_impl_primary', ['fk2'], ['id']); @@ -1166,9 +1285,18 @@ static function (Sequence $sequence) use ($sequenceName): bool { public function testPrimaryKeyAutoIncrement(): void { - $table = new Table('test_pk_auto_increment'); - $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('text', Types::STRING, ['length' => 1]); + $table = new Table('test_pk_auto_increment', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('text') + ->setTypeName(Types::STRING) + ->setLength(1) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -1201,9 +1329,17 @@ public function testGenerateAnIndexWithPartialColumnLength(): void ); } - $table = new Table('test_partial_column_index'); - $table->addColumn('long_column', Types::STRING, ['length' => 40]); - $table->addColumn('standard_column', Types::INTEGER); + $table = new Table('test_partial_column_index', [ + Column::editor() + ->setUnquotedName('long_column') + ->setTypeName(Types::STRING) + ->setLength(40) + ->create(), + Column::editor() + ->setUnquotedName('standard_column') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['long_column'], 'partial_long_column_idx', [], ['lengths' => [4]]); $table->addIndex(['standard_column', 'long_column'], 'standard_and_partial_idx', [], ['lengths' => [null, 2]]); @@ -1215,8 +1351,12 @@ public function testGenerateAnIndexWithPartialColumnLength(): void public function testCommentInTable(): void { - $table = new Table('table_with_comment'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('table_with_comment', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->setComment('\'\\ Foo with control characters \'\\'); $this->dropAndCreateTable($table); @@ -1289,9 +1429,16 @@ public function testQuotedIdentifiers(): void ); } - $artists = new Table('"Artists"'); - $artists->addColumn('"Id"', Types::INTEGER); - $artists->addColumn('"Name"', Types::INTEGER); + $artists = new Table('"Artists"', [ + Column::editor() + ->setQuotedName('Id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setQuotedName('Name') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $artists->addIndex(['"Name"'], '"Idx_Artist_Name"'); $artists->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() @@ -1300,9 +1447,16 @@ public function testQuotedIdentifiers(): void ); $artists->setComment('"Artists" table'); - $tracks = new Table('"Tracks"'); - $tracks->addColumn('"Id"', Types::INTEGER); - $tracks->addColumn('"Artist_Id"', Types::INTEGER); + $tracks = new Table('"Tracks"', [ + Column::editor() + ->setQuotedName('Id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setQuotedName('Artist_Id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tracks->addIndex(['"Artist_Id"'], '"Idx_Artist_Id"'); $tracks->addForeignKeyConstraint( '"Artists"', @@ -1442,9 +1596,16 @@ public function testChangeIndexWithForeignKeys(): void public function testSwitchPrimaryKeyOrder(): void { - $prototype = new Table('test_switch_pk_order'); - $prototype->addColumn('foo_id', Types::INTEGER); - $prototype->addColumn('bar_id', Types::INTEGER); + $prototype = new Table('test_switch_pk_order', [ + Column::editor() + ->setUnquotedName('foo_id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $oldPrimaryKeyConstraint = PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('foo_id', 'bar_id') @@ -1479,9 +1640,17 @@ public function testSwitchPrimaryKeyOrder(): void public function testDropColumnWithDefault(): void { - $table = new Table('drop_column_with_default'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('todrop', Types::INTEGER, ['default' => 10]); + $table = new Table('drop_column_with_default', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('todrop') + ->setTypeName(Types::INTEGER) + ->setDefaultValue(10) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -1534,14 +1703,22 @@ public function testTableWithSchema(): void ->setUnquotedColumnNames('id') ->create(); - $nestedRelatedTable = new Table('nested.schemarelated'); - $column = $nestedRelatedTable->addColumn('id', Types::INTEGER); - $column->setAutoincrement(true); + $nestedRelatedTable = new Table('nested.schemarelated', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $nestedRelatedTable->addPrimaryKeyConstraint($primaryKeyConstraint); - $nestedSchemaTable = new Table('nested.schematable'); - $column = $nestedSchemaTable->addColumn('id', Types::INTEGER); - $column->setAutoincrement(true); + $nestedSchemaTable = new Table('nested.schematable', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $nestedSchemaTable->addPrimaryKeyConstraint($primaryKeyConstraint); $nestedSchemaTable->addForeignKeyConstraint($nestedRelatedTable->getName(), ['id'], ['id']); $nestedSchemaTable->setComment('This is a comment'); diff --git a/tests/Functional/Schema/SchemaManagerTest.php b/tests/Functional/Schema/SchemaManagerTest.php index e962eff319c..593b57e3f74 100644 --- a/tests/Functional/Schema/SchemaManagerTest.php +++ b/tests/Functional/Schema/SchemaManagerTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Exception; use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Exception\InvalidName; use Doctrine\DBAL\Schema\Name\UnqualifiedName; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; @@ -35,8 +36,12 @@ public function testEmptyDiffRegardlessOfForeignTableQuotes(string $foreignTable $this->dropAndCreateSchema(UnqualifiedName::unquoted('other_schema')); - $tableForeign = new Table($foreignTableName); - $tableForeign->addColumn('id', 'integer'); + $tableForeign = new Table($foreignTableName, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableForeign->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -44,9 +49,16 @@ public function testEmptyDiffRegardlessOfForeignTableQuotes(string $foreignTable ); $this->dropAndCreateTable($tableForeign); - $tableTo = new Table('other_schema.other_table'); - $tableTo->addColumn('id', 'integer'); - $tableTo->addColumn('user_id', 'integer'); + $tableTo = new Table('other_schema.other_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('user_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableTo->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -82,9 +94,17 @@ public function testDropIndexInAnotherSchema(string $tableName): void $this->dropAndCreateSchema(UnqualifiedName::unquoted('other_schema')); $this->dropAndCreateSchema(UnqualifiedName::quoted('case')); - $tableFrom = new Table($tableName); - $tableFrom->addColumn('id', Types::INTEGER); - $tableFrom->addColumn('name', Types::STRING, ['length' => 32]); + $tableFrom = new Table($tableName, [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]); $tableFrom->addUniqueIndex(['name'], 'some_table_name_unique_index'); $this->dropAndCreateTable($tableFrom); @@ -120,8 +140,13 @@ public function testAutoIncrementColumnIntrospection(bool $autoincrement): void self::markTestSkipped('This test is only supported on platforms that have autoincrement'); } - $table = new Table('test_autoincrement'); - $table->addColumn('id', Types::INTEGER, ['autoincrement' => $autoincrement]); + $table = new Table('test_autoincrement', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement($autoincrement) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -150,9 +175,17 @@ public function testAutoIncrementColumnInCompositePrimaryKeyIntrospection(bool $ ); } - $table = new Table('test_autoincrement'); - $table->addColumn('id1', Types::INTEGER, ['autoincrement' => $autoincrement]); - $table->addColumn('id2', Types::INTEGER); + $table = new Table('test_autoincrement', [ + Column::editor() + ->setUnquotedName('id1') + ->setTypeName(Types::INTEGER) + ->setAutoincrement($autoincrement) + ->create(), + Column::editor() + ->setUnquotedName('id2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id1', 'id2') diff --git a/tests/Functional/Schema/UniqueConstraintTest.php b/tests/Functional/Schema/UniqueConstraintTest.php index 9ac2c1a302b..532dca1bb0f 100644 --- a/tests/Functional/Schema/UniqueConstraintTest.php +++ b/tests/Functional/Schema/UniqueConstraintTest.php @@ -8,7 +8,6 @@ use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\UniqueConstraint; use Doctrine\DBAL\Tests\FunctionalTestCase; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; final class UniqueConstraintTest extends FunctionalTestCase @@ -18,9 +17,16 @@ public function testUnnamedUniqueConstraint(): void $this->dropTableIfExists('users'); $users = new Table('users', [ - new Column('id', Type::getType(Types::INTEGER)), - new Column('username', Type::getType(Types::STRING), ['length' => 32]), - new Column('email', Type::getType(Types::STRING), ['length' => 255]), + Column::editor() + ->setUnquotedName('username') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + Column::editor() + ->setUnquotedName('email') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), ], [], [ UniqueConstraint::editor() ->setUnquotedColumnNames('username') diff --git a/tests/Functional/StatementTest.php b/tests/Functional/StatementTest.php index 5d3dcd0d66b..2f1103130df 100644 --- a/tests/Functional/StatementTest.php +++ b/tests/Functional/StatementTest.php @@ -11,6 +11,7 @@ use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\TestUtil; @@ -29,9 +30,17 @@ class StatementTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('stmt_test'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('name', Types::TEXT, ['notnull' => false]); + $table = new Table('stmt_test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::TEXT) + ->setNotNull(false) + ->create(), + ]); $this->dropAndCreateTable($table); } @@ -70,9 +79,17 @@ public function testReuseStatementWithLongerResults(): void self::markTestIncomplete("PDO_OCI doesn't support fetching blobs via PDOStatement::fetchAll()"); } - $table = new Table('stmt_longer_results'); - $table->addColumn('param', Types::STRING, ['length' => 24]); - $table->addColumn('val', Types::TEXT); + $table = new Table('stmt_longer_results', [ + Column::editor() + ->setUnquotedName('param') + ->setTypeName(Types::STRING) + ->setLength(24) + ->create(), + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::TEXT) + ->create(), + ]); $this->dropAndCreateTable($table); $row1 = [ @@ -112,8 +129,13 @@ public function testFetchLongBlob(): void // but is still not enough to store a LONGBLOB of the max possible size ini_set('memory_limit', '4G'); - $table = new Table('stmt_long_blob'); - $table->addColumn('contents', Types::BLOB, ['length' => 0xFFFFFFFF]); + $table = new Table('stmt_long_blob', [ + Column::editor() + ->setUnquotedName('contents') + ->setTypeName(Types::BLOB) + ->setLength(0xFFFFFFFF) + ->create(), + ]); $this->dropAndCreateTable($table); $contents = base64_decode(<<<'EOF' diff --git a/tests/Functional/TemporaryTableTest.php b/tests/Functional/TemporaryTableTest.php index 9349827d6c2..7fe80fa1678 100644 --- a/tests/Functional/TemporaryTableTest.php +++ b/tests/Functional/TemporaryTableTest.php @@ -10,7 +10,6 @@ use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use Throwable; @@ -24,7 +23,10 @@ public function testDropTemporaryTableNotAutoCommitTransaction(): void self::markTestSkipped('Test does not work on Oracle.'); } - $column = new Column('id', Type::getType(Types::INTEGER)); + $column = Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(); $tempTable = $platform->getTemporaryTableName('my_temporary'); @@ -32,8 +34,12 @@ public function testDropTemporaryTableNotAutoCommitTransaction(): void . $platform->getColumnDeclarationListSQL([$column->toArray()]) . ')'; $this->connection->executeStatement($createTempTableSQL); - $table = new Table('nontemporary'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('nontemporary', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -61,15 +67,22 @@ public function testCreateTemporaryTableNotAutoCommitTransaction(): void self::markTestSkipped('Test does not work on Oracle.'); } - $column = new Column('id', Type::getType(Types::INTEGER)); + $column = Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(); $tempTable = $platform->getTemporaryTableName('my_temporary'); $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' (' . $platform->getColumnDeclarationListSQL([$column->toArray()]) . ')'; - $table = new Table('nontemporary'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('nontemporary', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/Ticket/DBAL202Test.php b/tests/Functional/Ticket/DBAL202Test.php index c63b67bb02a..1f7f164db21 100644 --- a/tests/Functional/Ticket/DBAL202Test.php +++ b/tests/Functional/Ticket/DBAL202Test.php @@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests\Functional\Ticket; use Doctrine\DBAL\Platforms\OraclePlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -21,8 +22,12 @@ protected function setUp(): void if ($this->connection->createSchemaManager()->tableExists('DBAL202')) { $this->connection->executeStatement('DELETE FROM DBAL202'); } else { - $table = new Table('DBAL202'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('DBAL202', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/TransactionTest.php b/tests/Functional/TransactionTest.php index 54ab5b76e90..8d02ba03197 100644 --- a/tests/Functional/TransactionTest.php +++ b/tests/Functional/TransactionTest.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Exception\ConnectionLost; use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -100,8 +101,12 @@ public function testNestedTransactionWalkthrough(): void self::markTestIncomplete('Broken when savepoints are not supported.'); } - $table = new Table('storage'); - $table->addColumn('test_int', Types::INTEGER); + $table = new Table('storage', [ + Column::editor() + ->setUnquotedName('test_int') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('test_int') diff --git a/tests/Functional/TypeConversionTest.php b/tests/Functional/TypeConversionTest.php index c2a97a2c930..94ebe464470 100644 --- a/tests/Functional/TypeConversionTest.php +++ b/tests/Functional/TypeConversionTest.php @@ -6,6 +6,7 @@ use BcMath\Number; use DateTime; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -24,25 +25,87 @@ class TypeConversionTest extends FunctionalTestCase protected function setUp(): void { - $table = new Table('type_conversion'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('test_string', Types::STRING, [ - 'length' => 16, - 'notnull' => false, + $table = new Table('type_conversion', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('test_string') + ->setTypeName(Types::STRING) + ->setLength(16) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_boolean') + ->setTypeName(Types::BOOLEAN) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_bigint') + ->setTypeName(Types::BIGINT) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_smallint') + ->setTypeName(Types::SMALLINT) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_datetime') + ->setTypeName(Types::DATETIME_MUTABLE) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_datetimetz') + ->setTypeName(Types::DATETIMETZ_MUTABLE) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_date') + ->setTypeName(Types::DATE_MUTABLE) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_time') + ->setTypeName(Types::TIME_MUTABLE) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_text') + ->setTypeName(Types::TEXT) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_json') + ->setTypeName(Types::JSON) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_float') + ->setTypeName(Types::FLOAT) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_smallfloat') + ->setTypeName(Types::SMALLFLOAT) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('test_decimal') + ->setTypeName(Types::DECIMAL) + ->setNotNull(false) + ->setPrecision(10) + ->setScale(2) + ->create(), + Column::editor() + ->setUnquotedName('test_number') + ->setTypeName(Types::NUMBER) + ->setNotNull(false) + ->setPrecision(10) + ->setScale(2) + ->create(), ]); - $table->addColumn('test_boolean', Types::BOOLEAN, ['notnull' => false]); - $table->addColumn('test_bigint', Types::BIGINT, ['notnull' => false]); - $table->addColumn('test_smallint', Types::BIGINT, ['notnull' => false]); - $table->addColumn('test_datetime', Types::DATETIME_MUTABLE, ['notnull' => false]); - $table->addColumn('test_datetimetz', Types::DATETIMETZ_MUTABLE, ['notnull' => false]); - $table->addColumn('test_date', Types::DATE_MUTABLE, ['notnull' => false]); - $table->addColumn('test_time', Types::TIME_MUTABLE, ['notnull' => false]); - $table->addColumn('test_text', Types::TEXT, ['notnull' => false]); - $table->addColumn('test_json', Types::JSON, ['notnull' => false]); - $table->addColumn('test_float', Types::FLOAT, ['notnull' => false]); - $table->addColumn('test_smallfloat', Types::SMALLFLOAT, ['notnull' => false]); - $table->addColumn('test_decimal', Types::DECIMAL, ['notnull' => false, 'scale' => 2, 'precision' => 10]); - $table->addColumn('test_number', Types::NUMBER, ['notnull' => false, 'scale' => 2, 'precision' => 10]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/Types/AsciiStringTest.php b/tests/Functional/Types/AsciiStringTest.php index 1ae015bb76c..f6f30296b15 100644 --- a/tests/Functional/Types/AsciiStringTest.php +++ b/tests/Functional/Types/AsciiStringTest.php @@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests\Functional\Types; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -14,13 +15,19 @@ class AsciiStringTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('ascii_table'); - $table->addColumn('id', Types::ASCII_STRING, [ - 'length' => 3, - 'fixed' => true, + $table = new Table('ascii_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::ASCII_STRING) + ->setLength(3) + ->setFixed(true) + ->create(), + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::ASCII_STRING) + ->setLength(4) + ->create(), ]); - - $table->addColumn('val', Types::ASCII_STRING, ['length' => 4]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/Types/BigIntTypeTest.php b/tests/Functional/Types/BigIntTypeTest.php index 60ef62d0e77..ab158425ae5 100644 --- a/tests/Functional/Types/BigIntTypeTest.php +++ b/tests/Functional/Types/BigIntTypeTest.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tests\Functional\Types; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -21,9 +22,17 @@ class BigIntTypeTest extends FunctionalTestCase #[DataProvider('provideBigIntLiterals')] public function testSelectBigInt(string $sqlLiteral, int|string|null $expectedValue): void { - $table = new Table('bigint_type_test'); - $table->addColumn('id', Types::SMALLINT, ['notnull' => true]); - $table->addColumn('my_integer', Types::BIGINT, ['notnull' => false]); + $table = new Table('bigint_type_test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::SMALLINT) + ->create(), + Column::editor() + ->setUnquotedName('my_integer') + ->setTypeName(Types::BIGINT) + ->setNotNull(false) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -64,9 +73,18 @@ public function testUnsignedBigIntOnMySQL(): void self::markTestSkipped('This test only works on MySQL/MariaDB.'); } - $table = new Table('bigint_type_test'); - $table->addColumn('id', Types::SMALLINT, ['notnull' => true]); - $table->addColumn('my_integer', Types::BIGINT, ['notnull' => false, 'unsigned' => true]); + $table = new Table('bigint_type_test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::SMALLINT) + ->create(), + Column::editor() + ->setUnquotedName('my_integer') + ->setTypeName(Types::BIGINT) + ->setNotNull(false) + ->setUnsigned(true) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/Types/BinaryTest.php b/tests/Functional/Types/BinaryTest.php index ebf3e07fa22..f6048055a77 100644 --- a/tests/Functional/Types/BinaryTest.php +++ b/tests/Functional/Types/BinaryTest.php @@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests\Functional\Types; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -23,12 +24,19 @@ protected function setUp(): void self::markTestSkipped("PDO_OCI doesn't support binding binary values"); } - $table = new Table('binary_table'); - $table->addColumn('id', Types::BINARY, [ - 'length' => 16, - 'fixed' => true, + $table = new Table('binary_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::BINARY) + ->setLength(16) + ->setFixed(true) + ->create(), + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::BINARY) + ->setLength(64) + ->create(), ]); - $table->addColumn('val', Types::BINARY, ['length' => 64]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/Types/DecimalTest.php b/tests/Functional/Types/DecimalTest.php index 0af1ed6ff5c..186c9c27da6 100644 --- a/tests/Functional/Types/DecimalTest.php +++ b/tests/Functional/Types/DecimalTest.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tests\Functional\Types; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Type; @@ -26,8 +27,14 @@ public static function dataValuesProvider(): array #[DataProvider('dataValuesProvider')] public function testInsertAndRetrieveDecimal(string $expected): void { - $table = new Table('decimal_table'); - $table->addColumn('val', Types::DECIMAL, ['precision' => 4, 'scale' => 2]); + $table = new Table('decimal_table', [ + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::DECIMAL) + ->setPrecision(4) + ->setScale(2) + ->create(), + ]); $this->dropAndCreateTable($table); diff --git a/tests/Functional/Types/GuidTest.php b/tests/Functional/Types/GuidTest.php index 57845f0da10..dca7450e430 100644 --- a/tests/Functional/Types/GuidTest.php +++ b/tests/Functional/Types/GuidTest.php @@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Tests\Functional\Types; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; @@ -12,8 +13,12 @@ class GuidTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('guid_table'); - $table->addColumn('guid', Types::GUID); + $table = new Table('guid_table', [ + Column::editor() + ->setUnquotedName('guid') + ->setTypeName(Types::GUID) + ->create(), + ]); $this->dropAndCreateTable($table); } diff --git a/tests/Functional/Types/JsonTest.php b/tests/Functional/Types/JsonTest.php index 2e64b480494..6e6526f3318 100644 --- a/tests/Functional/Types/JsonTest.php +++ b/tests/Functional/Types/JsonTest.php @@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests\Functional\Types; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -20,10 +21,16 @@ class JsonTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('json_test_table'); - $table->addColumn('id', Types::INTEGER); - - $table->addColumn('val', Types::JSON); + $table = new Table('json_test_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::JSON) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Functional/Types/NumberTest.php b/tests/Functional/Types/NumberTest.php index a57d59af581..e068e1195c9 100644 --- a/tests/Functional/Types/NumberTest.php +++ b/tests/Functional/Types/NumberTest.php @@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests\Functional\Types; use BcMath\Number; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; @@ -22,8 +23,14 @@ public function testInsertAndRetrieveNumber(string $numberAsString): void { $expected = new Number($numberAsString); - $table = new Table('number_table'); - $table->addColumn('val', Types::NUMBER, ['precision' => 4, 'scale' => 2]); + $table = new Table('number_table', [ + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::NUMBER) + ->setPrecision(4) + ->setScale(2) + ->create(), + ]); $this->dropAndCreateTable($table); @@ -44,8 +51,14 @@ public function testInsertAndRetrieveNumber(string $numberAsString): void public function testCompareNumberTable(): void { - $table = new Table('number_table'); - $table->addColumn('val', Types::NUMBER, ['precision' => 4, 'scale' => 2]); + $table = new Table('number_table', [ + Column::editor() + ->setUnquotedName('val') + ->setTypeName(Types::NUMBER) + ->setPrecision(4) + ->setScale(2) + ->create(), + ]); $this->dropAndCreateTable($table); diff --git a/tests/Functional/UniqueConstraintViolationsTest.php b/tests/Functional/UniqueConstraintViolationsTest.php index dd59acf5913..4f64bff7b94 100644 --- a/tests/Functional/UniqueConstraintViolationsTest.php +++ b/tests/Functional/UniqueConstraintViolationsTest.php @@ -13,9 +13,11 @@ use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\UniqueConstraint; use Doctrine\DBAL\Tests\FunctionalTestCase; +use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\Assert; use Throwable; @@ -41,8 +43,12 @@ protected function setUp(): void $schemaManager = $this->connection->createSchemaManager(); - $table = new Table('unique_constraint_violations'); - $table->addColumn('unique_field', 'integer', ['notnull' => true]); + $table = new Table('unique_constraint_violations', [ + Column::editor() + ->setUnquotedName('unique_field') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $schemaManager->createTable($table); if ($platform instanceof OraclePlatform || $platform instanceof PostgreSQLPlatform) { diff --git a/tests/Functional/WriteTest.php b/tests/Functional/WriteTest.php index cc01b2b3ba2..dd81ff3ceb9 100644 --- a/tests/Functional/WriteTest.php +++ b/tests/Functional/WriteTest.php @@ -11,6 +11,7 @@ use Doctrine\DBAL\Driver\Exception\NoIdentityValue; use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; @@ -22,12 +23,22 @@ class WriteTest extends FunctionalTestCase { protected function setUp(): void { - $table = new Table('write_table'); - $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('test_int', Types::INTEGER); - $table->addColumn('test_string', Types::STRING, [ - 'length' => 32, - 'notnull' => false, + $table = new Table('write_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('test_int') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('test_string') + ->setTypeName(Types::STRING) + ->setLength(32) + ->setNotNull(false) + ->create(), ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() @@ -280,8 +291,13 @@ public function testEmptyIdentityInsert(): void ); } - $table = new Table('test_empty_identity'); - $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); + $table = new Table('test_empty_identity', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') diff --git a/tests/Platforms/AbstractMySQLPlatformTestCase.php b/tests/Platforms/AbstractMySQLPlatformTestCase.php index a6c87518d02..35682dbd92c 100644 --- a/tests/Platforms/AbstractMySQLPlatformTestCase.php +++ b/tests/Platforms/AbstractMySQLPlatformTestCase.php @@ -10,6 +10,7 @@ use Doctrine\DBAL\Platforms\MySQL\CharsetMetadataProvider; use Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider; use Doctrine\DBAL\Platforms\MySQL\DefaultTableOptions; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\ComparatorConfig; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; @@ -30,8 +31,12 @@ public function testModifyLimitQueryWithoutLimit(): void public function testGenerateMixedCaseTableCreate(): void { - $table = new Table('Foo'); - $table->addColumn('Bar', Types::INTEGER); + $table = new Table('Foo', [ + Column::editor() + ->setUnquotedName('Bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $sql = $this->platform->getCreateTableSQL($table); self::assertEquals( @@ -138,9 +143,20 @@ protected function getGenerateForeignKeySql(): string public function testUniquePrimaryKey(): void { - $keyTable = new Table('foo'); - $keyTable->addColumn('bar', Types::INTEGER); - $keyTable->addColumn('baz', Types::STRING, ['length' => 32]); + $oldTable = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]); + + $keyTable = $oldTable->edit() + ->create(); $keyTable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('bar') @@ -148,10 +164,6 @@ public function testUniquePrimaryKey(): void ); $keyTable->addUniqueIndex(['baz']); - $oldTable = new Table('foo'); - $oldTable->addColumn('bar', Types::INTEGER); - $oldTable->addColumn('baz', Types::STRING, ['length' => 32]); - $diff = $this->createComparator() ->compareTables($oldTable, $keyTable); @@ -223,9 +235,13 @@ protected function getQuotedColumnInForeignKeySQL(): array public function testCreateTableWithFulltextIndex(): void { - $table = new Table('fulltext_table'); + $table = new Table('fulltext_table', [ + Column::editor() + ->setUnquotedName('text') + ->setTypeName(Types::TEXT) + ->create(), + ]); $table->addOption('engine', 'MyISAM'); - $table->addColumn('text', Types::TEXT); $table->addIndex(['text'], 'fulltext_text', ['fulltext']); $sql = $this->platform->getCreateTableSQL($table); @@ -241,9 +257,14 @@ public function testCreateTableWithFulltextIndex(): void public function testCreateTableWithSpatialIndex(): void { - $table = new Table('spatial_table'); + $table = new Table('spatial_table', [ + Column::editor() + ->setUnquotedName('point') + // This should be a point type + ->setTypeName(Types::TEXT) + ->create(), + ]); $table->addOption('engine', 'MyISAM'); - $table->addColumn('point', Types::TEXT); // This should be a point type $table->addIndex(['point'], 'spatial_text', ['spatial']); $sql = $this->platform->getCreateTableSQL($table); @@ -355,11 +376,30 @@ protected function getQuotedCommentOnColumnSQLWithQuoteCharacter(): string public function testIgnoresDifferenceInDefaultValuesForUnsupportedColumnTypes(): void { - $table = new Table('text_blob_default_value'); - $table->addColumn('def_text', Types::TEXT, ['default' => 'def']); - $table->addColumn('def_text_null', Types::TEXT, ['notnull' => false, 'default' => 'def']); - $table->addColumn('def_blob', Types::BLOB, ['default' => 'def']); - $table->addColumn('def_blob_null', Types::BLOB, ['notnull' => false, 'default' => 'def']); + $table = new Table('text_blob_default_value', [ + Column::editor() + ->setUnquotedName('def_text') + ->setTypeName(Types::TEXT) + ->setDefaultValue('def') + ->create(), + Column::editor() + ->setUnquotedName('def_text_null') + ->setTypeName(Types::TEXT) + ->setNotNull(false) + ->setDefaultValue('def') + ->create(), + Column::editor() + ->setUnquotedName('def_blob') + ->setTypeName(Types::BLOB) + ->setDefaultValue('def') + ->create(), + Column::editor() + ->setUnquotedName('def_blob_null') + ->setTypeName(Types::BLOB) + ->setNotNull(false) + ->setDefaultValue('def') + ->create(), + ]); self::assertSame( [ diff --git a/tests/Platforms/AbstractPlatformTestCase.php b/tests/Platforms/AbstractPlatformTestCase.php index 11c6598ca0f..3ecb1e6cf28 100644 --- a/tests/Platforms/AbstractPlatformTestCase.php +++ b/tests/Platforms/AbstractPlatformTestCase.php @@ -130,9 +130,19 @@ public function testCreateWithNoColumns(): void public function testGeneratesTableCreationSql(): void { - $table = new Table('test'); - $table->addColumn('id', Types::INTEGER, ['notnull' => true, 'autoincrement' => true]); - $table->addColumn('test', Types::STRING, ['notnull' => false, 'length' => 255]); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('test') + ->setTypeName(Types::STRING) + ->setLength(255) + ->setNotNull(false) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -147,9 +157,20 @@ abstract public function getGenerateTableSql(): string; public function testGenerateTableWithMultiColumnUniqueIndex(): void { - $table = new Table('test'); - $table->addColumn('foo', Types::STRING, ['notnull' => false, 'length' => 255]); - $table->addColumn('bar', Types::STRING, ['notnull' => false, 'length' => 255]); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::STRING) + ->setLength(255) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setLength(255) + ->setNotNull(false) + ->create(), + ]); $table->addUniqueIndex(['foo', 'bar']); $sql = $this->platform->getCreateTableSQL($table); @@ -236,8 +257,13 @@ public function getGenerateConstraintPrimaryIndexSql(): string public function testQuotedColumnInPrimaryKeyPropagation(): void { - $table = new Table('`quoted`'); - $table->addColumn('create', Types::STRING, ['length' => 255]); + $table = new Table('`quoted`', [ + Column::editor() + ->setUnquotedName('create') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('create') @@ -262,8 +288,13 @@ abstract protected function getQuotedColumnInForeignKeySQL(): array; public function testQuotedColumnInIndexPropagation(): void { - $table = new Table('`quoted`'); - $table->addColumn('create', Types::STRING, ['length' => 255]); + $table = new Table('`quoted`', [ + Column::editor() + ->setUnquotedName('create') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), + ]); $table->addIndex(['create']); $sql = $this->platform->getCreateTableSQL($table); @@ -272,8 +303,13 @@ public function testQuotedColumnInIndexPropagation(): void public function testQuotedNameInIndexSQL(): void { - $table = new Table('test'); - $table->addColumn('column1', Types::STRING, ['length' => 255]); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('column1') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), + ]); $table->addIndex(['column1'], '`key`'); $sql = $this->platform->getCreateTableSQL($table); @@ -282,65 +318,42 @@ public function testQuotedNameInIndexSQL(): void public function testQuotedColumnInForeignKeyPropagation(): void { - $table = new Table('`quoted`'); - $table->addColumn('create', Types::STRING, ['length' => 255]); - $table->addColumn('foo', Types::STRING, ['length' => 255]); - $table->addColumn('`bar`', Types::STRING, ['length' => 255]); - - // Foreign table with reserved keyword as name (needs quotation). - $foreignTable = new Table('foreign'); - - // Foreign column with reserved keyword as name (needs quotation). - $foreignTable->addColumn('create', Types::STRING); - - // Foreign column with non-reserved keyword as name (does not need quotation). - $foreignTable->addColumn('bar', Types::STRING); - - // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite). - $foreignTable->addColumn('`foo-bar`', Types::STRING); + $table = new Table('`quoted`', [ + Column::editor() + ->setUnquotedName('create') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), + Column::editor() + ->setQuotedName('bar') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), + ]); $table->addForeignKeyConstraint( - $foreignTable->getObjectName()->toSQL($this->platform), + 'foreign', ['create', 'foo', '`bar`'], ['create', 'bar', '`foo-bar`'], [], 'FK_WITH_RESERVED_KEYWORD', ); - // Foreign table with non-reserved keyword as name - $foreignTable = new Table('foo'); - - // Foreign column with reserved keyword as name - $foreignTable->addColumn('create', Types::STRING); - - // Foreign column with non-reserved keyword as name - $foreignTable->addColumn('bar', Types::STRING); - - // Foreign table with special character in name - $foreignTable->addColumn('`foo-bar`', Types::STRING); - $table->addForeignKeyConstraint( - $foreignTable->getObjectName()->toSQL($this->platform), + 'foo', ['create', 'foo', '`bar`'], ['create', 'bar', '`foo-bar`'], [], 'FK_WITH_NON_RESERVED_KEYWORD', ); - // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite). - $foreignTable = new Table('`foo-bar`'); - - // Foreign column with reserved keyword as name (needs quotation). - $foreignTable->addColumn('create', Types::STRING); - - // Foreign column with non-reserved keyword as name (does not need quotation). - $foreignTable->addColumn('bar', Types::STRING); - - // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite). - $foreignTable->addColumn('`foo-bar`', Types::STRING); - $table->addForeignKeyConstraint( - $foreignTable->getObjectName()->toSQL($this->platform), + '`foo-bar`', ['create', 'foo', '`bar`'], ['create', 'bar', '`foo-bar`'], [], @@ -372,17 +385,21 @@ public function testGetCreateSchemaSQL(): void public function testAlterTableChangeQuotedColumn(): void { - $table = new Table('mytable'); - $table->addColumn('select', Types::INTEGER); + $table = new Table('mytable', [ + Column::editor() + ->setUnquotedName('select') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableDiff = new TableDiff($table, changedColumns: [ 'select' => new ColumnDiff( $table->getColumn('select'), - new Column( - 'select', - Type::getType(Types::STRING), - ['length' => 255], - ), + Column::editor() + ->setUnquotedName('select') + ->setTypeName(Types::STRING) + ->setLength(255) + ->create(), ), ]); @@ -554,8 +571,12 @@ public function testReturnsJsonbTypeDeclarationSQL(): void public function testAlterTableRenameIndex(): void { - $table = new Table('mytable'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('mytable', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -586,8 +607,12 @@ protected function getAlterTableRenameIndexSQL(): array public function testQuotesAlterTableRenameIndex(): void { - $table = new Table('table'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -624,8 +649,12 @@ protected function getQuotedAlterTableRenameIndexSQL(): array public function testAlterTableRenameIndexInSchema(): void { - $table = new Table('myschema.mytable'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('myschema.mytable', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -656,8 +685,12 @@ protected function getAlterTableRenameIndexInSchemaSQL(): array public function testQuotesAlterTableRenameIndexInSchema(): void { - $table = new Table('`schema`.table'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('`schema`.table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -761,17 +794,23 @@ public function testReturnsGuidTypeDeclarationSQL(): void public function testAlterStringToFixedString(): void { - $table = new Table('mytable'); - $table->addColumn('name', Types::STRING, ['length' => 2]); + $table = new Table('mytable', [ + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::STRING) + ->setLength(2) + ->create(), + ]); $tableDiff = new TableDiff($table, changedColumns: [ 'name' => new ColumnDiff( $table->getColumn('name'), - new Column( - 'name', - Type::getType(Types::STRING), - ['fixed' => true, 'length' => 2], - ), + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::STRING) + ->setFixed(true) + ->setLength(2) + ->create(), ), ]); @@ -787,18 +826,32 @@ abstract protected function getAlterStringToFixedStringSQL(): array; public function testGeneratesAlterTableRenameIndexUsedByForeignKeySQL(): void { - $foreignTable = new Table('foreign_table'); - $foreignTable->addColumn('id', Types::INTEGER); + $foreignTable = new Table('foreign_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $foreignTable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') ->create(), ); - $primaryTable = new Table('mytable'); - $primaryTable->addColumn('foo', Types::INTEGER); - $primaryTable->addColumn('bar', Types::INTEGER); - $primaryTable->addColumn('baz', Types::INTEGER); + $primaryTable = new Table('mytable', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $primaryTable->addIndex(['foo'], 'idx_foo'); $primaryTable->addIndex(['bar'], 'idx_bar'); $primaryTable->addForeignKeyConstraint($foreignTable->getName(), ['foo'], ['id'], [], 'fk_foo'); diff --git a/tests/Platforms/DB2PlatformTest.php b/tests/Platforms/DB2PlatformTest.php index 663cbae5fc6..41cc1d67215 100644 --- a/tests/Platforms/DB2PlatformTest.php +++ b/tests/Platforms/DB2PlatformTest.php @@ -12,7 +12,6 @@ use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\Attributes\DataProvider; @@ -116,9 +115,17 @@ protected function getBitOrComparisonExpressionSql(string $value1, string $value public function testGeneratesCreateTableSQLWithCommonIndexes(): void { - $table = new Table('test'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('name', Types::STRING, ['length' => 50]); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::STRING) + ->setLength(50) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -139,10 +146,20 @@ public function testGeneratesCreateTableSQLWithCommonIndexes(): void public function testGeneratesCreateTableSQLWithForeignKeyConstraints(): void { - $table = new Table('test'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('fk_1', Types::INTEGER); - $table->addColumn('fk_2', Types::INTEGER); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('fk_1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('fk_2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -173,10 +190,22 @@ public function testGeneratesCreateTableSQLWithForeignKeyConstraints(): void public function testGeneratesCreateTableSQLWithCheckConstraints(): void { - $table = new Table('test'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('check_max', Types::INTEGER, ['platformOptions' => ['max' => 10]]); - $table->addColumn('check_min', Types::INTEGER, ['platformOptions' => ['min' => 10]]); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('check_max') + ->setTypeName(Types::INTEGER) + ->setMaximumValue(10) + ->create(), + Column::editor() + ->setUnquotedName('check_min') + ->setTypeName(Types::INTEGER) + ->setMinimumValue(10) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -461,64 +490,152 @@ public function testGeneratesAlterColumnSQL( self::assertSame($expectedSQL, $this->platform->getAlterTableSQL($tableDiff)); } - /** @return mixed[][] */ + /** @return iterable */ public static function getGeneratesAlterColumnSQL(): iterable { return [ [ - new Column('bar', Type::getType(Types::DECIMAL), ['columnDefinition' => 'MONEY NULL']), - new Column('bar', Type::getType(Types::DECIMAL), ['columnDefinition' => 'MONEY NOT NULL']), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::DECIMAL) + ->setColumnDefinition('MONEY NULL') + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::DECIMAL) + ->setColumnDefinition('MONEY NOT NULL') + ->create(), 'MONEY NOT NULL', ], [ - new Column('bar', Type::getType(Types::STRING)), - new Column('bar', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), 'SET DATA TYPE INTEGER', ], [ - new Column('bar', Type::getType(Types::STRING), ['length' => 50]), - new Column('bar', Type::getType(Types::STRING), ['length' => 100]), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setLength(50) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setLength(100) + ->create(), 'SET DATA TYPE VARCHAR(100)', ], [ - new Column('bar', Type::getType(Types::DECIMAL), ['precision' => 8, 'scale' => 2]), - new Column('bar', Type::getType(Types::DECIMAL), ['precision' => 10, 'scale' => 2]), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::DECIMAL) + ->setPrecision(8) + ->setScale(2) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::DECIMAL) + ->setPrecision(10) + ->setScale(2) + ->create(), 'SET DATA TYPE NUMERIC(10, 2)', ], [ - new Column('bar', Type::getType(Types::DECIMAL), ['precision' => 5, 'scale' => 3]), - new Column('bar', Type::getType(Types::DECIMAL), ['precision' => 5, 'scale' => 4]), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::DECIMAL) + ->setPrecision(5) + ->setScale(3) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::DECIMAL) + ->setPrecision(5) + ->setScale(4) + ->create(), 'SET DATA TYPE NUMERIC(5, 4)', ], [ - new Column('bar', Type::getType(Types::STRING), ['length' => 10, 'fixed' => true]), - new Column('bar', Type::getType(Types::STRING), ['length' => 20, 'fixed' => true]), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setLength(10) + ->setFixed(true) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setLength(20) + ->setFixed(true) + ->create(), 'SET DATA TYPE CHAR(20)', ], [ - new Column('bar', Type::getType(Types::STRING), ['notnull' => false]), - new Column('bar', Type::getType(Types::STRING), ['notnull' => true]), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->create(), 'SET NOT NULL', ], [ - new Column('bar', Type::getType(Types::STRING), ['notnull' => true]), - new Column('bar', Type::getType(Types::STRING), ['notnull' => false]), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setNotNull(false) + ->create(), 'DROP NOT NULL', ], [ - new Column('bar', Type::getType(Types::STRING)), - new Column('bar', Type::getType(Types::STRING), ['default' => 'foo']), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setDefaultValue('foo') + ->create(), "SET DEFAULT 'foo'", ], [ - new Column('bar', Type::getType(Types::INTEGER)), - new Column('bar', Type::getType(Types::INTEGER), ['autoincrement' => true, 'default' => 666]), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->setDefaultValue(666) + ->create(), null, false, ], [ - new Column('bar', Type::getType(Types::STRING), ['default' => 'foo']), - new Column('bar', Type::getType(Types::STRING)), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->setDefaultValue('foo') + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::STRING) + ->create(), 'DROP DEFAULT', ], ]; diff --git a/tests/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProviderTest.php b/tests/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProviderTest.php index 02071abf7aa..6ef40ed039a 100644 --- a/tests/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProviderTest.php +++ b/tests/Platforms/MySQL/CollationMetadataProvider/CachingCollationMetadataProviderTest.php @@ -11,6 +11,10 @@ class CachingCollationMetadataProviderTest extends TestCase { + /** + * @param non-empty-string $collation + * @param ?non-empty-string $charset + */ #[DataProvider('charsetAndCollationProvider')] public function testCharsetCaching(string $collation, ?string $charset): void { @@ -25,7 +29,7 @@ public function testCharsetCaching(string $collation, ?string $charset): void self::assertSame($charset, $cachingProvider->getCollationCharset($collation)); } - /** @return iterable */ + /** @return iterable */ public static function charsetAndCollationProvider(): iterable { yield 'found' => ['utf8mb4_unicode_ci', 'utf8mb4']; diff --git a/tests/Platforms/MySQLPlatformTest.php b/tests/Platforms/MySQLPlatformTest.php index 77ab38bdb70..0476452ac71 100644 --- a/tests/Platforms/MySQLPlatformTest.php +++ b/tests/Platforms/MySQLPlatformTest.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MySQLPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\TransactionIsolationLevel; use Doctrine\DBAL\Types\Types; @@ -68,8 +69,12 @@ public function testHasCorrectDefaultTransactionIsolationLevel(): void public function testCollationOptionIsTakenIntoAccount(): void { - $table = new Table('quotations'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('quotations', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addOption('collation', 'my_collation'); self::assertStringContainsString( 'my_collation', diff --git a/tests/Platforms/OraclePlatformTest.php b/tests/Platforms/OraclePlatformTest.php index 32178b356dc..c0de18b52be 100644 --- a/tests/Platforms/OraclePlatformTest.php +++ b/tests/Platforms/OraclePlatformTest.php @@ -13,7 +13,6 @@ use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\TransactionIsolationLevel; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\Attributes\DataProvider; @@ -157,10 +156,13 @@ public function testGenerateTableWithAutoincrement(): void { $columnName = strtoupper('id' . uniqid()); $tableName = strtoupper('table' . uniqid()); - $table = new Table($tableName); - - $column = $table->addColumn($columnName, Types::INTEGER); - $column->setAutoincrement(true); + $table = new Table($tableName, [ + Column::editor() + ->setUnquotedName($columnName) + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); self::assertSame([ sprintf('CREATE TABLE "%s" ("%s" NUMBER(10) NOT NULL)', $tableName, $columnName), @@ -315,19 +317,33 @@ public function getExpectedVariableLengthBinaryTypeDeclarationSQLWithLength(): s public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType(): void { - $table1 = new Table('mytable'); - $table1->addColumn('column_varbinary', Types::BINARY, ['length' => 32]); - $table1->addColumn('column_binary', Types::BINARY, [ - 'fixed' => true, - 'length' => 32, + $table1 = new Table('mytable', [ + Column::editor() + ->setUnquotedName('column_varbinary') + ->setTypeName(Types::BINARY) + ->setLength(32) + ->create(), + Column::editor() + ->setUnquotedName('column_binary') + ->setTypeName(Types::BINARY) + ->setFixed(true) + ->setLength(32) + ->create(), ]); - $table2 = new Table('mytable'); - $table2->addColumn('column_varbinary', Types::BINARY, [ - 'fixed' => true, - 'length' => 32, + $table2 = new Table('mytable', [ + Column::editor() + ->setUnquotedName('column_varbinary') + ->setTypeName(Types::BINARY) + ->setFixed(true) + ->setLength(32) + ->create(), + Column::editor() + ->setUnquotedName('column_binary') + ->setTypeName(Types::BINARY) + ->setLength(32) + ->create(), ]); - $table2->addColumn('column_binary', Types::BINARY, ['length' => 32]); self::assertTrue( $this->createComparator() @@ -445,24 +461,40 @@ protected function getCommentOnColumnSQL(): array public function testAltersTableColumnCommentWithExplicitlyQuotedIdentifiers(): void { - $table1 = new Table('"foo"', [new Column('"bar"', Type::getType(Types::INTEGER))]); - $table2 = new Table('"foo"', [new Column('"bar"', Type::getType(Types::INTEGER), ['comment' => 'baz'])]); + $table1 = new Table('"Foo"', [ + Column::editor() + ->setQuotedName('Bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); + + $table2 = new Table('"Foo"', [ + Column::editor() + ->setQuotedName('Bar') + ->setTypeName(Types::INTEGER) + ->setComment('Baz') + ->create(), + ]); $tableDiff = $this->createComparator() ->compareTables($table1, $table2); self::assertSame( - ['COMMENT ON COLUMN "foo"."bar" IS \'baz\''], + ['COMMENT ON COLUMN "Foo"."Bar" IS \'Baz\''], $this->platform->getAlterTableSQL($tableDiff), ); } public function testQuotedTableNames(): void { - $table = new Table('"test"'); - $table->addColumn('"id"', Types::INTEGER, ['autoincrement' => true]); + $table = new Table('"test"', [ + Column::editor() + ->setQuotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); - // assert tabel self::assertTrue($table->isQuoted()); self::assertEquals('test', $table->getName()); self::assertEquals('"test"', $table->getObjectName()->toSQL($this->platform)); diff --git a/tests/Platforms/PostgreSQLPlatformTest.php b/tests/Platforms/PostgreSQLPlatformTest.php index cee63c6cd9a..7ff9288b000 100644 --- a/tests/Platforms/PostgreSQLPlatformTest.php +++ b/tests/Platforms/PostgreSQLPlatformTest.php @@ -101,9 +101,13 @@ public function testGeneratesDDLSnippets(): void public function testGenerateTableWithAutoincrement(): void { - $table = new Table('autoinc_table'); - $column = $table->addColumn('id', Types::INTEGER); - $column->setAutoincrement(true); + $table = new Table('autoinc_table', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); self::assertEquals( ['CREATE TABLE "autoinc_table" ("id" INT GENERATED BY DEFAULT AS IDENTITY NOT NULL)'], @@ -113,9 +117,13 @@ public function testGenerateTableWithAutoincrement(): void public function testGenerateUnloggedTable(): void { - $table = new Table('mytable'); + $table = new Table('mytable', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::STRING) + ->create(), + ]); $table->addOption('unlogged', true); - $table->addColumn('foo', 'string'); self::assertEquals( ['CREATE UNLOGGED TABLE "mytable" ("foo" VARCHAR NOT NULL)'], @@ -133,12 +141,16 @@ public static function serialTypes(): iterable } #[DataProvider('serialTypes')] - public function testGenerateTableWithAutoincrementDoesNotSetDefault(string $type, string $definition): void - { - $table = new Table('autoinc_table_notnull'); - $column = $table->addColumn('id', $type); - $column->setAutoincrement(true); - $column->setNotnull(false); + public function testGenerateTableWithAutoincrementDoesNotSetDefault(string $typeName, string $definition): void + { + $table = new Table('autoinc_table_notnull', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName($typeName) + ->setAutoincrement(true) + ->setNotNull(false) + ->create(), + ]); $sql = $this->platform->getCreateTableSQL($table); @@ -146,12 +158,15 @@ public function testGenerateTableWithAutoincrementDoesNotSetDefault(string $type } #[DataProvider('serialTypes')] - public function testCreateTableWithAutoincrementAndNotNullAddsConstraint(string $type, string $definition): void + public function testCreateTableWithAutoincrementAndNotNullAddsConstraint(string $typeName, string $definition): void { - $table = new Table('autoinc_table_notnull_enabled'); - $column = $table->addColumn('id', $type); - $column->setAutoincrement(true); - $column->setNotnull(true); + $table = new Table('autoinc_table_notnull_enabled', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName($typeName) + ->setAutoincrement(true) + ->create(), + ]); $sql = $this->platform->getCreateTableSQL($table); @@ -340,8 +355,12 @@ public function testGetCreateSchemaSQL(): void public function testDroppingConstraintsBeforeColumns(): void { - $newTable = new Table('mytable'); - $newTable->addColumn('id', Types::INTEGER); + $newTable = new Table('mytable', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $newTable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -368,8 +387,12 @@ public function testDroppingConstraintsBeforeColumns(): void public function testDroppingPrimaryKey(): void { - $oldTable = new Table('test'); - $oldTable->addColumn('id', 'integer'); + $oldTable = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $oldTable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedName('test_pkey') @@ -428,15 +451,37 @@ public function getExpectedVariableLengthBinaryTypeDeclarationSQLWithLength(): s public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType(): void { - $table1 = new Table('mytable'); - $table1->addColumn('column_varbinary', Types::BINARY); - $table1->addColumn('column_binary', Types::BINARY, ['fixed' => true]); - $table1->addColumn('column_blob', Types::BLOB); + $table1 = new Table('mytable', [ + Column::editor() + ->setUnquotedName('column_varbinary') + ->setTypeName(Types::BINARY) + ->create(), + Column::editor() + ->setUnquotedName('column_binary') + ->setTypeName(Types::BINARY) + ->setFixed(true) + ->create(), + Column::editor() + ->setUnquotedName('column_blob') + ->setTypeName(Types::BLOB) + ->create(), + ]); - $table2 = new Table('mytable'); - $table2->addColumn('column_varbinary', Types::BINARY, ['fixed' => true]); - $table2->addColumn('column_binary', Types::BINARY); - $table2->addColumn('column_blob', Types::BINARY); + $table2 = new Table('mytable', [ + Column::editor() + ->setUnquotedName('column_varbinary') + ->setTypeName(Types::BINARY) + ->setFixed(true) + ->create(), + Column::editor() + ->setUnquotedName('column_binary') + ->setTypeName(Types::BINARY) + ->create(), + Column::editor() + ->setUnquotedName('column_blob') + ->setTypeName(Types::BINARY) + ->create(), + ]); $comparator = $this->createComparator(); @@ -448,10 +493,23 @@ public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType(): vo ->isEmpty(), ); - $table2 = new Table('mytable'); - $table2->addColumn('column_varbinary', Types::BINARY, ['length' => 42]); - $table2->addColumn('column_binary', Types::BLOB); - $table2->addColumn('column_blob', Types::BINARY, ['length' => 11, 'fixed' => true]); + $table2 = new Table('mytable', [ + Column::editor() + ->setUnquotedName('column_varbinary') + ->setTypeName(Types::BINARY) + ->setLength(42) + ->create(), + Column::editor() + ->setUnquotedName('column_binary') + ->setTypeName(Types::BLOB) + ->create(), + Column::editor() + ->setUnquotedName('column_blob') + ->setTypeName(Types::BINARY) + ->setLength(11) + ->setFixed(true) + ->create(), + ]); // VARBINARY -> VARBINARY with changed length // BINARY -> BLOB @@ -461,10 +519,22 @@ public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType(): vo ->isEmpty(), ); - $table2 = new Table('mytable'); - $table2->addColumn('column_varbinary', Types::BLOB); - $table2->addColumn('column_binary', Types::BINARY, ['length' => 42, 'fixed' => true]); - $table2->addColumn('column_blob', Types::BLOB); + $table2 = new Table('mytable', [ + Column::editor() + ->setUnquotedName('column_varbinary') + ->setTypeName(Types::BLOB) + ->create(), + Column::editor() + ->setUnquotedName('column_binary') + ->setTypeName(Types::BINARY) + ->setLength(42) + ->setFixed(true) + ->create(), + Column::editor() + ->setUnquotedName('column_blob') + ->setTypeName(Types::BLOB) + ->create(), + ]); // VARBINARY -> BLOB // BINARY -> BINARY with changed length @@ -567,14 +637,23 @@ protected function getCommentOnColumnSQL(): array public function testAltersTableColumnCommentWithExplicitlyQuotedIdentifiers(): void { - $table1 = new Table('"foo"', [new Column('"bar"', Type::getType(Types::INTEGER))]); - $table2 = new Table('"foo"', [new Column('"bar"', Type::getType(Types::INTEGER), ['comment' => 'baz'])]); + $table1 = new Table('"Foo"', [Column::editor() + ->setQuotedName('Bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); + $table2 = new Table('"Foo"', [Column::editor() + ->setQuotedName('Bar') + ->setTypeName(Types::INTEGER) + ->setComment('Baz') + ->create(), + ]); $tableDiff = $this->createComparator() ->compareTables($table1, $table2); self::assertSame( - ['COMMENT ON COLUMN "foo"."bar" IS \'baz\''], + ['COMMENT ON COLUMN "Foo"."Bar" IS \'Baz\''], $this->platform->getAlterTableSQL($tableDiff), ); } @@ -613,8 +692,12 @@ public function testInitializesTsvectorTypeMapping(): void public function testGetCreateTableSQLWithUniqueConstraints(): void { - $table = new Table('foo'); - $table->addColumn('id', Types::STRING); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::STRING) + ->create(), + ]); $table->addUniqueConstraint(['id'], 'test_unique_constraint'); self::assertSame( [ @@ -628,8 +711,12 @@ public function testGetCreateTableSQLWithUniqueConstraints(): void public function testGetCreateTableSQLWithColumnCollation(): void { - $table = new Table('foo'); - $table->addColumn('id', Types::STRING); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::STRING) + ->create(), + ]); $table->addOption('comment', 'foo'); self::assertSame( [ @@ -725,10 +812,10 @@ public function testAlterTableChangeJsonbToJson(): void $tableDiff = new TableDiff($table, changedColumns: [ 'payload' => new ColumnDiff( $table->getColumn('payload'), - new Column( - 'payload', - Type::getType(Types::JSON), - ), + Column::editor() + ->setUnquotedName('payload') + ->setTypeName(Types::JSON) + ->create(), ), ]); diff --git a/tests/Platforms/SQLServerPlatformTest.php b/tests/Platforms/SQLServerPlatformTest.php index 1f4510597d8..89ad2036ef2 100644 --- a/tests/Platforms/SQLServerPlatformTest.php +++ b/tests/Platforms/SQLServerPlatformTest.php @@ -20,7 +20,6 @@ use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\TransactionIsolationLevel; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\Attributes\DataProvider; @@ -571,8 +570,12 @@ public function testCreateClusteredIndex(): void public function testCreateNonClusteredPrimaryKeyInTable(): void { - $table = new Table('tbl'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('tbl', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -643,8 +646,13 @@ public function testGetCreateSchemaSQL(): void public function testCreateTableWithSchemaColumnComments(): void { - $table = new Table('testschema.test'); - $table->addColumn('id', Types::INTEGER, ['comment' => 'This is a comment']); + $table = new Table('testschema.test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setComment('This is a comment') + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -664,12 +672,11 @@ public function testAlterTableWithSchemaColumnComments(): void { $table = new Table('testschema.mytable'); - $tableDiff = new TableDiff($table, addedColumns: [ - new Column( - 'quota', - Type::getType(Types::INTEGER), - ['comment' => 'A comment'], - ), + $tableDiff = new TableDiff($table, addedColumns: [Column::editor() + ->setUnquotedName('quota') + ->setTypeName(Types::INTEGER) + ->setComment('A comment') + ->create(), ]); $expectedSql = [ @@ -687,8 +694,15 @@ public function testAlterTableWithSchemaDropColumnComments(): void $tableDiff = new TableDiff($table, changedColumns: [ 'quota' => new ColumnDiff( - new Column('quota', Type::getType(Types::INTEGER), ['comment' => 'A comment']), - new Column('quota', Type::getType(Types::INTEGER), []), + Column::editor() + ->setUnquotedName('quota') + ->setTypeName(Types::INTEGER) + ->setComment('A comment') + ->create(), + Column::editor() + ->setUnquotedName('quota') + ->setTypeName(Types::INTEGER) + ->create(), ), ]); @@ -706,8 +720,16 @@ public function testAlterTableWithSchemaUpdateColumnComments(): void $tableDiff = new TableDiff($table, changedColumns: [ 'quota' => new ColumnDiff( - new Column('quota', Type::getType(Types::INTEGER), ['comment' => 'A comment']), - new Column('quota', Type::getType(Types::INTEGER), ['comment' => 'B comment']), + Column::editor() + ->setUnquotedName('quota') + ->setTypeName(Types::INTEGER) + ->setComment('A comment') + ->create(), + Column::editor() + ->setUnquotedName('quota') + ->setTypeName(Types::INTEGER) + ->setComment('B comment') + ->create(), ), ]); @@ -1065,8 +1087,17 @@ public function testAlterTableWithSchemaSameColumnComments(): void $tableDiff = new TableDiff($table, changedColumns: [ 'quota' => new ColumnDiff( - new Column('quota', Type::getType(Types::INTEGER), ['comment' => 'A comment', 'notnull' => false]), - new Column('quota', Type::getType(Types::INTEGER), ['comment' => 'A comment', 'notnull' => true]), + Column::editor() + ->setUnquotedName('quota') + ->setTypeName(Types::INTEGER) + ->setComment('A comment') + ->setNotNull(false) + ->create(), + Column::editor() + ->setUnquotedName('quota') + ->setTypeName(Types::INTEGER) + ->setComment('A comment') + ->create(), ), ]); diff --git a/tests/Platforms/SQLitePlatformTest.php b/tests/Platforms/SQLitePlatformTest.php index 3875e07d2d0..1399654807a 100644 --- a/tests/Platforms/SQLitePlatformTest.php +++ b/tests/Platforms/SQLitePlatformTest.php @@ -18,7 +18,6 @@ use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\TransactionIsolationLevel; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; /** @extends AbstractPlatformTestCase */ @@ -227,8 +226,13 @@ public function testModifyLimitQueryWithOffsetAndEmptyLimit(): void public function testGenerateTableSqlShouldNotAutoQuotePrimaryKey(): void { - $table = new Table('test'); - $table->addColumn('"like"', Types::INTEGER, ['notnull' => true, 'autoincrement' => true]); + $table = new Table('test', [ + Column::editor() + ->setQuotedName('like') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('like') @@ -247,11 +251,16 @@ public function testAlterTableAddColumns(): void $table = new Table('user'); $diff = new TableDiff($table, addedColumns: [ - new Column('foo', Type::getType(Types::STRING)), - new Column('count', Type::getType(Types::INTEGER), [ - 'notnull' => false, - 'default' => 1, - ]), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('count') + ->setTypeName(Types::INTEGER) + ->setNotNull(false) + ->setDefaultValue(1) + ->create(), ]); $expected = [ @@ -264,13 +273,23 @@ public function testAlterTableAddColumns(): void public function testRenameNonExistingColumn(): void { - $table = new Table('test'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableDiff = new TableDiff($table, changedColumns: [ 'value' => new ColumnDiff( - new Column('data', Type::getType(Types::STRING)), - new Column('value', Type::getType(Types::STRING)), + Column::editor() + ->setUnquotedName('data') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('value') + ->setTypeName(Types::STRING) + ->create(), ), ]); @@ -280,11 +299,24 @@ public function testRenameNonExistingColumn(): void public function testCreateTableWithDeferredForeignKeys(): void { - $table = new Table('user'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('article', Types::INTEGER); - $table->addColumn('post', Types::INTEGER); - $table->addColumn('parent', Types::INTEGER); + $table = new Table('user', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('article') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('post') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('parent') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -315,11 +347,24 @@ public function testCreateTableWithDeferredForeignKeys(): void public function testAlterTable(): void { - $table = new Table('user'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('article', Types::INTEGER); - $table->addColumn('post', Types::INTEGER); - $table->addColumn('parent', Types::INTEGER); + $table = new Table('user', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('article') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('post') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('parent') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -335,15 +380,24 @@ public function testAlterTable(): void changedColumns: [ 'id' => new ColumnDiff( $table->getColumn('id'), - new Column('key', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('key') + ->setTypeName(Types::INTEGER) + ->create(), ), 'post' => new ColumnDiff( $table->getColumn('post'), - new Column('comment', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('comment') + ->setTypeName(Types::INTEGER) + ->create(), ), ], droppedColumns: [ - new Column('parent', Type::getType(Types::INTEGER), []), + Column::editor() + ->setUnquotedName('parent') + ->setTypeName(Types::INTEGER) + ->create(), ], droppedIndexes: [ $table->getIndex('index1'), @@ -502,13 +556,23 @@ public function testReturnsGuidTypeDeclarationSQL(): void public function testGeneratesAlterTableRenameColumnSQLWithSchema(): void { - $table = new Table('main.t'); - $table->addColumn('a', Types::INTEGER); + $table = new Table('main.t', [ + Column::editor() + ->setUnquotedName('a') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableDiff = new TableDiff($table, changedColumns: [ 'a' => new ColumnDiff( - new Column('a', Type::getType(Types::INTEGER)), - new Column('b', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('a') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('b') + ->setTypeName(Types::INTEGER) + ->create(), ), ]); @@ -639,8 +703,13 @@ public function testGetCreateTableSQLWithColumnCollation(): void public function testCreateTableWithNonPrimaryKeyAutoIncrementColumn(): void { - $table = new Table('test_autoincrement'); - $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); + $table = new Table('test_autoincrement', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $this->expectException(UnsupportedTableDefinition::class); $this->platform->getCreateTableSQL($table); @@ -648,9 +717,17 @@ public function testCreateTableWithNonPrimaryKeyAutoIncrementColumn(): void public function testCreateTableWithCompositePrimaryKeyAutoIncrementColumn(): void { - $table = new Table('test_autoincrement'); - $table->addColumn('id1', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('id2', Types::INTEGER); + $table = new Table('test_autoincrement', [ + Column::editor() + ->setUnquotedName('id1') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('id2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id1', 'id2') diff --git a/tests/Schema/AbstractComparatorTestCase.php b/tests/Schema/AbstractComparatorTestCase.php index 6e246915601..283cfcd4b42 100644 --- a/tests/Schema/AbstractComparatorTestCase.php +++ b/tests/Schema/AbstractComparatorTestCase.php @@ -43,20 +43,20 @@ protected function setUp(): void public function testCompareSame1(): void { $schema1 = new Schema([ - 'bugdb' => new Table( - 'bugdb', - [ - 'integercolumn1' => new Column('integercolumn1', Type::getType(Types::INTEGER)), - ], - ), + new Table('bugdb', [ + Column::editor() + ->setUnquotedName('integercolumn1') + ->setTypeName(Types::INTEGER) + ->create(), + ]), ]); $schema2 = new Schema([ - 'bugdb' => new Table( - 'bugdb', - [ - 'integercolumn1' => new Column('integercolumn1', Type::getType(Types::INTEGER)), - ], - ), + new Table('bugdb', [ + Column::editor() + ->setUnquotedName('integercolumn1') + ->setTypeName(Types::INTEGER) + ->create(), + ]), ]); self::assertEquals( @@ -68,22 +68,28 @@ public function testCompareSame1(): void public function testCompareSame2(): void { $schema1 = new Schema([ - 'bugdb' => new Table( - 'bugdb', - [ - 'integercolumn1' => new Column('integercolumn1', Type::getType(Types::INTEGER)), - 'integercolumn2' => new Column('integercolumn2', Type::getType(Types::INTEGER)), - ], - ), + new Table('bugdb', [ + Column::editor() + ->setUnquotedName('integercolumn1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('integercolumn2') + ->setTypeName(Types::INTEGER) + ->create(), + ]), ]); $schema2 = new Schema([ - 'bugdb' => new Table( - 'bugdb', - [ - 'integercolumn2' => new Column('integercolumn2', Type::getType(Types::INTEGER)), - 'integercolumn1' => new Column('integercolumn1', Type::getType(Types::INTEGER)), - ], - ), + new Table('bugdb', [ + Column::editor() + ->setUnquotedName('integercolumn2') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('integercolumn1') + ->setTypeName(Types::INTEGER) + ->create(), + ]), ]); self::assertEquals( @@ -97,7 +103,10 @@ public function testCompareMissingTable(): void $schemaConfig = new SchemaConfig(); $table = new Table('bugdb', [ - 'integercolumn1' => new Column('integercolumn1', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('integercolumn1') + ->setTypeName(Types::INTEGER) + ->create(), ], [], [], [], [], $schemaConfig->toTableConfiguration()); $schema1 = new Schema([$table], [], $schemaConfig); @@ -114,7 +123,10 @@ public function testCompareNewTable(): void $schemaConfig = new SchemaConfig(); $table = new Table('bugdb', [ - 'integercolumn1' => new Column('integercolumn1', Type::getType(Types::INTEGER)), + Column::editor() + ->setUnquotedName('integercolumn1') + ->setTypeName(Types::INTEGER) + ->create(), ], [], [], [], [], $schemaConfig->toTableConfiguration()); $schema1 = new Schema([], [], $schemaConfig); @@ -127,8 +139,15 @@ public function testCompareNewTable(): void public function testCompareAutoIncrementChanged(): void { - $column1 = new Column('foo', Type::getType(Types::INTEGER), ['autoincrement' => true]); - $column2 = new Column('foo', Type::getType(Types::INTEGER), ['autoincrement' => false]); + $column1 = Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(); + + $column2 = $column1->edit() + ->setAutoincrement(false) + ->create(); $diff = new ColumnDiff($column2, $column1); @@ -137,8 +156,14 @@ public function testCompareAutoIncrementChanged(): void public function testCompareChangedColumnsChangeType(): void { - $column1 = new Column('id', Type::getType(Types::STRING)); - $column2 = new Column('id', Type::getType(Types::INTEGER)); + $column1 = Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::STRING) + ->create(); + + $column2 = $column1->edit() + ->setTypeName(Types::INTEGER) + ->create(); $diff12 = new ColumnDiff($column2, $column1); self::assertTrue($diff12->hasTypeChanged()); @@ -154,8 +179,14 @@ public function testDifferentTypeInstancesOfTheSameType(): void self::assertNotSame($type1, $type2); - $column1 = new Column('id', $type1); - $column2 = new Column('id', $type2); + $column1 = Column::editor() + ->setUnquotedName('id') + ->setType($type1) + ->create(); + + $column2 = $column1->edit() + ->setType($type2) + ->create(); $diff = new ColumnDiff($column2, $column1); self::assertFalse($diff->hasTypeChanged()); @@ -171,8 +202,14 @@ public function testOverriddenType(): void Type::overrideType(Types::STRING, $defaultStringType::class); - $column1 = new Column('id', $integerType); - $column2 = new Column('id', $overriddenStringType); + $column1 = Column::editor() + ->setUnquotedName('id') + ->setType($integerType) + ->create(); + + $column2 = $column1->edit() + ->setType($overriddenStringType) + ->create(); $diff = new ColumnDiff($column2, $column1); self::assertFalse($diff->hasTypeChanged()); @@ -180,12 +217,23 @@ public function testOverriddenType(): void public function testCompareChangeColumnsMultipleNewColumnsRename(): void { - $tableA = new Table('foo'); - $tableA->addColumn('datecolumn1', Types::DATETIME_MUTABLE); + $tableA = new Table('foo', [ + Column::editor() + ->setUnquotedName('datecolumn1') + ->setTypeName(Types::DATETIME_MUTABLE) + ->create(), + ]); - $tableB = new Table('foo'); - $tableB->addColumn('new_datecolumn1', Types::DATETIME_MUTABLE); - $tableB->addColumn('new_datecolumn2', Types::DATETIME_MUTABLE); + $tableB = new Table('foo', [ + Column::editor() + ->setUnquotedName('new_datecolumn1') + ->setTypeName(Types::DATETIME_MUTABLE) + ->create(), + Column::editor() + ->setUnquotedName('new_datecolumn2') + ->setTypeName(Types::DATETIME_MUTABLE) + ->create(), + ]); $tableDiff = $this->comparator->compareTables($tableA, $tableB); @@ -234,14 +282,26 @@ public function testAddedSequence(): void public function testTableAddForeignKey(): void { - $tableForeign = new Table('bar'); - $tableForeign->addColumn('id', Types::INTEGER); + $tableForeign = new Table('bar', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $table1 = new Table('foo'); - $table1->addColumn('fk', Types::INTEGER); + $table1 = new Table('foo', [ + Column::editor() + ->setUnquotedName('fk') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $table2 = new Table('foo'); - $table2->addColumn('fk', Types::INTEGER); + $table2 = new Table('foo', [ + Column::editor() + ->setUnquotedName('fk') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table2->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id']); $tableDiff = $this->comparator->compareTables($table1, $table2); @@ -251,14 +311,26 @@ public function testTableAddForeignKey(): void public function testTableDropForeignKey(): void { - $tableForeign = new Table('bar'); - $tableForeign->addColumn('id', Types::INTEGER); + $tableForeign = new Table('bar', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $table1 = new Table('foo'); - $table1->addColumn('fk', Types::INTEGER); + $table1 = new Table('foo', [ + Column::editor() + ->setUnquotedName('fk') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $table2 = new Table('foo'); - $table2->addColumn('fk', Types::INTEGER); + $table2 = new Table('foo', [ + Column::editor() + ->setUnquotedName('fk') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table2->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id']); $tableDiff = $this->comparator->compareTables($table2, $table1); @@ -268,15 +340,27 @@ public function testTableDropForeignKey(): void public function testTableUpdateForeignKey(): void { - $tableForeign = new Table('bar'); - $tableForeign->addColumn('id', Types::INTEGER); + $tableForeign = new Table('bar', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $table1 = new Table('foo'); - $table1->addColumn('fk', Types::INTEGER); + $table1 = new Table('foo', [ + Column::editor() + ->setUnquotedName('fk') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table1->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id']); - $table2 = new Table('foo'); - $table2->addColumn('fk', Types::INTEGER); + $table2 = new Table('foo', [ + Column::editor() + ->setUnquotedName('fk') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table2->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id'], ['onUpdate' => 'CASCADE']); $tableDiff = $this->comparator->compareTables($table1, $table2); @@ -287,18 +371,34 @@ public function testTableUpdateForeignKey(): void public function testMovedForeignKeyForeignTable(): void { - $tableForeign = new Table('bar'); - $tableForeign->addColumn('id', Types::INTEGER); + $tableForeign = new Table('bar', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $tableForeign2 = new Table('bar2'); - $tableForeign2->addColumn('id', Types::INTEGER); + $tableForeign2 = new Table('bar2', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $table1 = new Table('foo'); - $table1->addColumn('fk', Types::INTEGER); + $table1 = new Table('foo', [ + Column::editor() + ->setUnquotedName('fk') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table1->addForeignKeyConstraint($tableForeign->getName(), ['fk'], ['id']); - $table2 = new Table('foo'); - $table2->addColumn('fk', Types::INTEGER); + $table2 = new Table('foo', [ + Column::editor() + ->setUnquotedName('fk') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table2->addForeignKeyConstraint($tableForeign2->getName(), ['fk'], ['id']); $tableDiff = $this->comparator->compareTables($table1, $table2); @@ -351,11 +451,19 @@ public function testSequencesCaseInsensitive(): void public function testCompareColumnCompareCaseInsensitive(): void { - $tableA = new Table('foo'); - $tableA->addColumn('id', Types::INTEGER); + $tableA = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $tableB = new Table('foo'); - $tableB->addColumn('ID', Types::INTEGER); + $tableB = new Table('foo', [ + Column::editor() + ->setUnquotedName('ID') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableDiff = $this->comparator->compareTables($tableA, $tableB); @@ -364,12 +472,20 @@ public function testCompareColumnCompareCaseInsensitive(): void public function testCompareIndexBasedOnPropertiesNotName(): void { - $tableA = new Table('foo'); - $tableA->addColumn('id', Types::INTEGER); + $tableA = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableA->addIndex(['id'], 'foo_bar_idx'); - $tableB = new Table('foo'); - $tableB->addColumn('ID', Types::INTEGER); + $tableB = new Table('foo', [ + Column::editor() + ->setUnquotedName('ID') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableB->addIndex(['id'], 'bar_foo_idx'); self::assertEquals( @@ -385,12 +501,20 @@ public function testCompareIndexBasedOnPropertiesNotName(): void public function testCompareForeignKeyBasedOnPropertiesNotName(): void { - $tableA = new Table('foo'); - $tableA->addColumn('id', Types::INTEGER); + $tableA = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableA->addForeignKeyConstraint('bar', ['id'], ['id'], [], 'foo_constraint'); - $tableB = new Table('foo'); - $tableB->addColumn('ID', Types::INTEGER); + $tableB = new Table('foo', [ + Column::editor() + ->setUnquotedName('ID') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableB->addForeignKeyConstraint('bar', ['id'], ['id'], [], 'bar_constraint'); self::assertEquals( @@ -401,11 +525,19 @@ public function testCompareForeignKeyBasedOnPropertiesNotName(): void public function testDetectRenameColumn(): void { - $tableA = new Table('foo'); - $tableA->addColumn('foo', Types::INTEGER); + $tableA = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $tableB = new Table('foo'); - $tableB->addColumn('bar', Types::INTEGER); + $tableB = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableDiff = $this->comparator->compareTables($tableA, $tableB); @@ -419,11 +551,19 @@ public function testDetectRenameColumn(): void public function testDetectRenameColumnDisabled(): void { - $tableA = new Table('foo'); - $tableA->addColumn('foo', Types::INTEGER); + $tableA = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $tableB = new Table('foo'); - $tableB->addColumn('bar', Types::INTEGER); + $tableB = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->comparator = $this->createComparator((new ComparatorConfig())->withDetectRenamedColumns(false)); $tableDiff = $this->comparator->compareTables($tableA, $tableB); @@ -440,12 +580,23 @@ public function testDetectRenameColumnDisabled(): void */ public function testDetectRenameColumnAmbiguous(): void { - $tableA = new Table('foo'); - $tableA->addColumn('foo', Types::INTEGER); - $tableA->addColumn('bar', Types::INTEGER); + $tableA = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $tableB = new Table('foo'); - $tableB->addColumn('baz', Types::INTEGER); + $tableB = new Table('foo', [ + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableDiff = $this->comparator->compareTables($tableA, $tableB); @@ -456,8 +607,12 @@ public function testDetectRenameColumnAmbiguous(): void public function testDetectRenameIndex(): void { - $table1 = new Table('foo'); - $table1->addColumn('foo', Types::INTEGER); + $table1 = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table2 = clone $table1; @@ -477,8 +632,12 @@ public function testDetectRenameIndex(): void public function testDetectRenameIndexDisabled(): void { - $table1 = new Table('foo'); - $table1->addColumn('foo', Types::INTEGER); + $table1 = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table2 = clone $table1; @@ -501,8 +660,12 @@ public function testDetectRenameIndexDisabled(): void */ public function testDetectRenameIndexAmbiguous(): void { - $table1 = new Table('foo'); - $table1->addColumn('foo', Types::INTEGER); + $table1 = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table2 = clone $table1; @@ -520,11 +683,20 @@ public function testDetectRenameIndexAmbiguous(): void public function testDetectChangeIdentifierType(): void { - $tableA = new Table('foo'); - $tableA->addColumn('id', Types::INTEGER, ['autoincrement' => false]); + $tableA = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $tableB = new Table('foo'); - $tableB->addColumn('id', Types::INTEGER, ['autoincrement' => true]); + $tableB = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + ]); $tableDiff = $this->comparator->compareTables($tableA, $tableB); @@ -537,21 +709,48 @@ public function testDetectChangeIdentifierType(): void public function testDiff(): void { - $table = new Table('twitter_users'); - $table->addColumn('id', Types::INTEGER, ['autoincrement' => true]); - $table->addColumn('twitterId', Types::INTEGER); - $table->addColumn('displayName', Types::STRING, ['length' => 32]); + $table = new Table('twitter_users', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('twitterId') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('displayName') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') ->create(), ); - $newtable = new Table('twitter_users'); - $newtable->addColumn('id', Types::INTEGER, ['autoincrement' => true]); - $newtable->addColumn('twitter_id', Types::INTEGER); - $newtable->addColumn('display_name', Types::STRING, ['length' => 32]); - $newtable->addColumn('logged_in_at', Types::DATETIME_MUTABLE); + $newtable = new Table('twitter_users', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->setAutoincrement(true) + ->create(), + Column::editor() + ->setUnquotedName('twitter_id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('display_name') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + Column::editor() + ->setUnquotedName('logged_in_at') + ->setTypeName(Types::DATETIME_MUTABLE) + ->create(), + ]); $newtable->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() ->setUnquotedColumnNames('id') @@ -645,8 +844,15 @@ public function testComparesNamespaces(): void #[DataProvider('getCompareColumnComments')] public function testCompareColumnComments(string $comment1, string $comment2, bool $equals): void { - $column1 = new Column('foo', Type::getType(Types::INTEGER), ['comment' => $comment1]); - $column2 = new Column('foo', Type::getType(Types::INTEGER), ['comment' => $comment2]); + $column1 = Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->setComment($comment1) + ->create(); + + $column2 = $column1->edit() + ->setComment($comment2) + ->create(); $diff1 = new ColumnDiff($column2, $column1); $diff2 = new ColumnDiff($column1, $column2); @@ -678,53 +884,53 @@ public static function getCompareColumnComments(): iterable public function testForeignKeyRemovalWithRenamedLocalColumn(): void { $oldSchema = new Schema([ - 'table1' => new Table( - 'table1', - [ - 'id' => new Column('id', Type::getType(Types::INTEGER)), - ], - ), - 'table2' => new Table( - 'table2', - [ - 'id' => new Column('id', Type::getType(Types::INTEGER)), - 'id_table1' => new Column('id_table1', Type::getType(Types::INTEGER)), - ], - [], - [], - [ - ForeignKeyConstraint::editor() - ->setUnquotedReferencingColumnNames('id_table1') - ->setUnquotedReferencedTableName('table1') - ->setUnquotedReferencedColumnNames('fk_table2_table1') - ->create(), - ], - ), + new Table('table1', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]), + new Table('table2', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('id_table1') + ->setTypeName(Types::INTEGER) + ->create(), + ], [], [], [ + ForeignKeyConstraint::editor() + ->setUnquotedReferencingColumnNames('id_table1') + ->setUnquotedReferencedTableName('table1') + ->setUnquotedReferencedColumnNames('fk_table2_table1') + ->create(), + ]), ]); $newSchema = new Schema([ - 'table2' => new Table( - 'table2', - [ - 'id' => new Column('id', Type::getType(Types::INTEGER)), - 'id_table3' => new Column('id_table3', Type::getType(Types::INTEGER)), - ], - [], - [], - [ - ForeignKeyConstraint::editor() - ->setUnquotedName('fk_table2_table3') - ->setUnquotedReferencingColumnNames('id_table3') - ->setUnquotedReferencedTableName('table3') - ->setUnquotedReferencedColumnNames('id') - ->create(), - ], - ), - 'table3' => new Table( - 'table3', - [ - 'id' => new Column('id', Type::getType(Types::INTEGER)), - ], - ), + new Table('table2', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('id_table3') + ->setTypeName(Types::INTEGER) + ->create(), + ], [], [], [ + ForeignKeyConstraint::editor() + ->setUnquotedName('fk_table2_table3') + ->setUnquotedReferencingColumnNames('id_table3') + ->setUnquotedReferencedTableName('table3') + ->setUnquotedReferencedColumnNames('id') + ->create(), + ]), + new Table('table3', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]), ]); $schemaDiff = $this->comparator->compareSchemas($oldSchema, $newSchema); @@ -742,37 +948,25 @@ public function testForeignKeyRemovalWithRenamedLocalColumn(): void public function testWillNotProduceSchemaDiffOnTableWithAddedCustomSchemaDefinition(): void { - $oldSchema = new Schema( - [ - new Table( - 'a_table', - [ - new Column( - 'is_default', - Type::getType(Types::STRING), - ['length' => 32], - ), - ], - ), - ], - ); - $newSchema = new Schema( - [ - new Table( - 'a_table', - [ - new Column( - 'is_default', - Type::getType(Types::STRING), - [ - 'columnDefinition' => 'ENUM(\'default\')', - 'length' => 32, - ], - ), - ], - ), - ], - ); + $oldSchema = new Schema([ + new Table('a_table', [ + Column::editor() + ->setUnquotedName('is_default') + ->setTypeName(Types::STRING) + ->setLength(32) + ->create(), + ]), + ]); + $newSchema = new Schema([ + new Table('a_table', [ + Column::editor() + ->setUnquotedName('is_default') + ->setTypeName(Types::STRING) + ->setLength(32) + ->setColumnDefinition('ENUM(\'default\')') + ->create(), + ]), + ]); self::assertEmpty( $this->comparator->compareSchemas($oldSchema, $newSchema) diff --git a/tests/Schema/ColumnEditorTest.php b/tests/Schema/ColumnEditorTest.php new file mode 100644 index 00000000000..3ca067c412e --- /dev/null +++ b/tests/Schema/ColumnEditorTest.php @@ -0,0 +1,81 @@ +setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(); + + self::assertEquals( + UnqualifiedName::unquoted('id'), + $column->getObjectName(), + ); + } + + public function testSetQuotedName(): void + { + $column = Column::editor() + ->setQuotedName('id') + ->setTypeName(Types::INTEGER) + ->create(); + + self::assertEquals( + UnqualifiedName::quoted('id'), + $column->getObjectName(), + ); + } + + public function testSetType(): void + { + $type = new IntegerType(); + + $column = Column::editor() + ->setUnquotedName('id') + ->setType($type) + ->create(); + + self::assertSame($type, $column->getType()); + } + + public function testSetTypeName(): void + { + $column = Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(); + + self::assertEquals(Type::getType(Types::INTEGER), $column->getType()); + } + + public function testNameNotSet(): void + { + $this->expectException(InvalidColumnDefinition::class); + + Column::editor()->create(); + } + + public function testTypeNotSet(): void + { + $editor = Column::editor() + ->setUnquotedName('id'); + + $this->expectException(InvalidColumnDefinition::class); + + $editor->create(); + } +} diff --git a/tests/Schema/ColumnTest.php b/tests/Schema/ColumnTest.php index c5a482aca88..9702fb879f1 100644 --- a/tests/Schema/ColumnTest.php +++ b/tests/Schema/ColumnTest.php @@ -85,26 +85,26 @@ public function testOptionsShouldNotBeIgnored(): void public function createColumn(): Column { - $options = [ - 'length' => 200, - 'precision' => 5, - 'scale' => 2, - 'unsigned' => true, - 'notnull' => false, - 'fixed' => true, - 'default' => 'baz', - 'platformOptions' => ['charset' => 'utf8'], - ]; - - $string = Type::getType(Types::STRING); - - return new Column('foo', $string, $options); + return Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::STRING) + ->setLength(200) + ->setPrecision(5) + ->setScale(2) + ->setUnsigned(true) + ->setNotNull(false) + ->setFixed(true) + ->setDefaultValue('baz') + ->setCharset('utf8') + ->create(); } public function testQuotedColumnName(): void { - $string = Type::getType(Types::STRING); - $column = new Column('`bar`', $string, []); + $column = Column::editor() + ->setQuotedName('bar') + ->setTypeName(Types::STRING) + ->create(); $mysqlPlatform = new MySQLPlatform(); $sqlitePlatform = new SQLitePlatform(); @@ -113,7 +113,10 @@ public function testQuotedColumnName(): void self::assertEquals('`bar`', $column->getObjectName()->toSQL($mysqlPlatform)); self::assertEquals('"bar"', $column->getObjectName()->toSQL($sqlitePlatform)); - $column = new Column('[bar]', $string); + $column = Column::editor() + ->setQuotedName('bar') + ->setTypeName(Types::STRING) + ->create(); $sqlServerPlatform = new SQLServerPlatform(); @@ -143,7 +146,10 @@ public static function getIsQuoted(): iterable public function testColumnComment(): void { - $column = new Column('bar', Type::getType(Types::STRING)); + $column = Column::editor() + ->setUnquotedName('bar') + ->setType(Type::getType(Types::STRING)) + ->create(); self::assertSame('', $column->getComment()); $column->setComment('foo'); @@ -167,13 +173,19 @@ public function testQualifiedName(): void { $this->expectException(InvalidName::class); - new Column('t.id', Type::getType(Types::INTEGER)); + Column::editor() + ->setUnquotedName('t.id') + ->setType(Type::getType(Types::INTEGER)) + ->create(); } /** @throws Exception */ public function testGetObjectName(): void { - $column = new Column('id', Type::getType(Types::INTEGER)); + $column = Column::editor() + ->setUnquotedName('id') + ->setType(Type::getType(Types::INTEGER)) + ->create(); self::assertEquals(Identifier::unquoted('id'), $column->getObjectName()->getIdentifier()); } diff --git a/tests/Schema/MySQLInheritCharsetTest.php b/tests/Schema/MySQLInheritCharsetTest.php index a9a0cb94e8c..1e5a174c470 100644 --- a/tests/Schema/MySQLInheritCharsetTest.php +++ b/tests/Schema/MySQLInheritCharsetTest.php @@ -12,7 +12,6 @@ use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\MySQLSchemaManager; use Doctrine\DBAL\Schema\Table; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\TestCase; @@ -41,14 +40,22 @@ public function testTableOptions(): void $platform = new MySQLPlatform(); // no options - $table = new Table('foobar', [new Column('aa', Type::getType(Types::INTEGER))]); + $table = new Table('foobar', [Column::editor() + ->setUnquotedName('aa') + ->setTypeName(Types::INTEGER) + ->create(), + ]); self::assertSame( ['CREATE TABLE `foobar` (`aa` INT NOT NULL)'], $platform->getCreateTableSQL($table), ); // charset - $table = new Table('foobar', [new Column('aa', Type::getType(Types::INTEGER))]); + $table = new Table('foobar', [Column::editor() + ->setUnquotedName('aa') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addOption('charset', 'utf8'); self::assertSame( ['CREATE TABLE `foobar` (`aa` INT NOT NULL) DEFAULT CHARACTER SET utf8'], diff --git a/tests/Schema/Platforms/MySQLSchemaTest.php b/tests/Schema/Platforms/MySQLSchemaTest.php index 586270da934..248a8bbc271 100644 --- a/tests/Schema/Platforms/MySQLSchemaTest.php +++ b/tests/Schema/Platforms/MySQLSchemaTest.php @@ -10,6 +10,7 @@ use Doctrine\DBAL\Platforms\MySQL\CollationMetadataProvider; use Doctrine\DBAL\Platforms\MySQL\DefaultTableOptions; use Doctrine\DBAL\Platforms\MySQLPlatform; +use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\ComparatorConfig; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; @@ -28,8 +29,12 @@ protected function setUp(): void public function testGenerateForeignKeySQL(): void { - $tableOld = new Table('test'); - $tableOld->addColumn('foo_id', Types::INTEGER); + $tableOld = new Table('test', [ + Column::editor() + ->setUnquotedName('foo_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $tableOld->addForeignKeyConstraint('test_foreign', ['foo_id'], ['foo_id']); $sqls = []; @@ -48,9 +53,17 @@ public function testGenerateForeignKeySQL(): void public function testClobNoAlterTable(): void { - $tableOld = new Table('test'); - $tableOld->addColumn('id', Types::INTEGER); - $tableOld->addColumn('description', Types::STRING, ['length' => 65536]); + $tableOld = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('description') + ->setTypeName(Types::STRING) + ->setLength(65536) + ->create(), + ]); $tableNew = $tableOld->edit() ->setPrimaryKeyConstraint( diff --git a/tests/Schema/TableEditorTest.php b/tests/Schema/TableEditorTest.php index ee84b7a4f5f..cc3900fc156 100644 --- a/tests/Schema/TableEditorTest.php +++ b/tests/Schema/TableEditorTest.php @@ -8,7 +8,6 @@ use Doctrine\DBAL\Schema\Exception\InvalidTableDefinition; use Doctrine\DBAL\Schema\Name\OptionallyQualifiedName; use Doctrine\DBAL\Schema\Table; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\TestCase; @@ -44,8 +43,12 @@ public function testSetName(): void { $name = OptionallyQualifiedName::unquoted('contacts'); - $table = new Table('accounts'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('accounts', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table = $table->edit() ->setName($name) @@ -73,6 +76,9 @@ public function testColumnsNotSet(): void private function createColumn(): Column { - return new Column('id', Type::getType(Types::INTEGER)); + return Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(); } } diff --git a/tests/Schema/TableTest.php b/tests/Schema/TableTest.php index a125acd714f..af29da4cdbd 100644 --- a/tests/Schema/TableTest.php +++ b/tests/Schema/TableTest.php @@ -52,11 +52,16 @@ public function testGetName(): void public function testColumns(): void { - $type = Type::getType(Types::INTEGER); - $columns = []; - $columns[] = new Column('foo', $type); - $columns[] = new Column('bar', $type); - $table = new Table('foo', $columns, [], []); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); self::assertTrue($table->hasColumn('foo')); self::assertTrue($table->hasColumn('bar')); @@ -116,9 +121,16 @@ public function testRenameColumnLoop(): void public function testRenameColumnInIndex(): void { - $table = new Table('t'); - $table->addColumn('c1', Types::INTEGER); - $table->addColumn('c2', Types::INTEGER); + $table = new Table('t', [ + Column::editor() + ->setUnquotedName('c1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('c2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['c1', 'c2'], 'idx_c1_c2'); $table->renameColumn('c1', 'c1a'); @@ -130,9 +142,16 @@ public function testRenameColumnInIndex(): void public function testRenameColumnInForeignKeyConstraint(): void { - $table = new Table('t1'); - $table->addColumn('c1', Types::INTEGER); - $table->addColumn('c2', Types::INTEGER); + $table = new Table('t1', [ + Column::editor() + ->setUnquotedName('c1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('c2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addForeignKeyConstraint('t2', ['c1', 'c2'], ['c1', 'c2'], [], 'fk_c1_c2'); $table->renameColumn('c2', 'c2a'); @@ -144,9 +163,16 @@ public function testRenameColumnInForeignKeyConstraint(): void public function testRenameColumnInUniqueConstraint(): void { - $table = new Table('t'); - $table->addColumn('c1', Types::INTEGER); - $table->addColumn('c2', Types::INTEGER); + $table = new Table('t', [ + Column::editor() + ->setUnquotedName('c1') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('c2') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addUniqueConstraint(['c1', 'c2'], 'uq_c1_c2'); $table->renameColumn('c1', 'c1a'); @@ -158,14 +184,18 @@ public function testRenameColumnInUniqueConstraint(): void public function testColumnsCaseInsensitive(): void { - $table = new Table('foo'); - $column = $table->addColumn('Foo', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('Foo') + ->setTypeName(Types::INTEGER) + ->create(), + ]); self::assertTrue($table->hasColumn('Foo')); self::assertTrue($table->hasColumn('foo')); self::assertTrue($table->hasColumn('FOO')); - self::assertSame($column, $table->getColumn('Foo')); + $column = $table->getColumn('Foo'); self::assertSame($column, $table->getColumn('foo')); self::assertSame($column, $table->getColumn('FOO')); } @@ -184,11 +214,16 @@ public function testCreateColumn(): void public function testDropColumn(): void { - $type = Type::getType(Types::INTEGER); - $columns = []; - $columns[] = new Column('foo', $type); - $columns[] = new Column('bar', $type); - $table = new Table('foo', $columns, [], []); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); self::assertTrue($table->hasColumn('foo')); self::assertTrue($table->hasColumn('bar')); @@ -211,18 +246,34 @@ public function testAddColumnTwiceThrowsException(): void { $this->expectException(SchemaException::class); - $type = Type::getType(Types::INTEGER); - $columns = []; - $columns[] = new Column('foo', $type); - $columns[] = new Column('foo', $type); - new Table('foo', $columns, [], []); + new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + ]); } public function testCreateIndex(): void { - $type = Type::getType(Types::INTEGER); - $columns = [new Column('foo', $type), new Column('bar', $type), new Column('baz', $type)]; - $table = new Table('foo', $columns); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['foo', 'bar'], 'foo_foo_bar_idx'); $table->addUniqueIndex(['bar', 'baz'], 'foo_bar_baz_uniq'); @@ -233,13 +284,20 @@ public function testCreateIndex(): void public function testIndexCaseInsensitive(): void { - $type = Type::getType(Types::INTEGER); - $columns = [ - new Column('foo', $type), - new Column('bar', $type), - new Column('baz', $type), - ]; - $table = new Table('foo', $columns); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['foo', 'bar', 'baz'], 'Foo_Idx'); @@ -250,10 +308,15 @@ public function testIndexCaseInsensitive(): void public function testAddIndexes(): void { - $type = Type::getType(Types::INTEGER); $columns = [ - new Column('foo', $type), - new Column('bar', $type), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), ]; $indexes = [ Index::editor() @@ -295,8 +358,16 @@ public function testAddTwoIndexesWithSameNameThrowsException(): void ->setUnquotedColumnNames('bar') ->create(); - $type = Type::getType(Types::INTEGER); - $columns = [new Column('foo', $type), new Column('bar', $type)]; + $columns = [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]; $indexes = [$index1, $index2]; $this->expectException(SchemaException::class); @@ -313,9 +384,12 @@ public function testOptions(): void public function testBuilderAddUniqueIndex(): void { - $table = new Table('foo'); - - $table->addColumn('bar', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addUniqueIndex(['bar'], 'my_idx'); self::assertTrue($table->hasIndex('my_idx')); @@ -324,9 +398,12 @@ public function testBuilderAddUniqueIndex(): void public function testBuilderAddIndex(): void { - $table = new Table('foo'); - - $table->addColumn('bar', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['bar'], 'my_idx'); self::assertTrue($table->hasIndex('my_idx')); @@ -337,8 +414,12 @@ public function testBuilderAddIndexWithInvalidNameThrowsException(): void { $this->expectException(SchemaException::class); - $table = new Table('foo'); - $table->addColumn('bar', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['bar'], 'invalid name %&/'); } @@ -360,22 +441,27 @@ public function testBuilderOptions(): void public function testAddForeignKeyConstraintUnknownLocalColumnThrowsException(): void { - $this->expectException(SchemaException::class); - - $table = new Table('foo'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $foreignTable = new Table('bar'); - $foreignTable->addColumn('id', Types::INTEGER); + $this->expectException(SchemaException::class); - $table->addForeignKeyConstraint($foreignTable->getName(), ['foo'], ['id']); + $table->addForeignKeyConstraint('bar', ['foo'], ['id']); } /** @throws Exception */ public function testAddForeignKeyConstraintWithInvalidMatchType(): void { - $table = new Table('foo'); - $table->addColumn('bar_id', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->expectException(ValueError::class); @@ -385,8 +471,12 @@ public function testAddForeignKeyConstraintWithInvalidMatchType(): void /** @throws Exception */ public function testAddForeignKeyConstraintWithInvalidOnUpdateAction(): void { - $table = new Table('foo'); - $table->addColumn('bar_id', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->expectException(ValueError::class); @@ -396,8 +486,12 @@ public function testAddForeignKeyConstraintWithInvalidOnUpdateAction(): void /** @throws Exception */ public function testAddForeignKeyConstraintWithInvalidOnDeleteAction(): void { - $table = new Table('foo'); - $table->addColumn('bar_id', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->expectException(ValueError::class); @@ -406,8 +500,12 @@ public function testAddForeignKeyConstraintWithInvalidOnDeleteAction(): void public function testAddForeignKeyConstraintWithInvalidDeferrability(): void { - $table = new Table('foo'); - $table->addColumn('bar_id', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->expectException(InvalidForeignKeyConstraintDefinition::class); @@ -419,8 +517,12 @@ public function testAddForeignKeyConstraintWithInvalidDeferrability(): void public function testAllowImplicitSchemaTableInAutogeneratedIndexNames(): void { - $table = new Table('foo.bar'); - $table->addColumn('baz', Types::INTEGER, []); + $table = new Table('foo.bar', [ + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['baz']); self::assertCount(1, $table->getIndexes()); @@ -428,13 +530,14 @@ public function testAllowImplicitSchemaTableInAutogeneratedIndexNames(): void public function testAddForeignKeyIndexImplicitly(): void { - $table = new Table('foo'); - $table->addColumn('id', Types::INTEGER); - - $foreignTable = new Table('bar'); - $foreignTable->addColumn('id', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); - $table->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id'], ['foo' => 'bar']); + $table->addForeignKeyConstraint('bar', ['id'], ['id'], ['foo' => 'bar']); $indexes = $table->getIndexes(); self::assertCount(1, $indexes); @@ -450,14 +553,14 @@ public function testAddForeignKeyIndexImplicitly(): void public function testAddForeignKeyDoesNotCreateDuplicateIndex(): void { - $table = new Table('foo'); - $table->addColumn('bar', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['bar'], 'bar_idx'); - - $foreignTable = new Table('bar'); - $foreignTable->addColumn('foo', Types::INTEGER); - - $table->addForeignKeyConstraint($foreignTable->getName(), ['bar'], ['foo']); + $table->addForeignKeyConstraint('bar', ['bar'], ['foo']); self::assertCount(1, $table->getIndexes()); self::assertTrue($table->hasIndex('bar_idx')); @@ -469,18 +572,23 @@ public function testAddForeignKeyDoesNotCreateDuplicateIndex(): void public function testAddForeignKeyAddsImplicitIndexIfIndexColumnsDoNotSpan(): void { - $table = new Table('foo'); - $table->addColumn('bar', Types::INTEGER); - $table->addColumn('baz', Types::STRING); - $table->addColumn('bloo', Types::STRING); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('bloo') + ->setTypeName(Types::STRING) + ->create(), + ]); $table->addIndex(['baz', 'bar'], 'composite_idx'); $table->addIndex(['bar', 'baz', 'bloo'], 'full_idx'); - - $foreignTable = new Table('bar'); - $foreignTable->addColumn('foo', Types::INTEGER); - $foreignTable->addColumn('baz', Types::STRING); - - $table->addForeignKeyConstraint($foreignTable->getName(), ['bar', 'baz'], ['foo', 'baz']); + $table->addForeignKeyConstraint('bar', ['bar', 'baz'], ['foo', 'baz']); self::assertCount(3, $table->getIndexes()); self::assertTrue($table->hasIndex('composite_idx')); @@ -506,8 +614,12 @@ public function testAddForeignKeyAddsImplicitIndexIfIndexColumnsDoNotSpan(): voi public function testOverrulingIndexDoesNotDropOverruledIndex(): void { - $table = new Table('bar'); - $table->addColumn('baz', Types::INTEGER, []); + $table = new Table('bar', [ + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['baz']); $indexes = $table->getIndexes(); @@ -522,8 +634,12 @@ public function testOverrulingIndexDoesNotDropOverruledIndex(): void public function testAllowsAddingDuplicateIndexesBasedOnColumns(): void { - $table = new Table('foo'); - $table->addColumn('bar', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['bar'], 'bar_idx'); $table->addIndex(['bar'], 'duplicate_idx'); @@ -542,9 +658,16 @@ public function testAllowsAddingDuplicateIndexesBasedOnColumns(): void public function testAllowsAddingFulfillingIndexesBasedOnColumns(): void { - $table = new Table('foo'); - $table->addColumn('bar', Types::INTEGER); - $table->addColumn('baz', Types::STRING); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::STRING) + ->create(), + ]); $table->addIndex(['bar'], 'bar_idx'); $table->addIndex(['bar', 'baz'], 'fulfilling_idx'); @@ -564,12 +687,13 @@ public function testAllowsAddingFulfillingIndexesBasedOnColumns(): void public function testAddingFulfillingRegularIndexOverridesImplicitForeignKeyConstraintIndex(): void { - $foreignTable = new Table('foreign'); - $foreignTable->addColumn('id', Types::INTEGER); - - $localTable = new Table('local'); - $localTable->addColumn('id', Types::INTEGER); - $localTable->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id']); + $localTable = new Table('local', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); + $localTable->addForeignKeyConstraint('foreign', ['id'], ['id']); self::assertCount(1, $localTable->getIndexes()); @@ -581,12 +705,13 @@ public function testAddingFulfillingRegularIndexOverridesImplicitForeignKeyConst public function testAddingFulfillingUniqueIndexOverridesImplicitForeignKeyConstraintIndex(): void { - $foreignTable = new Table('foreign'); - $foreignTable->addColumn('id', Types::INTEGER); - - $localTable = new Table('local'); - $localTable->addColumn('id', Types::INTEGER); - $localTable->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id']); + $localTable = new Table('local', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); + $localTable->addForeignKeyConstraint('foreign', ['id'], ['id']); self::assertCount(1, $localTable->getIndexes()); @@ -598,12 +723,13 @@ public function testAddingFulfillingUniqueIndexOverridesImplicitForeignKeyConstr public function testAddingFulfillingExplicitIndexOverridingImplicitForeignKeyConstraintIndexWithSameName(): void { - $foreignTable = new Table('foreign'); - $foreignTable->addColumn('id', Types::INTEGER); - - $localTable = new Table('local'); - $localTable->addColumn('id', Types::INTEGER); - $localTable->addForeignKeyConstraint($foreignTable->getName(), ['id'], ['id']); + $localTable = new Table('local', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); + $localTable->addForeignKeyConstraint('foreign', ['id'], ['id']); self::assertCount(1, $localTable->getIndexes()); self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750')); @@ -631,14 +757,18 @@ public function testQuotedTableName(): void public function testTableHasPrimaryKey(): void { - $table = new Table('test'); - $table->addColumn('foo', Types::INTEGER); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + ]); self::assertNull($table->getPrimaryKeyConstraint()); $table->addPrimaryKeyConstraint( PrimaryKeyConstraint::editor() - ->setUnquotedColumnNames('id') + ->setUnquotedColumnNames('foo') ->create(), ); @@ -651,9 +781,17 @@ public function testTableHasPrimaryKey(): void public function testAddForeignKeyWithQuotedColumnsAndTable(): void { - $table = new Table('test'); - $table->addColumn('"foo"', Types::INTEGER); - $table->addColumn('bar', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setQuotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); + $table->addForeignKeyConstraint('"boing"', ['"foo"', '"bar"'], ['id1', 'id2']); self::assertCount(1, $table->getForeignKeys()); @@ -668,8 +806,12 @@ public function testQuoteSchemaPrefixed(): void public function testDropIndex(): void { - $table = new Table('test'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['id'], 'idx'); self::assertTrue($table->hasIndex('idx')); @@ -680,11 +822,24 @@ public function testDropIndex(): void public function testRenameIndex(): void { - $table = new Table('test'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('foo', Types::INTEGER); - $table->addColumn('bar', Types::INTEGER); - $table->addColumn('baz', Types::INTEGER); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('baz') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['foo'], 'idx', ['clustered']); $table->addUniqueIndex(['bar', 'baz'], 'uniq'); @@ -762,8 +917,12 @@ public function testRenameNonExistingIndexToTheSameName(): void public function testKeepsPredicateOnRenamingRegularIndex(): void { - $table = new Table('foo'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['id'], 'idx_bar', [], ['where' => '1 = 1']); $table->renameIndex('idx_bar', 'idx_baz'); @@ -773,8 +932,12 @@ public function testKeepsPredicateOnRenamingRegularIndex(): void public function testKeepsPredicateOnRenamingUniqueIndex(): void { - $table = new Table('foo'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addUniqueIndex(['id'], 'idx_bar', ['where' => '1 = 1']); $table->renameIndex('idx_bar', 'idx_baz'); @@ -784,8 +947,12 @@ public function testKeepsPredicateOnRenamingUniqueIndex(): void public function testThrowsExceptionOnRenamingNonExistingIndex(): void { - $table = new Table('test'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['id'], 'idx'); $this->expectException(SchemaException::class); @@ -795,9 +962,16 @@ public function testThrowsExceptionOnRenamingNonExistingIndex(): void public function testThrowsExceptionOnRenamingToAlreadyExistingIndex(): void { - $table = new Table('test'); - $table->addColumn('id', Types::INTEGER); - $table->addColumn('foo', Types::INTEGER); + $table = new Table('test', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + Column::editor() + ->setUnquotedName('foo') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['id'], 'idx_id'); $table->addIndex(['foo'], 'idx_foo'); @@ -881,19 +1055,31 @@ public function testTableComment(): void public function testUniqueConstraintWithEmptyName(): void { - $column1 = new Column('column1', Type::getType(Types::STRING)); - $column2 = new Column('column2', Type::getType(Types::STRING)); - $column3 = new Column('column3', Type::getType(Types::STRING)); - $column4 = new Column('column4', Type::getType(Types::STRING)); - - $columns = [$column1, $column2, $column3, $column4]; + $columns = [ + Column::editor() + ->setUnquotedName('column1') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('column2') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('column3') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('column4') + ->setTypeName(Types::STRING) + ->create(), + ]; $uniqueConstraints = [ UniqueConstraint::editor() - ->setColumnNames($column1->getObjectName(), $column2->getObjectName()) + ->setUnquotedColumnNames('column1', 'column2') ->create(), UniqueConstraint::editor() - ->setColumnNames($column3->getObjectName(), $column4->getObjectName()) + ->setUnquotedColumnNames('column3', 'column4') ->create(), ]; @@ -914,8 +1100,12 @@ public function testUniqueConstraintWithEmptyName(): void public function testDropUniqueConstraint(): void { - $table = new Table('foo'); - $table->addColumn('bar', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addUniqueConstraint(['bar'], 'unique_constraint'); $table->dropUniqueConstraint('unique_constraint'); @@ -927,16 +1117,24 @@ public function testDropUniqueConstraintUnknownNameThrowsException(): void { $this->expectException(SchemaException::class); - $table = new Table('foo'); - $table->addColumn('bar', Types::INTEGER); + $table = new Table('foo', [ + Column::editor() + ->setUnquotedName('bar') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->dropUniqueConstraint('unique_constraint'); } public function testDropColumnWithForeignKeyConstraint(): void { - $table = new Table('t1'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('t1', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addForeignKeyConstraint('t2', ['id'], ['id']); $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6559'); @@ -945,8 +1143,12 @@ public function testDropColumnWithForeignKeyConstraint(): void public function testDropColumnWithUniqueConstraint(): void { - $table = new Table('t'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('t', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addUniqueConstraint(['id']); $this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6559'); @@ -955,8 +1157,12 @@ public function testDropColumnWithUniqueConstraint(): void public function testDropColumnWithoutConstraints(): void { - $table = new Table('t'); - $table->addColumn('id', Types::INTEGER); + $table = new Table('t', [ + Column::editor() + ->setUnquotedName('id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6559'); $table->dropColumn('id'); @@ -991,8 +1197,12 @@ public function testGetQualifiedObjectName(): void public function testAddIndexWithNonIntegerColumnLength(): void { - $table = new Table('users'); - $table->addColumn('name', Types::STRING); + $table = new Table('users', [ + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::STRING) + ->create(), + ]); $this->expectException(InvalidIndexDefinition::class); $table->addIndex(['name'], null, [], ['lengths' => ['8']]); @@ -1000,8 +1210,12 @@ public function testAddIndexWithNonIntegerColumnLength(): void public function testAddIndexWithNonPositiveColumnLength(): void { - $table = new Table('users'); - $table->addColumn('name', Types::STRING); + $table = new Table('users', [ + Column::editor() + ->setUnquotedName('name') + ->setTypeName(Types::STRING) + ->create(), + ]); $this->expectException(InvalidIndexDefinition::class); $table->addIndex(['name'], null, [], ['lengths' => [-1]]); @@ -1009,9 +1223,16 @@ public function testAddIndexWithNonPositiveColumnLength(): void public function testAddIndexWithColumnLength(): void { - $table = new Table('users'); - $table->addColumn('first_name', Types::STRING); - $table->addColumn('last_name', Types::STRING); + $table = new Table('users', [ + Column::editor() + ->setUnquotedName('first_name') + ->setTypeName(Types::STRING) + ->create(), + Column::editor() + ->setUnquotedName('last_name') + ->setTypeName(Types::STRING) + ->create(), + ]); $table->addIndex(['first_name', 'last_name'], 'idx_user_name', [], ['lengths' => [16]]); $indexedColumns = $table->getIndex('idx_user_name')->getIndexedColumns(); @@ -1031,8 +1252,12 @@ public function testAddIndexWithColumnLength(): void #[TestWith([['spatial'], IndexType::SPATIAL])] public function testParseNonUniqueIndexType(array $flags, IndexType $expectedType): void { - $table = new Table('users'); - $table->addColumn('user_id', Types::INTEGER); + $table = new Table('users', [ + Column::editor() + ->setUnquotedName('user_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $table->addIndex(['user_id'], 'idx_user_id', $flags); $index = $table->getIndex('idx_user_id'); @@ -1041,8 +1266,12 @@ public function testParseNonUniqueIndexType(array $flags, IndexType $expectedTyp public function testAddIndexWithInvalidFlag(): void { - $table = new Table('users'); - $table->addColumn('user_id', Types::INTEGER); + $table = new Table('users', [ + Column::editor() + ->setUnquotedName('user_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->expectException(InvalidIndexDefinition::class); $table->addIndex(['user_id'], null, ['banana']); @@ -1050,8 +1279,12 @@ public function testAddIndexWithInvalidFlag(): void public function testAddIndexWithInvalidOption(): void { - $table = new Table('users'); - $table->addColumn('user_id', Types::INTEGER); + $table = new Table('users', [ + Column::editor() + ->setUnquotedName('user_id') + ->setTypeName(Types::INTEGER) + ->create(), + ]); $this->expectException(InvalidIndexDefinition::class); $table->addIndex(['user_id'], null, [], ['potato' => true]);