PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PHPUnit/Runner/BaseTestRunner.php

https://github.com/item/sugarcrm_dev
PHP | 304 lines | 111 code | 34 blank | 159 comment | 6 complexity | 522e87fe87270181c9ca1831effb24c9 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category Testing
  38. * @package PHPUnit
  39. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 2.0.0
  44. */
  45. require_once 'PHPUnit/Framework.php';
  46. require_once 'PHPUnit/Util/Filter.php';
  47. require_once 'PHPUnit/Runner/StandardTestSuiteLoader.php';
  48. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  49. /**
  50. * Base class for all test runners.
  51. *
  52. * @category Testing
  53. * @package PHPUnit
  54. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  55. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  56. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  57. * @version Release: 3.3.17
  58. * @link http://www.phpunit.de/
  59. * @since Class available since Release 2.0.0
  60. * @abstract
  61. */
  62. abstract class PHPUnit_Runner_BaseTestRunner implements PHPUnit_Framework_TestListener
  63. {
  64. const STATUS_PASSED = 0;
  65. const STATUS_SKIPPED = 1;
  66. const STATUS_INCOMPLETE = 2;
  67. const STATUS_FAILURE = 3;
  68. const STATUS_ERROR = 4;
  69. const SUITE_METHODNAME = 'suite';
  70. /**
  71. * An error occurred.
  72. *
  73. * @param PHPUnit_Framework_Test $test
  74. * @param Exception $e
  75. * @param float $time
  76. */
  77. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  78. {
  79. $this->testFailed(self::STATUS_ERROR, $test, $e);
  80. }
  81. /**
  82. * A failure occurred.
  83. *
  84. * @param PHPUnit_Framework_Test $test
  85. * @param PHPUnit_Framework_AssertionFailedError $e
  86. * @param float $time
  87. */
  88. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  89. {
  90. $this->testFailed(self::STATUS_FAILURE, $test, $e);
  91. }
  92. /**
  93. * Incomplete test.
  94. *
  95. * @param PHPUnit_Framework_Test $test
  96. * @param Exception $e
  97. * @param float $time
  98. */
  99. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  100. {
  101. $this->testFailed(self::STATUS_INCOMPLETE, $test, $e);
  102. }
  103. /**
  104. * Skipped test.
  105. *
  106. * @param PHPUnit_Framework_Test $test
  107. * @param Exception $e
  108. * @param float $time
  109. * @since Method available since Release 3.0.0
  110. */
  111. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  112. {
  113. $this->testFailed(self::STATUS_SKIPPED, $test, $e);
  114. }
  115. /**
  116. * A testsuite started.
  117. *
  118. * @param PHPUnit_Framework_TestSuite $suite
  119. * @since Method available since Release 2.2.0
  120. */
  121. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  122. {
  123. }
  124. /**
  125. * A testsuite ended.
  126. *
  127. * @param PHPUnit_Framework_TestSuite $suite
  128. * @since Method available since Release 2.2.0
  129. */
  130. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  131. {
  132. }
  133. /**
  134. * A test started.
  135. *
  136. * @param PHPUnit_Framework_Test $test
  137. */
  138. public function startTest(PHPUnit_Framework_Test $test)
  139. {
  140. $this->testStarted($test->getName());
  141. }
  142. /**
  143. * A test ended.
  144. *
  145. * @param PHPUnit_Framework_Test $test
  146. * @param float $time
  147. */
  148. public function endTest(PHPUnit_Framework_Test $test, $time)
  149. {
  150. $this->testEnded($test->getName());
  151. }
  152. /**
  153. * Returns the loader to be used.
  154. *
  155. * @return PHPUnit_Runner_TestSuiteLoader
  156. */
  157. public function getLoader()
  158. {
  159. return new PHPUnit_Runner_StandardTestSuiteLoader;
  160. }
  161. /**
  162. * Returns the Test corresponding to the given suite.
  163. * This is a template method, subclasses override
  164. * the runFailed() and clearStatus() methods.
  165. *
  166. * @param string $suiteClassName
  167. * @param string $suiteClassFile
  168. * @param boolean $syntaxCheck
  169. * @return PHPUnit_Framework_Test
  170. */
  171. public function getTest($suiteClassName, $suiteClassFile = '', $syntaxCheck = TRUE)
  172. {
  173. if (is_dir($suiteClassName) && !is_file($suiteClassName . '.php') && empty($suiteClassFile)) {
  174. $testCollector = new PHPUnit_Runner_IncludePathTestCollector(
  175. array($suiteClassName)
  176. );
  177. $suite = new PHPUnit_Framework_TestSuite($suiteClassName);
  178. $suite->addTestFiles($testCollector->collectTests(), $syntaxCheck);
  179. return $suite;
  180. }
  181. try {
  182. $testClass = $this->loadSuiteClass(
  183. $suiteClassName, $suiteClassFile, $syntaxCheck
  184. );
  185. }
  186. catch (Exception $e) {
  187. $this->runFailed($e->getMessage());
  188. return NULL;
  189. }
  190. try {
  191. $suiteMethod = $testClass->getMethod(self::SUITE_METHODNAME);
  192. if (!$suiteMethod->isStatic()) {
  193. $this->runFailed(
  194. 'suite() method must be static.'
  195. );
  196. return NULL;
  197. }
  198. try {
  199. $test = $suiteMethod->invoke(NULL, $testClass->getName());
  200. }
  201. catch (ReflectionException $e) {
  202. $this->runFailed(
  203. sprintf(
  204. "Failed to invoke suite() method.\n%s",
  205. $e->getMessage()
  206. )
  207. );
  208. return NULL;
  209. }
  210. }
  211. catch (ReflectionException $e) {
  212. $test = new PHPUnit_Framework_TestSuite($testClass);
  213. }
  214. $this->clearStatus();
  215. return $test;
  216. }
  217. /**
  218. * Override to define how to handle a failed loading of
  219. * a test suite.
  220. *
  221. * @param string $message
  222. */
  223. abstract protected function runFailed($message);
  224. /**
  225. * Returns the loaded ReflectionClass for a suite name.
  226. *
  227. * @param string $suiteClassName
  228. * @param string $suiteClassFile
  229. * @param boolean $syntaxCheck
  230. * @return ReflectionClass
  231. */
  232. protected function loadSuiteClass($suiteClassName, $suiteClassFile = '', $syntaxCheck = TRUE)
  233. {
  234. $loader = $this->getLoader();
  235. if ($loader instanceof PHPUnit_Runner_StandardTestSuiteLoader) {
  236. return $loader->load($suiteClassName, $suiteClassFile, $syntaxCheck);
  237. } else {
  238. return $loader->load($suiteClassName, $suiteClassFile);
  239. }
  240. }
  241. /**
  242. * Clears the status message.
  243. *
  244. */
  245. protected function clearStatus()
  246. {
  247. }
  248. /**
  249. * A test started.
  250. *
  251. * @param string $testName
  252. */
  253. abstract public function testStarted($testName);
  254. /**
  255. * A test ended.
  256. *
  257. * @param string $testName
  258. */
  259. abstract public function testEnded($testName);
  260. /**
  261. * A test failed.
  262. *
  263. * @param integer $status
  264. * @param PHPUnit_Framework_Test $test
  265. * @param PHPUnit_Framework_AssertionFailedError $e
  266. */
  267. abstract public function testFailed($status, PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e);
  268. }
  269. ?>