LimeTestRunner.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Runs a set of test methods.
  13. *
  14. * You can add different types of callbacks to a test runner. The most important
  15. * type is the "test" callback. These callbacks are added by calling addTest().
  16. * All "test" callbacks are executed upon calling run().
  17. *
  18. * The other callback types are called before or after the "test" callbacks:
  19. *
  20. * - "before all": Called once before all tests
  21. * - "after all": Called once after all tests
  22. * - "before": Called before each test
  23. * - "after": Called after each test
  24. *
  25. * These callbacks are added by calling addBeforeAll(), addAfterAll(),
  26. * addBefore() and addAfter(). You can add multiple callbacks for each type.
  27. * Callbacks are called in the same order in which they are added.
  28. *
  29. * @package lime
  30. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  31. * @version SVN: $Id: LimeTestRunner.php 23701 2009-11-08 21:23:40Z bschussek $
  32. */
  33. class LimeTestRunner
  34. {
  35. protected
  36. $output = null,
  37. $beforeAllCallbacks = array(),
  38. $afterAllCallbacks = array(),
  39. $beforeCallbacks = array(),
  40. $afterCallbacks = array(),
  41. $testCallbacks = array(),
  42. $testComments = array(),
  43. $errorCallbacks = array(),
  44. $exceptionCallbacks = array();
  45. /**
  46. * Constructor.
  47. *
  48. * @param LimeOutputInterface $output
  49. */
  50. public function __construct(LimeOutputInterface $output = null)
  51. {
  52. if (is_null($output))
  53. {
  54. $output = new LimeOutputNone();
  55. }
  56. $this->output = $output;
  57. }
  58. /**
  59. * Runs all registered callbacks.
  60. */
  61. public function run()
  62. {
  63. foreach ($this->beforeAllCallbacks as $callback)
  64. {
  65. call_user_func($callback);
  66. }
  67. foreach ($this->testCallbacks as $key => $testCallback)
  68. {
  69. if (!empty($this->testComments[$key]))
  70. {
  71. $this->output->comment($this->testComments[$key]);
  72. }
  73. foreach ($this->beforeCallbacks as $callback)
  74. {
  75. call_user_func($callback);
  76. }
  77. try
  78. {
  79. call_user_func($testCallback);
  80. }
  81. catch (Exception $e)
  82. {
  83. $this->handleException($e);
  84. }
  85. foreach ($this->afterCallbacks as $callback)
  86. {
  87. call_user_func($callback);
  88. }
  89. }
  90. foreach ($this->afterAllCallbacks as $callback)
  91. {
  92. call_user_func($callback);
  93. }
  94. }
  95. /**
  96. * Adds a callable that is called once before all tests.
  97. *
  98. * @param callable $callback
  99. * @throws InvalidArgumentException If the argument is no callbale
  100. */
  101. public function addBeforeAll($callback)
  102. {
  103. $this->assertIsCallable($callback);
  104. $this->beforeAllCallbacks[] = $callback;
  105. }
  106. /**
  107. * Adds a callable that is called once after all tests.
  108. *
  109. * @param callable $callback
  110. * @throws InvalidArgumentException If the argument is no callbale
  111. */
  112. public function addAfterAll($callback)
  113. {
  114. $this->assertIsCallable($callback);
  115. $this->afterAllCallbacks[] = $callback;
  116. }
  117. /**
  118. * Adds a callable that is called before each test.
  119. *
  120. * @param callable $callback
  121. * @throws InvalidArgumentException If the argument is no callbale
  122. */
  123. public function addBefore($callback)
  124. {
  125. $this->assertIsCallable($callback);
  126. $this->beforeCallbacks[] = $callback;
  127. }
  128. /**
  129. * Adds a callable that is called after each test.
  130. *
  131. * @param callable $callback
  132. * @throws InvalidArgumentException If the argument is no callbale
  133. */
  134. public function addAfter($callback)
  135. {
  136. $this->assertIsCallable($callback);
  137. $this->afterCallbacks[] = $callback;
  138. }
  139. /**
  140. * Adds a test callable.
  141. *
  142. * @param callable $callback
  143. * @throws InvalidArgumentException If the argument is no callbale
  144. */
  145. public function addTest($callback, $comment = '')
  146. {
  147. $this->assertIsCallable($callback);
  148. $this->testCallbacks[] = $callback;
  149. $this->testComments[] = $comment;
  150. }
  151. /**
  152. * Adds a callback that is called when an exception is thrown in a test.
  153. *
  154. * The callback retrieves the exception as first argument. It
  155. * should return TRUE if it was able to handle the exception successfully and
  156. * FALSE otherwise. In the latter case, the exception is thrown globally.
  157. *
  158. * @param callable $callback
  159. * @throws InvalidArgumentException If the argument is no callbale
  160. */
  161. public function addExceptionHandler($callback)
  162. {
  163. $this->assertIsCallable($callback);
  164. $this->exceptionCallbacks[] = $callback;
  165. }
  166. /**
  167. * Calls all registered exception callbacks.
  168. *
  169. * The exception is passed to the callbacks as first argument.
  170. *
  171. * @param Exception $exception
  172. */
  173. protected function handleException(Exception $exception)
  174. {
  175. foreach ($this->exceptionCallbacks as $callback)
  176. {
  177. if (true === call_user_func($callback, $exception))
  178. {
  179. return;
  180. }
  181. }
  182. throw $exception;
  183. }
  184. /**
  185. * Asserts that the given argument is a callable.
  186. *
  187. * @param mixed $callable
  188. * @throws InvalidArgumentException If the argument is no callbale
  189. */
  190. private function assertIsCallable($callable)
  191. {
  192. if (!is_callable($callable))
  193. {
  194. throw new InvalidArgumentException('The given Argument must be a callable.');
  195. }
  196. }
  197. }