function SchemaDefinitionConversionTest::testConvertTableDefinition
Tests Table::toArray().
File
-
core/
tests/ Drupal/ Tests/ Core/ Database/ SchemaDefinitionConversionTest.php, line 36
Class
- SchemaDefinitionConversionTest
- Tests conversion of SchemaDefinition objects to legacy array-based structure.
Namespace
Drupal\Tests\Core\DatabaseCode
public function testConvertTableDefinition() : void {
$arraySpecification = [
'description' => 'Basic test table for the database unit tests.',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'name' => [
'description' => "A person's name",
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
'binary' => TRUE,
],
'age' => [
'description' => "The person's age",
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'job' => [
'description' => "The person's job",
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => 'Undefined',
],
'db_timestamp' => [
'description' => "The database timestamp",
'mysql_type' => 'timestamp',
'pgsql_type' => 'timestamp',
'sqlite_type' => 'datetime',
'not null' => FALSE,
'default' => NULL,
],
],
'primary key' => [
'id',
],
'unique keys' => [
'name' => [
'name',
],
],
'indexes' => [
'ages' => [
'age',
],
'age_job_prefix' => [
'age',
[
'job',
50,
],
],
'age_name_prefix' => [
'age',
[
'name',
20,
],
],
'foo' => [
'job',
],
],
'foreign keys' => [
'user_id' => [
'table' => 'test_users',
'columns' => [
'id' => 'id',
],
],
],
'mysql_engine' => 'InnoDB',
'mysql_character_set' => 'utf8mb4',
];
$schemaDefinition = new Table(name: 'test', description: 'Basic test table for the database unit tests.', columns: [
Column::serial(name: 'id'),
Column::varcharAscii(name: 'name', description: "A person's name", length: 255, notNull: TRUE, default: new StringValue(''), binary: TRUE),
Column::int(name: 'age', description: "The person's age", size: ColumnSize::Small, unsigned: TRUE, notNull: TRUE, default: new IntValue(0)),
Column::varchar(name: 'job', description: "The person's job", length: 255, notNull: TRUE, default: new StringValue('Undefined')),
Column::create(name: 'db_timestamp', description: "The database timestamp", dbSpecificExtra: [
'mysql' => [
'mysql_type' => 'timestamp',
],
'pgsql' => [
'pgsql_type' => 'timestamp',
],
'sqlite' => [
'sqlite_type' => 'datetime',
],
], notNull: FALSE, default: new NullValue()),
], primaryKey: new PrimaryKey([
'id',
]), uniqueKeys: [
new UniqueKey(name: 'name', columns: [
'name',
]),
], indexes: [
new Index(name: 'ages', columns: [
'age',
]),
new Index(name: 'age_job_prefix', columns: [
'age',
[
'job',
50,
],
]),
new Index(name: 'age_name_prefix', columns: [
'age',
new KeyColumn(name: 'name', length: 20),
]),
new Index(name: 'foo', columns: [
'job',
]),
], foreignKeys: [
new ForeignKey(name: 'user_id', foreignTable: 'test_users', columns: [
'id',
], foreignColumns: [
'id',
]),
], dbSpecificExtra: [
'mysql' => [
'mysql_engine' => 'InnoDB',
'mysql_character_set' => 'utf8mb4',
],
]);
$this->assertEquals($arraySpecification, $schemaDefinition->toArray());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.