function LoggingTest::testEnableTargetLogging

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

Tests logging queries against multiple targets on the same connection.

File

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

Class

LoggingTest
Tests the query logging facility.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testEnableTargetLogging() : void {
    // Clone the primary credentials to a replica connection and to another fake
    // connection.
    $connection_info = Database::getConnectionInfo('default');
    Database::addConnectionInfo('default', 'replica', $connection_info['default']);
    Database::startLog('testing1');
    $this->connection
        ->query('SELECT [name] FROM {test} WHERE [age] > :age', [
        ':age' => 25,
    ])
        ->fetchCol();
    Database::getConnection('replica')->query('SELECT [age] FROM {test} WHERE [name] = :name', [
        ':name' => 'Ringo',
    ])
        ->fetchCol();
    $queries1 = Database::getLog('testing1');
    $this->assertCount(2, $queries1, 'Recorded queries from all targets.');
    $this->assertEquals('default', $queries1[0]['target'], 'First query used default target.');
    $this->assertEquals('replica', $queries1[1]['target'], 'Second query used replica target.');
}

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