SimpleTestTask.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * $Id: SimpleTestTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. require_once 'phing/system/io/PhingFile.php';
  23. require_once 'phing/system/io/Writer.php';
  24. require_once 'phing/util/LogWriter.php';
  25. /**
  26. * Runs SimpleTest tests.
  27. *
  28. * @author Michiel Rook <michiel.rook@gmail.com>
  29. * @version $Id: SimpleTestTask.php 905 2010-10-05 16:28:03Z mrook $
  30. * @package phing.tasks.ext.simpletest
  31. * @since 2.2.0
  32. */
  33. class SimpleTestTask extends Task
  34. {
  35. private $formatters = array();
  36. private $haltonerror = false;
  37. private $haltonfailure = false;
  38. private $failureproperty;
  39. private $errorproperty;
  40. private $printsummary = false;
  41. private $testfailed = false;
  42. private $debug = false;
  43. /**
  44. * Initialize Task.
  45. * This method includes any necessary SimpleTest libraries and triggers
  46. * appropriate error if they cannot be found. This is not done in header
  47. * because we may want this class to be loaded w/o triggering an error.
  48. */
  49. function init() {
  50. @include_once 'simpletest/scorer.php';
  51. if (!class_exists('SimpleReporter')) {
  52. throw new BuildException("SimpleTestTask depends on SimpleTest package being installed.", $this->getLocation());
  53. }
  54. require_once 'simpletest/reporter.php';
  55. require_once 'simpletest/xml.php';
  56. require_once 'simpletest/test_case.php';
  57. require_once 'phing/tasks/ext/simpletest/SimpleTestCountResultFormatter.php';
  58. require_once 'phing/tasks/ext/simpletest/SimpleTestDebugResultFormatter.php';
  59. require_once 'phing/tasks/ext/simpletest/SimpleTestFormatterElement.php';
  60. }
  61. function setFailureproperty($value)
  62. {
  63. $this->failureproperty = $value;
  64. }
  65. function setErrorproperty($value)
  66. {
  67. $this->errorproperty = $value;
  68. }
  69. function setHaltonerror($value)
  70. {
  71. $this->haltonerror = $value;
  72. }
  73. function setHaltonfailure($value)
  74. {
  75. $this->haltonfailure = $value;
  76. }
  77. function setPrintsummary($printsummary)
  78. {
  79. $this->printsummary = $printsummary;
  80. }
  81. public function setDebug($debug)
  82. {
  83. $this->debug = $debug;
  84. }
  85. public function getDebug()
  86. {
  87. return $this->debug;
  88. }
  89. /**
  90. * Add a new formatter to all tests of this task.
  91. *
  92. * @param SimpleTestFormatterElement formatter element
  93. */
  94. function addFormatter(SimpleTestFormatterElement $fe)
  95. {
  96. $this->formatters[] = $fe;
  97. }
  98. /**
  99. * Add a new fileset containing the XML results to aggregate
  100. *
  101. * @param FileSet the new fileset containing XML results.
  102. */
  103. function addFileSet(FileSet $fileset)
  104. {
  105. $this->filesets[] = $fileset;
  106. }
  107. /**
  108. * Iterate over all filesets and return the filename of all files
  109. * that end with .php.
  110. *
  111. * @return array an array of filenames
  112. */
  113. private function getFilenames()
  114. {
  115. $filenames = array();
  116. foreach ($this->filesets as $fileset)
  117. {
  118. $ds = $fileset->getDirectoryScanner($this->project);
  119. $ds->scan();
  120. $files = $ds->getIncludedFiles();
  121. foreach ($files as $file)
  122. {
  123. if (strstr($file, ".php"))
  124. {
  125. $filenames[] = $ds->getBaseDir() . "/" . $file;
  126. }
  127. }
  128. }
  129. return $filenames;
  130. }
  131. /**
  132. * The main entry point
  133. *
  134. * @throws BuildException
  135. */
  136. function main()
  137. {
  138. $group = new GroupTest();
  139. $filenames = $this->getFilenames();
  140. foreach ($filenames as $testfile)
  141. {
  142. $group->addTestFile($testfile);
  143. }
  144. if ($this->debug)
  145. {
  146. $fe = new SimpleTestFormatterElement();
  147. $fe->setType('debug');
  148. $fe->setUseFile(false);
  149. $this->formatters[] = $fe;
  150. }
  151. if ($this->printsummary)
  152. {
  153. $fe = new SimpleTestFormatterElement();
  154. $fe->setType('summary');
  155. $fe->setUseFile(false);
  156. $this->formatters[] = $fe;
  157. }
  158. foreach ($this->formatters as $fe)
  159. {
  160. $formatter = $fe->getFormatter();
  161. $formatter->setProject($this->getProject());
  162. if ($fe->getUseFile())
  163. {
  164. $destFile = new PhingFile($fe->getToDir(), $fe->getOutfile());
  165. $writer = new FileWriter($destFile->getAbsolutePath());
  166. $formatter->setOutput($writer);
  167. }
  168. else
  169. {
  170. $formatter->setOutput($this->getDefaultOutput());
  171. }
  172. }
  173. $this->execute($group);
  174. if ($this->testfailed && $this->formatters[0]->getFormatter() instanceof SimpleTestDebugResultFormatter )
  175. {
  176. $this->getDefaultOutput()->write("Failed tests: ");
  177. $this->formatters[0]->getFormatter()->printFailingTests();
  178. }
  179. if ($this->testfailed)
  180. {
  181. throw new BuildException("One or more tests failed");
  182. }
  183. }
  184. private function execute($suite)
  185. {
  186. $counter = new SimpleTestCountResultFormatter();
  187. $reporter = new MultipleReporter();
  188. $reporter->attachReporter($counter);
  189. foreach ($this->formatters as $fe)
  190. {
  191. // SimpleTest 1.0.1 workaround
  192. $formatterList[] = $fe->getFormatter();
  193. $reporter->attachReporter(end($formatterList));
  194. }
  195. $suite->run($reporter);
  196. $retcode = $counter->getRetCode();
  197. if ($retcode == SimpleTestCountResultFormatter::ERRORS)
  198. {
  199. if ($this->errorproperty)
  200. {
  201. $this->project->setNewProperty($this->errorproperty, true);
  202. }
  203. if ($this->haltonerror)
  204. {
  205. $this->testfailed = true;
  206. }
  207. }
  208. elseif ($retcode == SimpleTestCountResultFormatter::FAILURES)
  209. {
  210. if ($this->failureproperty)
  211. {
  212. $this->project->setNewProperty($this->failureproperty, true);
  213. }
  214. if ($this->haltonfailure)
  215. {
  216. $this->testfailed = true;
  217. }
  218. }
  219. }
  220. private function getDefaultOutput()
  221. {
  222. return new LogWriter($this);
  223. }
  224. }