1 | <?php |
||
15 | class Test extends AbstractTest |
||
16 | { |
||
17 | /** |
||
18 | * @param string $description |
||
19 | * @param callable $definition |
||
20 | */ |
||
21 | public function __construct($description, callable $definition = null) |
||
31 | |||
32 | /** |
||
33 | * Execute the test along with any setup and tear down functions. |
||
34 | * |
||
35 | * @param TestResult $result |
||
36 | * @return void |
||
37 | */ |
||
38 | public function run(TestResult $result) |
||
49 | |||
50 | /** |
||
51 | * Attempt to execute setup functions and run the test definition |
||
52 | * |
||
53 | * @param TestResult $result |
||
54 | */ |
||
55 | protected function executeTest(TestResult $result) |
||
56 | { |
||
57 | $action = ['passTest', $this]; |
||
58 | $handler = $this->handleErrors($result, $action); |
||
59 | try { |
||
60 | $this->runSetup(); |
||
61 | call_user_func_array($this->getDefinition(), $this->getDefinitionArguments()); |
||
62 | } catch (Throwable $e) { |
||
63 | $this->failIfPassing($action, $e); |
||
64 | } catch (Exception $e) { |
||
65 | $this->failIfPassing($action, $e); |
||
66 | } |
||
67 | $this->runTearDown($result, $action); |
||
68 | $this->restoreErrorHandler($handler); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Excecute the test's setup functions |
||
73 | */ |
||
74 | protected function runSetup() |
||
83 | |||
84 | /** |
||
85 | * Run the tests tear down methods and have the result |
||
86 | * perform the method indicated by $action |
||
87 | * |
||
88 | * @param TestResult $result |
||
89 | * @param array $action |
||
90 | */ |
||
91 | protected function runTearDown(TestResult $result, array $action) |
||
92 | { |
||
93 | $this->forEachNodeBottomUp(function (TestInterface $test) use ($result, &$action) { |
||
94 | $tearDowns = $test->getTearDownFunctions(); |
||
95 | foreach ($tearDowns as $tearDown) { |
||
96 | try { |
||
97 | $tearDown(); |
||
98 | } catch (Throwable $e) { |
||
99 | $this->failIfPassing($action, $e); |
||
100 | } catch (Exception $e) { |
||
101 | $this->failIfPassing($action, $e); |
||
102 | } |
||
103 | } |
||
104 | }); |
||
105 | call_user_func_array([$result, $action[0]], array_slice($action, 1)); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Set an error handler to handle errors within the test |
||
110 | * |
||
111 | * @param TestResult $result |
||
112 | * @param array &$action |
||
113 | * |
||
114 | * @return callable|null |
||
115 | */ |
||
116 | protected function handleErrors(TestResult $result, array &$action) |
||
117 | { |
||
118 | $handler = null; |
||
119 | $handler = set_error_handler(function ($severity, $message, $path, $line) use ($result, &$action, &$handler) { |
||
120 | // if there is an existing error handler, call it and record the result |
||
121 | $isHandled = $handler && false !== $handler($severity, $message, $path, $line); |
||
122 | |||
123 | if (!$isHandled) { |
||
124 | $result->getEventEmitter()->emit('error', [$severity, $message, $path, $line]); |
||
125 | |||
126 | // honor the error reporting configuration - this also takes care of the error control operator (@) |
||
127 | $errorReporting = error_reporting(); |
||
128 | $shouldHandle = $severity === ($severity & $errorReporting); |
||
129 | |||
130 | if ($shouldHandle) { |
||
131 | $this->failIfPassing($action, new ErrorException($message, 0, $severity, $path, $line)); |
||
132 | } |
||
133 | } |
||
134 | }); |
||
135 | |||
136 | return $handler; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Restore the previous error handler |
||
141 | * |
||
142 | * @param callable|null $handler |
||
143 | */ |
||
144 | protected function restoreErrorHandler($handler) |
||
153 | |||
154 | /** |
||
155 | * Fail the test, but do not overwrite existing failures |
||
156 | * |
||
157 | * @param array &$action |
||
158 | * @param mixed $error |
||
159 | */ |
||
160 | protected function failIfPassing(array &$action, $error) |
||
166 | } |
||
167 |