function LoggingTest::testEnableLogging

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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']);
    }
}

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