You are here

public function LoggingTest::testEnableLogging in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php \Drupal\KernelTests\Core\Database\LoggingTest::testEnableLogging()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php \Drupal\KernelTests\Core\Database\LoggingTest::testEnableLogging()

Tests that we can log the existence of a query.

File

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

Class

LoggingTest
Tests the query logging facility.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testEnableLogging() {
  Database::startLog('testing');
  $start = microtime(TRUE);
  $this->connection
    ->query('SELECT [name] FROM {test} WHERE [age] > :age', [
    ':age' => 25,
  ])
    ->fetchCol();
  $this->connection
    ->query('SELECT [age] FROM {test} WHERE [name] = :name', [
    ':name' => 'Ringo',
  ])
    ->fetchCol();

  // Trigger a call that does not have file in the backtrace.
  call_user_func_array([
    Database::getConnection(),
    'query',
  ], [
    'SELECT [age] FROM {test} WHERE [name] = :name',
    [
      ':name' => 'Ringo',
    ],
  ])
    ->fetchCol();
  $queries = Database::getLog('testing', 'default');
  $this
    ->assertCount(3, $queries, 'Correct number of queries recorded.');
  foreach ($queries as $query) {
    $this
      ->assertEquals(__FUNCTION__, $query['caller']['function'], 'Correct function in query log.');
    $this
      ->assertIsFloat($query['start']);
    $this
      ->assertGreaterThanOrEqual($start, $query['start']);
  }
}