3 namespace Drupal\Tests\Core\Utility;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Core\Utility\Error;
9 * @coversDefaultClass \Drupal\Core\Utility\Error
12 class ErrorTest extends UnitTestCase {
15 * Tests the getLastCaller() method.
17 * @param array $backtrace
18 * The test backtrace array.
19 * @param array $expected
20 * The expected return array.
22 * @dataProvider providerTestGetLastCaller
24 public function testGetLastCaller($backtrace, $expected) {
25 $this->assertSame($expected, Error::getLastCaller($backtrace));
29 * Data provider for testGetLastCaller.
32 * An array of parameter data.
34 public function providerTestGetLastCaller() {
37 // Test with just one item. This should default to the function being
39 $single_item = [$this->createBacktraceItem()];
40 $data[] = [$single_item, $this->createBacktraceItem('main()')];
42 // Add a second item, without a class.
43 $two_items = $single_item;
44 $two_items[] = $this->createBacktraceItem('test_function_two');
45 $data[] = [$two_items, $this->createBacktraceItem('test_function_two()')];
47 // Add a second item, with a class.
48 $two_items = $single_item;
49 $two_items[] = $this->createBacktraceItem('test_function_two', 'TestClass');
50 $data[] = [$two_items, $this->createBacktraceItem('TestClass->test_function_two()')];
52 // Add blacklist functions to backtrace. They should get removed.
53 foreach (['debug', '_drupal_error_handler', '_drupal_exception_handler'] as $function) {
54 $two_items = $single_item;
55 // Push to the start of the backtrace.
56 array_unshift($two_items, $this->createBacktraceItem($function));
57 $data[] = [$single_item, $this->createBacktraceItem('main()')];
64 * Tests the formatBacktrace() method.
66 * @param array $backtrace
67 * The test backtrace array.
68 * @param array $expected
69 * The expected return array.
71 * @dataProvider providerTestFormatBacktrace
73 public function testFormatBacktrace($backtrace, $expected) {
74 $this->assertSame($expected, Error::formatBacktrace($backtrace));
78 * Data provider for testFormatBacktrace.
82 public function providerTestFormatBacktrace() {
85 // Test with no function, main should be in the backtrace.
86 $data[] = [[$this->createBacktraceItem(NULL, NULL)], "main() (Line: 10)\n"];
88 $base = [$this->createBacktraceItem()];
89 $data[] = [$base, "test_function() (Line: 10)\n"];
93 $second_item[] = $this->createBacktraceItem('test_function_2');
95 $data[] = [$second_item, "test_function() (Line: 10)\ntest_function_2() (Line: 10)\n"];
97 // Add a second item, with a class.
98 $second_item_class = $base;
99 $second_item_class[] = $this->createBacktraceItem('test_function_2', 'TestClass');
101 $data[] = [$second_item_class, "test_function() (Line: 10)\nTestClass->test_function_2() (Line: 10)\n"];
103 // Add a second item, with a class.
104 $second_item_args = $base;
105 $second_item_args[] = $this->createBacktraceItem('test_function_2', NULL, ['string', 10, new \stdClass()]);
107 $data[] = [$second_item_args, "test_function() (Line: 10)\ntest_function_2('string', 10, Object) (Line: 10)\n"];
113 * Creates a mock backtrace item.
115 * @param string|null $function
116 * (optional) The function name to use in the backtrace item.
117 * @param string $class
118 * (optional) The class to use in the backtrace item.
120 * (optional) An array of function arguments to add to the backtrace item.
122 * (optional) The line where the function was called.
125 * A backtrace array item.
127 protected function createBacktraceItem($function = 'test_function', $class = NULL, array $args = [], $line = 10) {
129 'file' => 'test_file',
131 'function' => $function,
136 $backtrace['class'] = $class;
137 $backtrace['type'] = '->';
141 $backtrace['args'] = $args;