PageRenderTime 144ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/phing/tasks/ext/phpunit/PHPUnitTask.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 390 lines | 275 code | 53 blank | 62 comment | 42 complexity | 4c6d6449bfc894454ca4f491f9103af1 MD5 | raw file
  1. <?php
  2. /**
  3. * $Id: PHPUnitTask.php 427 2008-10-28 19:34:15Z 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 PHPUnit2/3 tests.
  27. *
  28. * @author Michiel Rook <michiel.rook@gmail.com>
  29. * @version $Id: PHPUnitTask.php 427 2008-10-28 19:34:15Z mrook $
  30. * @package phing.tasks.ext.phpunit
  31. * @see BatchTest
  32. * @since 2.1.0
  33. */
  34. class PHPUnitTask extends Task
  35. {
  36. private $batchtests = array();
  37. private $formatters = array();
  38. private $haltonerror = false;
  39. private $haltonfailure = false;
  40. private $haltonincomplete = false;
  41. private $haltonskipped = false;
  42. private $errorproperty;
  43. private $failureproperty;
  44. private $incompleteproperty;
  45. private $skippedproperty;
  46. private $printsummary = false;
  47. private $testfailed = false;
  48. private $codecoverage = false;
  49. private $groups = array();
  50. private $excludeGroups = array();
  51. /**
  52. * Initialize Task.
  53. * This method includes any necessary PHPUnit2 libraries and triggers
  54. * appropriate error if they cannot be found. This is not done in header
  55. * because we may want this class to be loaded w/o triggering an error.
  56. */
  57. function init() {
  58. if (version_compare(PHP_VERSION, '5.0.3') < 0) {
  59. throw new BuildException("PHPUnit2Task requires PHP version >= 5.0.3.", $this->getLocation());
  60. }
  61. /**
  62. * Determine PHPUnit version number
  63. */
  64. @include_once 'PHPUnit/Runner/Version.php';
  65. @include_once 'PHPUnit2/Runner/Version.php';
  66. if (class_exists('PHPUnit_Runner_Version'))
  67. {
  68. $version = PHPUnit_Runner_Version::id();
  69. }
  70. elseif (class_exists('PHPUnit2_Runner_Version'))
  71. {
  72. $version = PHPUnit2_Runner_Version::id();
  73. }
  74. else
  75. {
  76. throw new BuildException("PHPUnit task depends on PHPUnit 2 or 3 package being installed.", $this->getLocation());
  77. }
  78. if (version_compare($version, "3.0.0") >= 0)
  79. {
  80. PHPUnitUtil::$installedVersion = 3;
  81. if (version_compare($version, "3.2.0") >= 0)
  82. {
  83. PHPUnitUtil::$installedMinorVersion = 2;
  84. }
  85. }
  86. else
  87. {
  88. PHPUnitUtil::$installedVersion = 2;
  89. }
  90. /**
  91. * Other dependencies that should only be loaded when class is actually used.
  92. */
  93. require_once 'phing/tasks/ext/phpunit/PHPUnitTestRunner.php';
  94. require_once 'phing/tasks/ext/phpunit/BatchTest.php';
  95. require_once 'phing/tasks/ext/phpunit/FormatterElement.php';
  96. /**
  97. * Add some defaults to the PHPUnit filter
  98. */
  99. $pwd = dirname(__FILE__);
  100. if (PHPUnitUtil::$installedVersion == 3)
  101. {
  102. require_once 'PHPUnit/Framework.php';
  103. require_once 'PHPUnit/Util/Filter.php';
  104. // point PHPUnit_MAIN_METHOD define to non-existing method
  105. if (!defined('PHPUnit_MAIN_METHOD')) {
  106. define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined');
  107. }
  108. PHPUnit_Util_Filter::addFileToFilter($pwd . '/PHPUnitTask.php', 'PHING');
  109. PHPUnit_Util_Filter::addFileToFilter($pwd . '/PHPUnitTestRunner.php', 'PHING');
  110. PHPUnit_Util_Filter::addFileToFilter($pwd . '/../../../Task.php', 'PHING');
  111. PHPUnit_Util_Filter::addFileToFilter($pwd . '/../../../Target.php', 'PHING');
  112. PHPUnit_Util_Filter::addFileToFilter($pwd . '/../../../Project.php', 'PHING');
  113. PHPUnit_Util_Filter::addFileToFilter($pwd . '/../../../Phing.php', 'PHING');
  114. }
  115. else
  116. {
  117. require_once 'PHPUnit2/Framework.php';
  118. require_once 'PHPUnit2/Util/Filter.php';
  119. PHPUnit2_Util_Filter::addFileToFilter($pwd . '/PHPUnitTask.php');
  120. PHPUnit2_Util_Filter::addFileToFilter($pwd . '/PHPUnitTestRunner.php');
  121. PHPUnit2_Util_Filter::addFileToFilter($pwd . '/../../../Task.php');
  122. PHPUnit2_Util_Filter::addFileToFilter($pwd . '/../../../Target.php');
  123. PHPUnit2_Util_Filter::addFileToFilter($pwd . '/../../../Project.php');
  124. PHPUnit2_Util_Filter::addFileToFilter($pwd . '/../../../Phing.php');
  125. }
  126. }
  127. function setErrorproperty($value)
  128. {
  129. $this->errorproperty = $value;
  130. }
  131. function setFailureproperty($value)
  132. {
  133. $this->failureproperty = $value;
  134. }
  135. function setIncompleteproperty($value)
  136. {
  137. $this->incompleteproperty = $value;
  138. }
  139. function setSkippedproperty($value)
  140. {
  141. $this->skippedproperty = $value;
  142. }
  143. function setHaltonerror($value)
  144. {
  145. $this->haltonerror = $value;
  146. }
  147. function setHaltonfailure($value)
  148. {
  149. $this->haltonfailure = $value;
  150. }
  151. function setHaltonincomplete($value)
  152. {
  153. $this->haltonincomplete = $value;
  154. }
  155. function setHaltonskipped($value)
  156. {
  157. $this->haltonskipped = $value;
  158. }
  159. function setPrintsummary($printsummary)
  160. {
  161. $this->printsummary = $printsummary;
  162. }
  163. function setCodecoverage($codecoverage)
  164. {
  165. $this->codecoverage = $codecoverage;
  166. }
  167. function setGroups($groups)
  168. {
  169. if (PHPUnitUtil::$installedVersion < 3 || (PHPUnitUtil::$installedVersion == 3 && PHPUnitUtil::$installedMinorVersion < 2))
  170. {
  171. $this->log("The 'groups' attribute is only available with PHPUnit 3.2.0 or newer", Project::MSG_WARN);
  172. }
  173. $token = ' ,;';
  174. $this->groups = array();
  175. $tok = strtok($groups, $token);
  176. while ($tok !== false) {
  177. $this->groups[] = $tok;
  178. $tok = strtok($token);
  179. }
  180. }
  181. function setExcludeGroups($excludeGroups)
  182. {
  183. if (PHPUnitUtil::$installedVersion < 3 || (PHPUnitUtil::$installedVersion == 3 && PHPUnitUtil::$installedMinorVersion < 2))
  184. {
  185. $this->log("The 'excludeGroups' attribute is only available with PHPUnit 3.2.0 or newer", Project::MSG_WARN);
  186. }
  187. $token = ' ,;';
  188. $this->excludeGroups = array();
  189. $tok = strtok($groups, $token);
  190. while ($tok !== false) {
  191. $this->excludeGroups[] = $tok;
  192. $tok = strtok($token);
  193. }
  194. }
  195. /**
  196. * Add a new formatter to all tests of this task.
  197. *
  198. * @param FormatterElement formatter element
  199. */
  200. function addFormatter(FormatterElement $fe)
  201. {
  202. $this->formatters[] = $fe;
  203. }
  204. /**
  205. * The main entry point
  206. *
  207. * @throws BuildException
  208. */
  209. function main()
  210. {
  211. $tests = array();
  212. if ($this->printsummary)
  213. {
  214. $fe = new FormatterElement();
  215. $fe->setType("summary");
  216. $fe->setUseFile(false);
  217. $this->formatters[] = $fe;
  218. }
  219. foreach ($this->batchtests as $batchtest)
  220. {
  221. $tests = array_merge($tests, $batchtest->elements());
  222. }
  223. foreach ($this->formatters as $fe)
  224. {
  225. $formatter = $fe->getFormatter();
  226. $formatter->setProject($this->getProject());
  227. if ($fe->getUseFile())
  228. {
  229. $destFile = new PhingFile($fe->getToDir(), $fe->getOutfile());
  230. $writer = new FileWriter($destFile->getAbsolutePath());
  231. $formatter->setOutput($writer);
  232. }
  233. else
  234. {
  235. $formatter->setOutput($this->getDefaultOutput());
  236. }
  237. $formatter->startTestRun();
  238. }
  239. foreach ($tests as $test)
  240. {
  241. $suite = NULL;
  242. if ((PHPUnitUtil::$installedVersion == 3 && is_subclass_of($test, 'PHPUnit_Framework_TestSuite')) || (PHPUnitUtil::$installedVersion == 2 && is_subclass_of($test, 'PHPUnit2_Framework_TestSuite')))
  243. {
  244. if (is_object($test))
  245. {
  246. $suite = $test;
  247. }
  248. else
  249. {
  250. $suite = new $test();
  251. }
  252. }
  253. else
  254. {
  255. if (PHPUnitUtil::$installedVersion == 3)
  256. {
  257. require_once 'PHPUnit/Framework/TestSuite.php';
  258. $suite = new PHPUnit_Framework_TestSuite(new ReflectionClass($test));
  259. }
  260. else
  261. {
  262. require_once 'PHPUnit2/Framework/TestSuite.php';
  263. $suite = new PHPUnit2_Framework_TestSuite(new ReflectionClass($test));
  264. }
  265. }
  266. $this->execute($suite);
  267. }
  268. foreach ($this->formatters as $fe)
  269. {
  270. $formatter = $fe->getFormatter();
  271. $formatter->endTestRun();
  272. }
  273. if ($this->testfailed)
  274. {
  275. throw new BuildException("One or more tests failed");
  276. }
  277. }
  278. /**
  279. * @throws BuildException
  280. */
  281. private function execute($suite)
  282. {
  283. $runner = new PHPUnitTestRunner($suite, $this->project, $this->groups, $this->excludeGroups);
  284. $runner->setCodecoverage($this->codecoverage);
  285. foreach ($this->formatters as $fe)
  286. {
  287. $formatter = $fe->getFormatter();
  288. $runner->addFormatter($formatter);
  289. }
  290. $runner->run();
  291. $retcode = $runner->getRetCode();
  292. if ($retcode == PHPUnitTestRunner::ERRORS) {
  293. if ($this->errorproperty) {
  294. $this->project->setNewProperty($this->errorproperty, true);
  295. }
  296. if ($this->haltonerror) {
  297. $this->testfailed = true;
  298. }
  299. } elseif ($retcode == PHPUnitTestRunner::FAILURES) {
  300. if ($this->failureproperty) {
  301. $this->project->setNewProperty($this->failureproperty, true);
  302. }
  303. if ($this->haltonfailure) {
  304. $this->testfailed = true;
  305. }
  306. } elseif ($retcode == PHPUnitTestRunner::INCOMPLETES) {
  307. if ($this->incompleteproperty) {
  308. $this->project->setNewProperty($this->incompleteproperty, true);
  309. }
  310. if ($this->haltonincomplete) {
  311. $this->testfailed = true;
  312. }
  313. } elseif ($retcode == PHPUnitTestRunner::SKIPPED) {
  314. if ($this->skippedproperty) {
  315. $this->project->setNewProperty($this->skippedproperty, true);
  316. }
  317. if ($this->haltonskipped) {
  318. $this->testfailed = true;
  319. }
  320. }
  321. }
  322. private function getDefaultOutput()
  323. {
  324. return new LogWriter($this);
  325. }
  326. /**
  327. * Adds a set of tests based on pattern matching.
  328. *
  329. * @return BatchTest a new instance of a batch test.
  330. */
  331. function createBatchTest()
  332. {
  333. $batchtest = new BatchTest($this->getProject());
  334. $this->batchtests[] = $batchtest;
  335. return $batchtest;
  336. }
  337. }