NativePhpunitTask.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Native PHPUnit Task
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to kontakt@beberlei.de so I can send you a copy immediately.
  12. */
  13. require_once 'PHPUnit/Framework.php';
  14. /**
  15. * A more flexible and powerful PHPUnit Task than the native Phing one.
  16. *
  17. * Plus forward compability for PHPUnit 3.5 and later is ensured by using the PHPUnit Test Runner instead of implementing one.
  18. *
  19. * @author Benjamin Eberlei <kontakt@beberlei.de>
  20. */
  21. class NativePhpunitTask extends Task
  22. {
  23. private $test;
  24. private $testfile;
  25. private $testdirectory;
  26. private $configuration = null;
  27. private $coverageClover = null;
  28. private $junitlogfile = null;
  29. private $haltonfailure = true;
  30. private $haltonerror = true;
  31. public function setTestdirectory($directory) {
  32. $this->testdirectory = $directory;
  33. }
  34. public function setTest($test) {
  35. $this->test = $test;
  36. }
  37. public function setTestfile($testfile) {
  38. $this->testfile = $testfile;
  39. }
  40. public function setJunitlogfile($junitlogfile) {
  41. if (strlen($junitlogfile) == 0) {
  42. $junitlogfile = NULL;
  43. }
  44. $this->junitlogfile = $junitlogfile;
  45. }
  46. public function setConfiguration($configuration) {
  47. if (strlen($configuration) == 0) {
  48. $configuration = NULL;
  49. }
  50. $this->configuration = $configuration;
  51. }
  52. public function setCoverageClover($coverageClover) {
  53. if (strlen($coverageClover) == 0) {
  54. $coverageClover = NULL;
  55. }
  56. $this->coverageClover = $coverageClover;
  57. }
  58. public function setHaltonfailure($haltonfailures) {
  59. $this->haltonfailure = $haltonfailures;
  60. }
  61. public function setHaltonerror($haltonerrors) {
  62. $this->haltonerror = $haltonerrors;
  63. }
  64. public function init()
  65. {
  66. require_once "PHPUnit/Runner/Version.php";
  67. $version = PHPUnit_Runner_Version::id();
  68. if (version_compare($version, '3.4.0') < 0)
  69. {
  70. throw new BuildException("NativePHPUnitTask requires PHPUnit version >= 3.2.0", $this->getLocation());
  71. }
  72. require_once 'PHPUnit/Util/Filter.php';
  73. // point PHPUnit_MAIN_METHOD define to non-existing method
  74. if (!defined('PHPUnit_MAIN_METHOD'))
  75. {
  76. define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined');
  77. }
  78. }
  79. public function main()
  80. {
  81. if (!is_dir(realpath($this->testdirectory))) {
  82. throw new BuildException("NativePHPUnitTask requires a Test Directory path given, '".$this->testdirectory."' given.");
  83. }
  84. set_include_path(realpath($this->testdirectory) . PATH_SEPARATOR . get_include_path());
  85. $printer = new NativePhpunitPrinter();
  86. $arguments = array(
  87. 'configuration' => $this->configuration,
  88. 'coverageClover' => $this->coverageClover,
  89. 'junitLogfile' => $this->junitlogfile,
  90. 'printer' => $printer,
  91. );
  92. require_once "PHPUnit/TextUI/TestRunner.php";
  93. $runner = new PHPUnit_TextUI_TestRunner();
  94. $suite = $runner->getTest($this->test, $this->testfile, true);
  95. try {
  96. $result = $runner->doRun($suite, $arguments);
  97. /* @var $result PHPUnit_Framework_TestResult */
  98. if ( ($this->haltonfailure && $result->failureCount() > 0) || ($this->haltonerror && $result->errorCount() > 0) ) {
  99. throw new BuildException("PHPUnit: ".$result->failureCount()." Failures and ".$result->errorCount()." Errors, ".
  100. "last failure message: ".$printer->getMessages());
  101. }
  102. $this->log("PHPUnit Success: ".count($result->passed())." tests passed, no ".
  103. "failures (".$result->skippedCount()." skipped, ".$result->notImplementedCount()." not implemented)");
  104. // Hudson for example doesn't like the backslash in class names
  105. if (file_exists($this->coverageClover)) {
  106. $this->log("Generated Clover Coverage XML to: ".$this->coverageClover);
  107. $content = file_get_contents($this->coverageClover);
  108. $content = str_replace("\\", ".", $content);
  109. file_put_contents($this->coverageClover, $content);
  110. unset($content);
  111. }
  112. } catch(\Exception $e) {
  113. throw new BuildException("NativePhpunitTask failed: ".$e->getMessage());
  114. }
  115. }
  116. }
  117. class NativePhpunitPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
  118. {
  119. private $_messages = array();
  120. public function write($buffer)
  121. {
  122. // do nothing
  123. }
  124. public function getMessages()
  125. {
  126. return $this->_messages;
  127. }
  128. /**
  129. * An error occurred.
  130. *
  131. * @param PHPUnit_Framework_Test $test
  132. * @param Exception $e
  133. * @param float $time
  134. */
  135. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  136. {
  137. $this->_messages[] = "Test ERROR: ".$test->getName().": ".$e->getMessage();
  138. }
  139. /**
  140. * A failure occurred.
  141. *
  142. * @param PHPUnit_Framework_Test $test
  143. * @param PHPUnit_Framework_AssertionFailedError $e
  144. * @param float $time
  145. */
  146. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  147. {
  148. $this->_messages[] = "Test FAILED: ".$test->getName().": ".$e->getMessage();
  149. }
  150. /**
  151. * Incomplete test.
  152. *
  153. * @param PHPUnit_Framework_Test $test
  154. * @param Exception $e
  155. * @param float $time
  156. */
  157. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  158. {
  159. }
  160. /**
  161. * Skipped test.
  162. *
  163. * @param PHPUnit_Framework_Test $test
  164. * @param Exception $e
  165. * @param float $time
  166. * @since Method available since Release 3.0.0
  167. */
  168. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  169. {
  170. }
  171. /**
  172. * A test suite started.
  173. *
  174. * @param PHPUnit_Framework_TestSuite $suite
  175. * @since Method available since Release 2.2.0
  176. */
  177. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  178. {
  179. }
  180. /**
  181. * A test suite ended.
  182. *
  183. * @param PHPUnit_Framework_TestSuite $suite
  184. * @since Method available since Release 2.2.0
  185. */
  186. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  187. {
  188. }
  189. /**
  190. * A test started.
  191. *
  192. * @param PHPUnit_Framework_Test $test
  193. */
  194. public function startTest(PHPUnit_Framework_Test $test)
  195. {
  196. }
  197. /**
  198. * A test ended.
  199. *
  200. * @param PHPUnit_Framework_Test $test
  201. * @param float $time
  202. */
  203. public function endTest(PHPUnit_Framework_Test $test, $time)
  204. {
  205. }
  206. }