function LoggingTest::testEnableMultiConnectionLogging

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php \Drupal\KernelTests\Core\Database\LoggingTest::testEnableMultiConnectionLogging()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php \Drupal\KernelTests\Core\Database\LoggingTest::testEnableMultiConnectionLogging()
  3. 11.x core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php \Drupal\KernelTests\Core\Database\LoggingTest::testEnableMultiConnectionLogging()

Tests that we can log queries separately on different connections.

File

core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php, line 113

Class

LoggingTest
Tests the query logging facility.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testEnableMultiConnectionLogging() : void {
    // Clone the primary credentials to a fake connection.
    // That both connections point to the same physical database is irrelevant.
    $connection_info = Database::getConnectionInfo('default');
    Database::addConnectionInfo('test2', 'default', $connection_info['default']);
    Database::startLog('testing1');
    Database::startLog('testing1', 'test2');
    $this->connection
        ->query('SELECT [name] FROM {test} WHERE [age] > :age', [
        ':age' => 25,
    ])
        ->fetchCol();
    $old_key = Database::setActiveConnection('test2');
    Database::getConnection('replica')->query('SELECT [age] FROM {test} WHERE [name] = :name', [
        ':name' => 'Ringo',
    ])
        ->fetchCol();
    Database::setActiveConnection($old_key);
    $queries1 = Database::getLog('testing1');
    $queries2 = Database::getLog('testing1', 'test2');
    $this->assertCount(1, $queries1, 'Correct number of queries recorded for first connection.');
    $this->assertCount(1, $queries2, 'Correct number of queries recorded for second connection.');
}

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