function SchemaTest::testFindTables

Tests the findTables() method.

File

core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php, line 1135

Class

SchemaTest
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testFindTables() {
  // We will be testing with three tables, two of them using the default
  // prefix and the third one with an individually specified prefix.
  // Set up a new connection with different connection info.
  $connection_info = Database::getConnectionInfo();
  // Add per-table prefix to the second table.
  $new_connection_info = $connection_info['default'];
  $new_connection_info['prefix'] = [
    'default' => $connection_info['default']['prefix'],
    'test_2_table' => $connection_info['default']['prefix'] . '_shared_',
  ];
  Database::addConnectionInfo('test', 'default', $new_connection_info);
  Database::setActiveConnection('test');
  $test_schema = Database::getConnection()->schema();
  // Create the tables.
  $table_specification = [
    'description' => 'Test table.',
    'fields' => [
      'id' => [
        'type' => 'int',
        'default' => NULL,
      ],
    ],
  ];
  $test_schema->createTable('test_1_table', $table_specification);
  $test_schema->createTable('test_2_table', $table_specification);
  $test_schema->createTable('the_third_table', $table_specification);
  // Check the "all tables" syntax.
  $tables = $test_schema->findTables('%');
  sort($tables);
  $expected = [
    // The 'config' table is added by
    // \Drupal\KernelTests\KernelTestBase::containerBuild().
'config',
    'test_1_table',
    // This table uses a per-table prefix, yet it is returned as un-prefixed.
'test_2_table',
    'the_third_table',
  ];
  $this->assertEquals($expected, $tables, 'All tables were found.');
  // Check the restrictive syntax.
  $tables = $test_schema->findTables('test_%');
  sort($tables);
  $expected = [
    'test_1_table',
    'test_2_table',
  ];
  $this->assertEquals($expected, $tables, 'Two tables were found.');
  // Check '_' and '%' wildcards.
  $test_schema->createTable('test3table', $table_specification);
  $test_schema->createTable('test4', $table_specification);
  $test_schema->createTable('testTable', $table_specification);
  $test_schema->createTable('test', $table_specification);
  $tables = $test_schema->findTables('test%');
  sort($tables);
  $expected = [
    'test',
    'test3table',
    'test4',
    'testTable',
    'test_1_table',
    'test_2_table',
  ];
  $this->assertEquals($expected, $tables, 'All "test" prefixed tables were found.');
  $tables = $test_schema->findTables('test_%');
  sort($tables);
  $expected = [
    'test3table',
    'test4',
    'testTable',
    'test_1_table',
    'test_2_table',
  ];
  $this->assertEquals($expected, $tables, 'All "/^test..*?/" tables were found.');
  $tables = $test_schema->findTables('test%table');
  sort($tables);
  $expected = [
    'test3table',
    'testTable',
    'test_1_table',
    'test_2_table',
  ];
  $this->assertEquals($expected, $tables, 'All "/^test.*?table/" tables were found.');
  $tables = $test_schema->findTables('test_%table');
  sort($tables);
  $expected = [
    'test3table',
    'test_1_table',
    'test_2_table',
  ];
  $this->assertEquals($expected, $tables, 'All "/^test..*?table/" tables were found.');
  $tables = $test_schema->findTables('test_');
  sort($tables);
  $expected = [
    'test4',
  ];
  $this->assertEquals($expected, $tables, 'All "/^test./" tables were found.');
  // Go back to the initial connection.
  Database::setActiveConnection('default');
}

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