You are here

public function LoggingTest::testEnableTargetLogging in Drupal 8

Same name and namespace in other branches
  1. 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 68

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
    ->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
  $this
    ->assertEqual($queries1[1]['target'], 'replica', 'Second query used replica target.');
}