class SchemaInspector

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/TestTools/Extension/SchemaInspector.php \Drupal\TestTools\Extension\SchemaInspector
  2. 10 core/tests/Drupal/TestTools/Extension/SchemaInspector.php \Drupal\TestTools\Extension\SchemaInspector
  3. 9 core/tests/Drupal/TestTools/Extension/SchemaInspector.php \Drupal\TestTools\Extension\SchemaInspector

Provides methods to access modules' schema.

Hierarchy

Expanded class hierarchy of SchemaInspector

2 files declare their use of SchemaInspector
KernelTestBase.php in core/tests/Drupal/KernelTests/KernelTestBase.php
ModuleTestBase.php in core/modules/system/tests/src/Functional/Module/ModuleTestBase.php

File

core/tests/Drupal/TestTools/Extension/SchemaInspector.php, line 14

Namespace

Drupal\TestTools\Extension
View source
class SchemaInspector {
  
  /**
   * Returns the module's schema specification.
   *
   * This function can be used to retrieve a schema specification provided by
   * hook_schema(), so it allows you to derive your tables from existing
   * specifications.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $handler
   *   The module handler to use for calling schema hook.
   * @param string $module
   *   The module to which the table belongs.
   *
   * @return \Drupal\Core\Database\SchemaDefinition\Schema|array
   *   An array of schema definition provided by hook_schema().
   *
   * @see \hook_schema()
   */
  public static function getTablesSpecification(ModuleHandlerInterface $handler, string $module) : Schema|array {
    if ($handler->loadInclude($module, 'install')) {
      $schema = $handler->invoke($module, 'schema') ?? [];
      if ($schema instanceof Schema) {
        assert($schema->type === SchemaDefinitionType::Module, 'Invalid schema definition type, must be SchemaDefinitionType::Module');
        assert($schema->name === $module, 'Invalid schema definition name, must be equal to the module name');
      }
      return $schema;
    }
    return [];
  }
  
  /**
   * Returns the module's schema table names.
   *
   * This function can be used to retrieve a schema specification provided by
   * hook_schema(), so it allows you to derive your tables from existing
   * specifications.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $handler
   *   The module handler to use for calling schema hook.
   * @param string $module
   *   The module to which the tables belong.
   *
   * @return list<string>
   *   A list of table names.
   *
   * @see \hook_schema()
   */
  public static function getTableNames(ModuleHandlerInterface $handler, string $module) : array {
    $schema = self::getTablesSpecification($handler, $module);
    return $schema instanceof Schema ? $schema->tableNames() : array_keys($schema);
  }

}

Members

Title Sort descending Modifiers Object type Summary
SchemaInspector::getTableNames public static function Returns the module&#039;s schema table names.
SchemaInspector::getTablesSpecification public static function Returns the module&#039;s schema specification.

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