NativePhpunitTask.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/Autoload.php';
  14. /**
  15. * A more flexible and powerful PHPUnit Task than the native Phing one.
  16. *
  17. * Plus forward compatibility 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. throw new BuildException("NativePHPUnitTask requires PHPUnit version >= 3.2.0", $this->getLocation());
  70. }
  71. require_once 'PHPUnit/Util/Filter.php';
  72. // point PHPUnit_MAIN_METHOD define to non-existing method
  73. if (!defined('PHPUnit_MAIN_METHOD')) {
  74. define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined');
  75. }
  76. }
  77. public function main()
  78. {
  79. if (!is_dir(realpath($this->testdirectory))) {
  80. throw new BuildException("NativePHPUnitTask requires a Test Directory path given, '".$this->testdirectory."' given.");
  81. }
  82. set_include_path(realpath($this->testdirectory) . PATH_SEPARATOR . get_include_path());
  83. $printer = new NativePhpunitPrinter();
  84. $arguments = array(
  85. 'configuration' => $this->configuration,
  86. 'coverageClover' => $this->coverageClover,
  87. 'junitLogfile' => $this->junitlogfile,
  88. 'printer' => $printer,
  89. );
  90. $runner = new PHPUnit_TextUI_TestRunner();
  91. $suite = $runner->getTest($this->test, $this->testfile, true);
  92. try {
  93. $result = $runner->doRun($suite, $arguments);
  94. /* @var $result PHPUnit_Framework_TestResult */
  95. if ( ($this->haltonfailure && $result->failureCount() > 0) || ($this->haltonerror && $result->errorCount() > 0) ) {
  96. throw new BuildException("PHPUnit: ".$result->failureCount()." Failures and ".$result->errorCount()." Errors, ".
  97. "last failure message: ".$printer->getMessages());
  98. }
  99. $this->log("PHPUnit Success: ".count($result->passed())." tests passed, no ".
  100. "failures (".$result->skippedCount()." skipped, ".$result->notImplementedCount()." not implemented)");
  101. // Hudson for example doesn't like the backslash in class names
  102. if (is_file($this->coverageClover)) {
  103. $this->log("Generated Clover Coverage XML to: ".$this->coverageClover);
  104. $content = file_get_contents($this->coverageClover);
  105. $content = str_replace("\\", ".", $content);
  106. file_put_contents($this->coverageClover, $content);
  107. unset($content);
  108. }
  109. } catch(\Exception $e) {
  110. throw new BuildException("NativePhpunitTask failed: ".$e->getMessage());
  111. }
  112. }
  113. }
  114. class NativePhpunitPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
  115. {
  116. private $_messages = array();
  117. public function write($buffer)
  118. {
  119. // do nothing
  120. }
  121. public function getMessages()
  122. {
  123. return $this->_messages;
  124. }
  125. /**
  126. * An error occurred.
  127. *
  128. * @param PHPUnit_Framework_Test $test
  129. * @param Exception $e
  130. * @param float $time
  131. */
  132. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
  133. {
  134. $this->_messages[] = "Test ERROR: ".$test->getName().": ".$e->getMessage();
  135. }
  136. /**
  137. * A failure occurred.
  138. *
  139. * @param PHPUnit_Framework_Test $test
  140. * @param PHPUnit_Framework_AssertionFailedError $e
  141. * @param float $time
  142. */
  143. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
  144. {
  145. $this->_messages[] = "Test FAILED: ".$test->getName().": ".$e->getMessage();
  146. }
  147. /**
  148. * Incomplete test.
  149. *
  150. * @param PHPUnit_Framework_Test $test
  151. * @param Exception $e
  152. * @param float $time
  153. */
  154. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  155. {
  156. }
  157. /**
  158. * Skipped test.
  159. *
  160. * @param PHPUnit_Framework_Test $test
  161. * @param Exception $e
  162. * @param float $time
  163. * @since Method available since Release 3.0.0
  164. */
  165. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
  166. {
  167. }
  168. /**
  169. * A test suite started.
  170. *
  171. * @param PHPUnit_Framework_TestSuite $suite
  172. * @since Method available since Release 2.2.0
  173. */
  174. public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
  175. {
  176. }
  177. /**
  178. * A test suite ended.
  179. *
  180. * @param PHPUnit_Framework_TestSuite $suite
  181. * @since Method available since Release 2.2.0
  182. */
  183. public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
  184. {
  185. }
  186. /**
  187. * A test started.
  188. *
  189. * @param PHPUnit_Framework_Test $test
  190. */
  191. public function startTest(PHPUnit_Framework_Test $test)
  192. {
  193. }
  194. /**
  195. * A test ended.
  196. *
  197. * @param PHPUnit_Framework_Test $test
  198. * @param float $time
  199. */
  200. public function endTest(PHPUnit_Framework_Test $test, $time)
  201. {
  202. }
  203. }