@@ -11,6 +11,14 @@ class UnitTestCase
1111
1212 protected static $ _lastRunsPassesAndFails = array ('passes ' => array (), 'fails ' => array ());
1313
14+ public function setUp ()
15+ {
16+ }
17+
18+ public function tearDown ()
19+ {
20+ }
21+
1422 public function init ()
1523 {
1624 $ tmpFileName = $ this ->getPassesAndFailsCachePath ();
@@ -149,15 +157,11 @@ public function _fail($message = "")
149157 self ::$ _passesAndFails ['fails ' ][$ class ] = $ class ;
150158 }
151159
152- public function run (DoctrineTest_Reporter $ reporter = null , $ filter = null )
160+ public function run (DoctrineTest_Reporter $ reporter = null , $ filter = null )
153161 {
154162 foreach (get_class_methods ($ this ) as $ method ) {
155- if (substr ($ method , 0 , 4 ) === 'test ' ) {
156- $ this ->setUp ();
157-
158- $ this ->$ method ();
159-
160- $ this ->tearDown ();
163+ if ($ this ->isTestMethod ($ method )) {
164+ $ this ->runTest ($ method );
161165 }
162166 }
163167 }
@@ -249,4 +253,49 @@ public function getNumFixedFails()
249253 {
250254 return count ($ this ->getFixedFails ());
251255 }
252- }
256+
257+ private function runTest ($ method )
258+ {
259+ $ this ->setUp ();
260+
261+ $ this ->doRunTestAndTearDown ($ method );
262+ }
263+
264+ private function doRunTestAndTearDown ($ method )
265+ {
266+ $ test = $ this ;
267+
268+ $ this ->tryFinally (
269+ function () use ($ test , $ method ) {
270+ $ test ->$ method ();
271+ },
272+ function () use ($ test ) {
273+ $ test ->tearDown ();
274+ }
275+ );
276+ }
277+
278+ private function isTestMethod ($ method )
279+ {
280+ return 'test ' === substr ($ method , 0 , 4 );
281+ }
282+
283+ private function tryFinally (Closure $ try , Closure $ finally )
284+ {
285+ $ thrownException = null ;
286+
287+ try {
288+ $ try ();
289+ } catch (Throwable $ e ) {
290+ $ thrownException = $ e ;
291+ } catch (Exception $ e ) { // for PHP v5.x
292+ $ thrownException = $ e ;
293+ }
294+
295+ $ finally ();
296+
297+ if (null !== $ thrownException ) {
298+ throw $ thrownException ;
299+ }
300+ }
301+ }
0 commit comments