You are here

public function LoggingTest::testEnableTargetLogging in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php \Drupal\KernelTests\Core\Database\LoggingTest::testEnableTargetLogging()
  2. 9 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 63

Class

LoggingTest
Tests the query logging facility.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testEnableTargetLogging() {

  // 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.');
}