protected function DatabaseTestsTrait::assertHasPrimaryKey in Search API 8
Asserts that the given table exists and has a primary key.
Parameters
string $table: The name of the table.
string|null $message: (optional) The message to print for the assertion, or NULL to use an automatically generated one.
2 calls to DatabaseTestsTrait::assertHasPrimaryKey()
- BackendTest::checkServerBackend in modules/
search_api_db/ tests/ src/ Kernel/ BackendTest.php - Tests that all tables and all columns have been created.
- SearchApiDbUpdate8102Test::testUpdate8102 in modules/
search_api_db/ src/ Tests/ Update/ SearchApiDbUpdate8102Test.php - Tests whether search_api_db_update_8102() works correctly.
File
- modules/
search_api_db/ src/ Tests/ DatabaseTestsTrait.php, line 21
Class
- DatabaseTestsTrait
- Provides some common helper methods for database tests.
Namespace
Drupal\search_api_db\TestsCode
protected function assertHasPrimaryKey($table, $message = NULL) {
$schema = \Drupal::database()
->schema();
$this
->assertTrue($schema
->tableExists($table), "Table {$table} exists.");
if (!$message) {
$message = "Table {$table} has a primary key.";
}
// The database layer doesn't support generic introspection into primary
// keys. The simplest way to test whether a primary key exists is therefore
// to try to create one and see whether that leads to an exception.
try {
$schema
->addPrimaryKey($table, []);
$this
->assertTrue(FALSE, $message);
} catch (SchemaObjectExistsException $e) {
$this
->assertTrue(TRUE, $message);
} catch (\Exception $e) {
// Trying to create a primary key with an empty fields list will probably
// still throw an exception, so we catch that as well.
$this
->assertTrue(FALSE, $message);
}
}