function LoggingTest::testEnableTargetLoggingNoTarget

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

Tests that logs to separate targets use the same connection properly.

This test is identical to the one above, except that it doesn't create a fake target so the query should fall back to running on the default target.

File

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

Class

LoggingTest
Tests the query logging facility.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testEnableTargetLoggingNoTarget() {
    Database::startLog('testing1');
    $this->connection
        ->query('SELECT [name] FROM {test} WHERE [age] > :age', [
        ':age' => 25,
    ])
        ->fetchCol();
    // We use "fake" here as a target because any non-existent target will do.
    // However, because all of the tests in this class share a single page
    // request there is likely to be a target of "replica" from one of the other
    // unit tests, so we use a target here that we know with absolute certainty
    // does not exist.
    Database::getConnection('fake')->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('default', $queries1[1]['target'], 'Second query used default target as fallback.');
}

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