1 | <?php |
||
14 | class Test extends AbstractTest |
||
15 | { |
||
16 | /** |
||
17 | * @param string $description |
||
18 | * @param callable $definition |
||
19 | */ |
||
20 | public function __construct($description, callable $definition = null) |
||
30 | |||
31 | /** |
||
32 | * Execute the test along with any setup and tear down functions. |
||
33 | * |
||
34 | * @param TestResult $result |
||
35 | * @return void |
||
36 | */ |
||
37 | public function run(TestResult $result) |
||
48 | |||
49 | /** |
||
50 | * Attempt to execute setup functions and run the test definition |
||
51 | * |
||
52 | * @param TestResult $result |
||
53 | */ |
||
54 | protected function executeTest(TestResult $result) |
||
55 | { |
||
56 | $action = ['passTest', $this]; |
||
57 | $handler = $this->handleErrors($result, $action); |
||
58 | try { |
||
59 | $this->runSetup(); |
||
60 | call_user_func_array($this->getDefinition(), $this->getDefinitionArguments()); |
||
61 | } catch (Throwable $e) { |
||
|
|||
62 | if ('passTest' === $action[0]) { |
||
63 | $action = ['failTest', $this, $e]; |
||
64 | } |
||
65 | } catch (Exception $e) { |
||
66 | if ('passTest' === $action[0]) { |
||
67 | $action = ['failTest', $this, $e]; |
||
68 | } |
||
69 | } |
||
70 | $this->runTearDown($result, $action); |
||
71 | $this->restoreErrorHandler($handler); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Excecute the test's setup functions |
||
76 | */ |
||
77 | protected function runSetup() |
||
86 | |||
87 | /** |
||
88 | * Run the tests tear down methods and have the result |
||
89 | * perform the method indicated by $action |
||
90 | * |
||
91 | * @param TestResult $result |
||
92 | * @param array $action |
||
93 | */ |
||
94 | protected function runTearDown(TestResult $result, $action) |
||
114 | |||
115 | /** |
||
116 | * Set an error handler to handle errors within the test |
||
117 | * |
||
118 | * @param TestResult $result |
||
119 | * @param array &$action |
||
120 | * |
||
121 | * @return callable|null |
||
122 | */ |
||
123 | protected function handleErrors(TestResult $result, &$action) |
||
145 | |||
146 | /** |
||
147 | * Restore the previous error handler |
||
148 | * |
||
149 | * @param callable|null $handler |
||
150 | */ |
||
151 | protected function restoreErrorHandler($handler) |
||
160 | } |
||
161 |