View source
<?php
namespace Drupal\KernelTests\Core\Database;
use Drupal\Core\Database\Log;
use Drupal\Core\Database\Database;
class LoggingTest extends DatabaseTestBase {
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();
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']);
}
}
public function testEnableMultiLogging() {
Database::startLog('testing1');
$this->connection
->query('SELECT [name] FROM {test} WHERE [age] > :age', [
':age' => 25,
])
->fetchCol();
Database::startLog('testing2');
$this->connection
->query('SELECT [age] FROM {test} WHERE [name] = :name', [
':name' => 'Ringo',
])
->fetchCol();
$queries1 = Database::getLog('testing1');
$queries2 = Database::getLog('testing2');
$this
->assertCount(2, $queries1, 'Correct number of queries recorded for log 1.');
$this
->assertCount(1, $queries2, 'Correct number of queries recorded for log 2.');
}
public function testEnableTargetLogging() {
$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
->assertEquals('default', $queries1[0]['target'], 'First query used default target.');
$this
->assertEquals('replica', $queries1[1]['target'], 'Second query used replica target.');
}
public function testEnableTargetLoggingNoTarget() {
Database::startLog('testing1');
$this->connection
->query('SELECT [name] FROM {test} WHERE [age] > :age', [
':age' => 25,
])
->fetchCol();
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.');
}
public function testEnableMultiConnectionLogging() {
$connection_info = Database::getConnectionInfo('default');
Database::addConnectionInfo('test2', 'default', $connection_info['default']);
Database::startLog('testing1');
Database::startLog('testing1', 'test2');
$this->connection
->query('SELECT [name] FROM {test} WHERE [age] > :age', [
':age' => 25,
])
->fetchCol();
$old_key = Database::setActiveConnection('test2');
Database::getConnection('replica')
->query('SELECT [age] FROM {test} WHERE [name] = :name', [
':name' => 'Ringo',
])
->fetchCol();
Database::setActiveConnection($old_key);
$queries1 = Database::getLog('testing1');
$queries2 = Database::getLog('testing1', 'test2');
$this
->assertCount(1, $queries1, 'Correct number of queries recorded for first connection.');
$this
->assertCount(1, $queries2, 'Correct number of queries recorded for second connection.');
}
public function testGetLoggingWrongKey() {
$result = Database::getLog('wrong');
$this
->assertEquals([], $result, 'The function getLog with a wrong key returns an empty array.');
}
public function testContribDriverLog($driver_namespace, $stack, array $expected_entry) {
$mock_builder = $this
->getMockBuilder(Log::class);
$log = $mock_builder
->setMethods([
'getDebugBacktrace',
])
->setConstructorArgs([
'test',
])
->getMock();
$log
->expects($this
->once())
->method('getDebugBacktrace')
->will($this
->returnValue($stack));
Database::addConnectionInfo('test', 'default', [
'driver' => 'mysql',
'namespace' => $driver_namespace,
]);
$result = $log
->findCaller($stack);
$this
->assertEquals($expected_entry, $result);
}
public function providerContribDriverLog() {
$stack = [
[
'file' => '/var/www/core/lib/Drupal/Core/Database/Log.php',
'line' => 125,
'function' => 'findCaller',
'class' => 'Drupal\\Core\\Database\\Log',
'object' => 'test',
'type' => '->',
'args' => [
0 => 'test',
],
],
[
'file' => '/var/www/libraries/drudbal/lib/Statement.php',
'line' => 264,
'function' => 'log',
'class' => 'Drupal\\Core\\Database\\Log',
'object' => 'test',
'type' => '->',
'args' => [
0 => 'test',
],
],
[
'file' => '/var/www/libraries/drudbal/lib/Connection.php',
'line' => 213,
'function' => 'execute',
'class' => 'Drupal\\Driver\\Database\\dbal\\Statement',
'object' => 'test',
'type' => '->',
'args' => [
0 => 'test',
],
],
[
'file' => '/var/www/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php',
'line' => 23,
'function' => 'query',
'class' => 'Drupal\\Driver\\Database\\dbal\\Connection',
'object' => 'test',
'type' => '->',
'args' => [
0 => 'test',
],
],
[
'file' => '/var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php',
'line' => 1154,
'function' => 'testEnableLogging',
'class' => 'Drupal\\KernelTests\\Core\\Database\\LoggingTest',
'object' => 'test',
'type' => '->',
'args' => [
0 => 'test',
],
],
[
'file' => '/var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php',
'line' => 842,
'function' => 'runTest',
'class' => 'PHPUnit\\Framework\\TestCase',
'object' => 'test',
'type' => '->',
'args' => [
0 => 'test',
],
],
[
'file' => '/var/www/vendor/phpunit/phpunit/src/Framework/TestResult.php',
'line' => 693,
'function' => 'runBare',
'class' => 'PHPUnit\\Framework\\TestCase',
'object' => 'test',
'type' => '->',
'args' => [
0 => 'test',
],
],
[
'file' => '/var/www/vendor/phpunit/phpunit/src/Framework/TestCase.php',
'line' => 796,
'function' => 'run',
'class' => 'PHPUnit\\Framework\\TestResult',
'object' => 'test',
'type' => '->',
'args' => [
0 => 'test',
],
],
[
'file' => 'Standard input code',
'line' => 57,
'function' => 'run',
'class' => 'PHPUnit\\Framework\\TestCase',
'object' => 'test',
'type' => '->',
'args' => [
0 => 'test',
],
],
[
'file' => 'Standard input code',
'line' => 111,
'function' => '__phpunit_run_isolated_test',
'args' => [
0 => 'test',
],
],
];
return [
'contrib driver namespace' => [
'Drupal\\Driver\\Database\\dbal',
$stack,
[
'class' => 'Drupal\\KernelTests\\Core\\Database\\LoggingTest',
'function' => 'testEnableLogging',
'file' => '/var/www/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php',
'line' => 23,
'type' => '->',
'args' => [
0 => 'test',
],
],
],
'missing driver namespace' => [
'Drupal\\Driver\\Database\\fake',
$stack,
[
'class' => 'Drupal\\Driver\\Database\\dbal\\Statement',
'function' => 'execute',
'file' => '/var/www/libraries/drudbal/lib/Statement.php',
'line' => 264,
'type' => '->',
'args' => [
0 => 'test',
],
],
],
];
}
}