trait SchemaIntrospectionTestTrait

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Database/SchemaIntrospectionTestTrait.php \Drupal\Tests\Core\Database\SchemaIntrospectionTestTrait
  2. 10 core/tests/Drupal/Tests/Core/Database/SchemaIntrospectionTestTrait.php \Drupal\Tests\Core\Database\SchemaIntrospectionTestTrait
  3. 11.x core/tests/Drupal/Tests/Core/Database/SchemaIntrospectionTestTrait.php \Drupal\Tests\Core\Database\SchemaIntrospectionTestTrait

Provides methods for testing database schema characteristics.

Hierarchy

2 files declare their use of SchemaIntrospectionTestTrait
DriverSpecificSchemaTestBase.php in core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php
SchemaTest.php in core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php

File

core/tests/Drupal/Tests/Core/Database/SchemaIntrospectionTestTrait.php, line 8

Namespace

Drupal\Tests\Core\Database
View source
trait SchemaIntrospectionTestTrait {
    
    /**
     * Checks that an index covering exactly the given column names exists.
     *
     * @param string $table_name
     *   A non-prefixed table name.
     * @param array $column_names
     *   An array of column names
     * @param string $index_type
     *   (optional) The type of the index. Can be one of 'index', 'unique' or
     *   'primary'. Defaults to 'index'.
     */
    protected function assertIndexOnColumns($table_name, array $column_names, $index_type = 'index') {
        foreach ($this->getIndexColumnNames($table_name, $index_type) as $index_columns) {
            if ($column_names == $index_columns) {
                $this->assertTrue(TRUE);
                return;
            }
        }
        $this->assertTrue(FALSE);
    }
    
    /**
     * Checks that an index covering exactly the given column names doesn't exist.
     *
     * @param string $table_name
     *   A non-prefixed table name.
     * @param array $column_names
     *   An array of column names
     * @param string $index_type
     *   (optional) The type of the index. Can be one of 'index', 'unique' or
     *   'primary'. Defaults to 'index'.
     */
    protected function assertNoIndexOnColumns($table_name, array $column_names, $index_type = 'index') {
        foreach ($this->getIndexColumnNames($table_name, $index_type) as $index_columns) {
            if ($column_names == $index_columns) {
                $this->assertTrue(FALSE);
            }
        }
        $this->assertTrue(TRUE);
    }
    
    /**
     * Returns the column names used by the indexes of a table.
     *
     * @param string $table_name
     *   A table name.
     * @param string $index_type
     *   The type of the index. Can be one of 'index', 'unique' or 'primary'.
     *
     * @return array
     *   A multi-dimensional array containing the column names for each index of
     *   the given type.
     */
    protected function getIndexColumnNames($table_name, $index_type) {
        assert(in_array($index_type, [
            'index',
            'unique',
            'primary',
        ], TRUE));
        $schema = \Drupal::database()->schema();
        $introspect_index_schema = new \ReflectionMethod(get_class($schema), 'introspectIndexSchema');
        $introspect_index_schema->setAccessible(TRUE);
        $index_schema = $introspect_index_schema->invoke($schema, $table_name);
        // Filter the indexes by type.
        if ($index_type === 'primary') {
            $indexes = [
                $index_schema['primary key'],
            ];
        }
        elseif ($index_type === 'unique') {
            $indexes = array_values($index_schema['unique keys']);
        }
        else {
            $indexes = array_values($index_schema['indexes']);
        }
        return $indexes;
    }

}

Members

Title Sort descending Modifiers Object type Summary
SchemaIntrospectionTestTrait::assertIndexOnColumns protected function Checks that an index covering exactly the given column names exists.
SchemaIntrospectionTestTrait::assertNoIndexOnColumns protected function Checks that an index covering exactly the given column names doesn't exist.
SchemaIntrospectionTestTrait::getIndexColumnNames protected function Returns the column names used by the indexes of a table.

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