Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Utility / ErrorTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Utility;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Core\Utility\Error;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Utility\Error
10  * @group Utility
11  */
12 class ErrorTest extends UnitTestCase {
13
14   /**
15    * Tests the getLastCaller() method.
16    *
17    * @param array $backtrace
18    *   The test backtrace array.
19    * @param array $expected
20    *   The expected return array.
21    *
22    * @dataProvider providerTestGetLastCaller
23    */
24   public function testGetLastCaller($backtrace, $expected) {
25     $this->assertSame($expected, Error::getLastCaller($backtrace));
26   }
27
28   /**
29    * Data provider for testGetLastCaller.
30    *
31    * @return array
32    *   An array of parameter data.
33    */
34   public function providerTestGetLastCaller() {
35     $data = [];
36
37     // Test with just one item. This should default to the function being
38     // main().
39     $single_item = [$this->createBacktraceItem()];
40     $data[] = [$single_item, $this->createBacktraceItem('main()')];
41
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()')];
46
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()')];
51
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()')];
58     }
59
60     return $data;
61   }
62
63   /**
64    * Tests the formatBacktrace() method.
65    *
66    * @param array $backtrace
67    *   The test backtrace array.
68    * @param array $expected
69    *   The expected return array.
70    *
71    * @dataProvider providerTestFormatBacktrace
72    */
73   public function testFormatBacktrace($backtrace, $expected) {
74     $this->assertSame($expected, Error::formatBacktrace($backtrace));
75   }
76
77   /**
78    * Data provider for testFormatBacktrace.
79    *
80    * @return array
81    */
82   public function providerTestFormatBacktrace() {
83     $data = [];
84
85     // Test with no function, main should be in the backtrace.
86     $data[] = [[$this->createBacktraceItem(NULL, NULL)], "main() (Line: 10)\n"];
87
88     $base = [$this->createBacktraceItem()];
89     $data[] = [$base, "test_function() (Line: 10)\n"];
90
91     // Add a second item.
92     $second_item = $base;
93     $second_item[] = $this->createBacktraceItem('test_function_2');
94
95     $data[] = [$second_item, "test_function() (Line: 10)\ntest_function_2() (Line: 10)\n"];
96
97     // Add a second item, with a class.
98     $second_item_class = $base;
99     $second_item_class[] = $this->createBacktraceItem('test_function_2', 'TestClass');
100
101     $data[] = [$second_item_class, "test_function() (Line: 10)\nTestClass->test_function_2() (Line: 10)\n"];
102
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()]);
106
107     $data[] = [$second_item_args, "test_function() (Line: 10)\ntest_function_2('string', 10, Object) (Line: 10)\n"];
108
109     return $data;
110   }
111
112   /**
113    * Creates a mock backtrace item.
114    *
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.
119    * @param array $args
120    *   (optional) An array of function arguments to add to the backtrace item.
121    * @param int $line
122    *   (optional) The line where the function was called.
123    *
124    * @return array
125    *   A backtrace array item.
126    */
127   protected function createBacktraceItem($function = 'test_function', $class = NULL, array $args = [], $line = 10) {
128     $backtrace = [
129       'file' => 'test_file',
130       'line' => $line,
131       'function' => $function,
132       'args' => [],
133     ];
134
135     if (isset($class)) {
136       $backtrace['class'] = $class;
137       $backtrace['type'] = '->';
138     }
139
140     if (!empty($args)) {
141       $backtrace['args'] = $args;
142     }
143
144     return $backtrace;
145   }
146
147 }