class SchemaDefinitionConversionTest

Tests conversion of SchemaDefinition objects to legacy array-based structure.

Attributes

#[Group('Database')] #[CoversClass(Table::class)] #[CoversClass(Schema::class)]

Hierarchy

Expanded class hierarchy of SchemaDefinitionConversionTest

File

core/tests/Drupal/Tests/Core/Database/SchemaDefinitionConversionTest.php, line 28

Namespace

Drupal\Tests\Core\Database
View source
class SchemaDefinitionConversionTest extends UnitTestCase {
  
  /**
   * Tests Table::toArray().
   */
  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());
  }
  
  /**
   * Tests Schema::toArray().
   */
  public function testConvertSchemaDefinition() : void {
    $arraySpecification = [
      'foo' => [
        'description' => 'Foo table for the database unit tests.',
        'fields' => [
          'id' => [
            'type' => 'serial',
            'unsigned' => TRUE,
            'not null' => TRUE,
          ],
        ],
        'primary key' => [
          'id',
        ],
      ],
      'bar' => [
        'description' => 'Bar table for the database unit tests.',
        'fields' => [
          'id' => [
            'type' => 'serial',
            'unsigned' => TRUE,
            'not null' => TRUE,
          ],
        ],
        'primary key' => [
          'id',
        ],
      ],
    ];
    $schemaDefinition = new Schema(type: SchemaDefinitionType::Test, name: 'test_foo_bar', tables: [
      new Table(name: 'foo', description: 'Foo table for the database unit tests.', columns: [
        Column::serial(name: 'id'),
      ], primaryKey: new PrimaryKey([
        'id',
      ])),
      new Table(name: 'bar', description: 'Bar table for the database unit tests.', columns: [
        Column::serial(name: 'id'),
      ], primaryKey: new PrimaryKey([
        'id',
      ])),
    ]);
    $this->assertEquals($arraySpecification, $schemaDefinition->toArray());
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalTestCaseTrait::$root protected property The Drupal root directory.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::checkLegacySymfonyDeprecationHelperEnvVariable public function Checks legacy SYMFONY_DEPRECATIONS_HELPER env variable is not used.
DrupalTestCaseTrait::expectExceptionMessageIs protected function Expects an exactly matching exception message.
DrupalTestCaseTrait::expectExceptionMessageIsOrContains protected function Expects an exception message containing a specified string.
DrupalTestCaseTrait::getDrupalRoot Deprecated protected static function Returns the Drupal root directory. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
DrupalTestCaseTrait::setUpRoot final protected function Ensure that the $root property is set initially.
ExpectDeprecationTrait::expectDeprecation Deprecated public function Adds an expected deprecation.
ExpectDeprecationTrait::regularExpressionForFormatDescription private function
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
SchemaDefinitionConversionTest::testConvertSchemaDefinition public function Tests Schema::toArray().
SchemaDefinitionConversionTest::testConvertTableDefinition public function Tests Table::toArray().
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 379
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.