function DriverSpecificSchemaTestBase::testFindTables

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php \Drupal\KernelTests\Core\Database\DriverSpecificSchemaTestBase::testFindTables()

Tests the findTables() method.

File

core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php, line 1026

Class

DriverSpecificSchemaTestBase
Tests table creation and modification via the schema API.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testFindTables() : void {
  // We will be testing with three tables.
  $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.');
}

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