ErrorTest.php 4.42 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

namespace Drupal\Tests\Core\Utility;

use Drupal\Tests\UnitTestCase;
use Drupal\Core\Utility\Error;

/**
9 10
 * @coversDefaultClass \Drupal\Core\Utility\Error
 * @group Utility
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
 */
class ErrorTest extends UnitTestCase {

  /**
   * 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 = array();

    // Test with just one item. This should default to the function being
    // main().
    $single_item = array($this->createBacktraceItem());
    $data[] = array($single_item, $this->createBacktraceItem('main()'));

    // Add a second item, without a class.
    $two_items = $single_item;
    $two_items[] = $this->createBacktraceItem('test_function_two');
    $data[] = array($two_items, $this->createBacktraceItem('test_function_two()'));

    // Add a second item, with a class.
    $two_items = $single_item;
    $two_items[] = $this->createBacktraceItem('test_function_two', 'TestClass');
    $data[] = array($two_items, $this->createBacktraceItem('TestClass->test_function_two()'));

    // Add blacklist functions to backtrace. They should get removed.
    foreach (array('debug', '_drupal_error_handler', '_drupal_exception_handler') as $function) {
      $two_items = $single_item;
      // Push to the start of the backtrace.
      array_unshift($two_items, $this->createBacktraceItem($function));
      $data[] = array($single_item, $this->createBacktraceItem('main()'));
    }

    return $data;
  }

  /**
   * Tests the formatBacktrace() method.
   *
   * @param array $backtrace
   *   The test backtrace array.
   * @param array $expected
   *   The expected return array.
   *
   * @dataProvider providerTestFormatBacktrace
   */
  public function testFormatBacktrace($backtrace, $expected) {
    $this->assertSame($expected, Error::formatBacktrace($backtrace));
  }

  /**
   * Data provider for testFormatBacktrace.
   *
   * @return array
   */
  public function providerTestFormatBacktrace() {
    $data = array();

    // Test with no function, main should be in the backtrace.
87
    $data[] = array(array($this->createBacktraceItem(NULL, NULL)), "main() (Line: 10:test_file)\n");
88 89

    $base = array($this->createBacktraceItem());
90
    $data[] = array($base, "test_function() (Line: 10:test_file)\n");
91 92 93 94 95

    // Add a second item.
    $second_item = $base;
    $second_item[] = $this->createBacktraceItem('test_function_2');

96
    $data[] = array($second_item, "test_function() (Line: 10:test_file)\ntest_function_2() (Line: 10:test_file)\n");
97 98 99 100 101

    // Add a second item, with a class.
    $second_item_class = $base;
    $second_item_class[] = $this->createBacktraceItem('test_function_2', 'TestClass');

102
    $data[] = array($second_item_class, "test_function() (Line: 10:test_file)\nTestClass->test_function_2() (Line: 10:test_file)\n");
103 104 105 106 107

    // Add a second item, with a class.
    $second_item_args = $base;
    $second_item_args[] = $this->createBacktraceItem('test_function_2', NULL, array('string', 10, new \stdClass()));

108
    $data[] = array($second_item_args, "test_function() (Line: 10:test_file)\ntest_function_2('string', 10, Object) (Line: 10:test_file)\n");
109 110 111 112 113 114 115 116 117 118 119 120 121

    return $data;
  }

  /**
   * Creates a mock backtrace item.
   *
   * @param string|NULL $function
   *   (optional) The function name to use in the backtrace item.
   * @param string $class
   *   (optional) The class to use in the backtrace item.
   * @param array $args
   *   (optional) An array of function arguments to add to the backtrace item.
122 123
   * @param int $line
   *   (optional) The line where the function was called.
124 125 126 127
   *
   * @return array
   *   A backtrace array item.
   */
128
  protected function createBacktraceItem($function = 'test_function', $class = NULL, array $args = array(), $line = 10) {
129 130
    $backtrace = array(
      'file' => 'test_file',
131
      'line' => $line,
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
      'function' => $function,
      'args' => array(),
    );

    if (isset($class)) {
      $backtrace['class'] = $class;
      $backtrace['type'] = '->';
    }

    if (!empty($args)) {
      $backtrace['args'] = $args;
    }

    return $backtrace;
  }

}