View source
<?php
namespace Drupal\system\Tests\Database;
use Drupal\Core\Database\Database;
class LoggingTest extends DatabaseTestBase {
function testEnableLogging() {
Database::startLog('testing');
db_query('SELECT name FROM {test} WHERE age > :age', array(
':age' => 25,
))
->fetchCol();
db_query('SELECT age FROM {test} WHERE name = :name', array(
':name' => 'Ringo',
))
->fetchCol();
call_user_func_array('db_query', array(
'SELECT age FROM {test} WHERE name = :name',
array(
':name' => 'Ringo',
),
))
->fetchCol();
$queries = Database::getLog('testing', 'default');
$this
->assertEqual(count($queries), 3, 'Correct number of queries recorded.');
foreach ($queries as $query) {
$this
->assertEqual($query['caller']['function'], __FUNCTION__, 'Correct function in query log.');
}
}
function testEnableMultiLogging() {
Database::startLog('testing1');
db_query('SELECT name FROM {test} WHERE age > :age', array(
':age' => 25,
))
->fetchCol();
Database::startLog('testing2');
db_query('SELECT age FROM {test} WHERE name = :name', array(
':name' => 'Ringo',
))
->fetchCol();
$queries1 = Database::getLog('testing1');
$queries2 = Database::getLog('testing2');
$this
->assertEqual(count($queries1), 2, 'Correct number of queries recorded for log 1.');
$this
->assertEqual(count($queries2), 1, 'Correct number of queries recorded for log 2.');
}
function testEnableTargetLogging() {
$connection_info = Database::getConnectionInfo('default');
Database::addConnectionInfo('default', 'replica', $connection_info['default']);
Database::startLog('testing1');
db_query('SELECT name FROM {test} WHERE age > :age', array(
':age' => 25,
))
->fetchCol();
db_query('SELECT age FROM {test} WHERE name = :name', array(
':name' => 'Ringo',
), array(
'target' => 'replica',
));
$queries1 = Database::getLog('testing1');
$this
->assertEqual(count($queries1), 2, '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.');
}
function testEnableTargetLoggingNoTarget() {
Database::startLog('testing1');
db_query('SELECT name FROM {test} WHERE age > :age', array(
':age' => 25,
))
->fetchCol();
db_query('SELECT age FROM {test} WHERE name = :name', array(
':name' => 'Ringo',
), array(
'target' => 'fake',
))
->fetchCol();
$queries1 = Database::getLog('testing1');
$this
->assertEqual(count($queries1), 2, 'Recorded queries from all targets.');
$this
->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
$this
->assertEqual($queries1[1]['target'], 'default', 'Second query used default target as fallback.');
}
function testEnableMultiConnectionLogging() {
$connection_info = Database::getConnectionInfo('default');
Database::addConnectionInfo('test2', 'default', $connection_info['default']);
Database::startLog('testing1');
Database::startLog('testing1', 'test2');
db_query('SELECT name FROM {test} WHERE age > :age', array(
':age' => 25,
))
->fetchCol();
$old_key = db_set_active('test2');
db_query('SELECT age FROM {test} WHERE name = :name', array(
':name' => 'Ringo',
), array(
'target' => 'replica',
))
->fetchCol();
db_set_active($old_key);
$queries1 = Database::getLog('testing1');
$queries2 = Database::getLog('testing1', 'test2');
$this
->assertEqual(count($queries1), 1, 'Correct number of queries recorded for first connection.');
$this
->assertEqual(count($queries2), 1, 'Correct number of queries recorded for second connection.');
}
}