getLastCaller example



  /** * Retrieves the current calling line in the class under test. * * @return array * An associative array with keys 'file', 'line' and 'function'. */
  protected function getTestMethodCaller() {
    $backtrace = debug_backtrace();
    // Find the test class that has the test method.     while ($caller = Error::getLastCaller($backtrace)) {
      // If we match PHPUnit's TestCase::runTest, then the previously processed       // caller entry is where our test method sits.       if (isset($last_caller) && isset($caller['function']) && $caller['function'] === 'PHPUnit\Framework\TestCase->runTest()') {
        // Return the last caller since that has to be the test class.         $caller = $last_caller;
        break;
      }

      // If the test method is implemented by a test class's parent then the       // class name of $this will not be part of the backtrace.       // In that case we process the backtrace until the caller is not a
/** * Tests the getLastCaller() method. * * @param array $backtrace * The test backtrace array. * @param array $expected * The expected return array. * * @dataProvider providerTestGetLastCaller */
  public function testGetLastCaller($backtrace$expected) {
    $this->assertSame($expected, Error::getLastCaller($backtrace));
  }

  /** * Data provider for testGetLastCaller. * * @return array * An array of parameter data. */
  public function providerTestGetLastCaller() {
    $data = [];

    


  /** * Retrieves the current calling line in the class under test. * * @return array * An associative array with keys 'file', 'line' and 'function'. */
  protected function getTestMethodCaller() {
    $backtrace = debug_backtrace();
    // Find the test class that has the test method.     while ($caller = Error::getLastCaller($backtrace)) {
      if (isset($caller['class']) && $caller['class'] === static::class) {
        break;
      }
      // If the test method is implemented by a test class's parent then the       // class name of $this will not be part of the backtrace.       // In that case we process the backtrace until the caller is not a       // subclass of $this and return the previous caller.       if (isset($last_caller) && (!isset($caller['class']) || !is_subclass_of($this$caller['class']))) {
        // Return the last caller since that has to be the test class.         $caller = $last_caller;
        break;
      }
// For PDOException errors, we try to return the initial caller,     // skipping internal functions of the database layer.     if ($exception instanceof \PDOException || $exception instanceof DatabaseExceptionWrapper) {
      $driver_namespace = Database::getConnectionInfo()['default']['namespace'];
      $backtrace = Connection::removeDatabaseEntriesFromDebugBacktrace($backtrace$driver_namespace);
      if (isset($exception->query_string, $exception->args)) {
        $message .= ": " . $exception->query_string . "; " . print_r($exception->args, TRUE);
      }
    }

    $caller = static::getLastCaller($backtrace);

    return [
      '%type' => get_class($exception),
      // The standard PHP exception handler considers that the exception message       // is plain-text. We mimic this behavior here.       '@message' => $message,
      '%function' => $caller['function'],
      '%file' => $caller['file'],
      '%line' => $caller['line'],
      'severity_level' => static::ERROR,
      'backtrace' => $backtrace,
      
Home | Imprint | This part of the site doesn't use cookies.