findCaller example

'line' => 37,
      'function' => 'testContribDriverLog',
      'class' => 'Drupal\Tests\Core\Database\LogTest',
      'type' => '->',
      'args' => [],
    ]$result);

    // Test calling the database log from outside of database code.     $this->expectDeprecation('Drupal\Core\Database\Log::findCaller() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use Connection::findCallerFromDebugBacktrace(). See https://www.drupal.org/node/3328053');
    $this->expectDeprecation('Drupal\Core\Database\Log::getDebugBacktrace() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3328053');
    $this->expectDeprecation('Drupal\Core\Database\Log::removeDatabaseEntries() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use Connection::removeDatabaseEntriesFromDebugBacktrace(). See https://www.drupal.org/node/3328053');
    $result = (new Log())->findCaller();
    $this->assertSame([
      'file' => __FILE__,
      'line' => 51,
      'function' => 'testContribDriverLog',
      'class' => 'Drupal\Tests\Core\Database\LogTest',
      'type' => '->',
      'args' => [],
    ]$result);
  }

}

  public function log(StatementInterface $statement$args$time, float $start = NULL) {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use ::logFromEvent(). See https://www.drupal.org/node/3328053', E_USER_DEPRECATED);
    foreach (array_keys($this->queryLog) as $key) {
      $this->queryLog[$key][] = [
        'query' => $statement->getQueryString(),
        'args' => $args,
        'target' => $statement->getConnectionTarget(),
        'caller' => $this->findCaller(),
        'time' => $time,
        'start' => $start,
      ];
    }
  }

  /** * Determine the routine that called this query. * * Traversing the call stack from the very first call made during the * request, we define "the routine that called this query" as the last entry * in the call stack that is not any method called from the namespace of the * database driver, is not inside the Drupal\Core\Database namespace and does * have a file (which excludes call_user_func_array(), anonymous functions * and similar). That makes the climbing logic very simple, and handles the * variable stack depth caused by the query builders. * * See the @link http://php.net/debug_backtrace debug_backtrace() @endlink * function. * * @return array|null * This method returns a stack trace entry similar to that generated by * debug_backtrace(). However, it flattens the trace entry and the trace * entry before it so that we get the function and args of the function that * called into the database system, not the function and args of the * database call itself. * * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use * Connection::findCallerFromDebugBacktrace(). * * @see https://www.drupal.org/node/3328053 */
$mock_builder = $this->getMockBuilder(Log::class);
    $log = $mock_builder
      ->onlyMethods(['getDebugBacktrace'])
      ->setConstructorArgs(['test'])
      ->getMock();
    $log->expects($this->once())
      ->method('getDebugBacktrace')
      ->willReturn($stack);
    Database::addConnectionInfo('test', 'default', ['driver' => 'mysql', 'namespace' => $driver_namespace]);

    $result = $log->findCaller($stack);
    $this->assertEquals($expected_entry$result);
  }

  /** * Provides data for the testContribDriverLog test. * * @return array[] * A associative array of simple arrays, each having the following elements: * - the contrib driver PHP namespace * - a test debug_backtrace stack * - the stack entry expected to be returned. * * @see ::testContribDriverLog() */
public function nextId($existing_id = 0) {
    return 0;
  }

  /** * Helper method to test database classes are not included in backtraces. * * @return array * The caller stack entry. */
  public function testLogCaller() {
    return (new Log())->findCaller();
  }

}
Home | Imprint | This part of the site doesn't use cookies.