/resources/phing/AtoumTask.php

https://github.com/agallou/atoum · PHP · 459 lines · 360 code · 99 blank · 0 comment · 26 complexity · 350cc75d4f9c2571303f315f2e88b726 MD5 · raw file

  1. <?php
  2. use
  3. mageekguy\atoum,
  4. mageekguy\atoum\reports,
  5. mageekguy\atoum\reports\realtime,
  6. mageekguy\atoum\report\fields\runner\coverage
  7. ;
  8. if (defined('mageekguy\atoum\phing\task\path') === false)
  9. {
  10. define('mageekguy\atoum\phing\task\path', 'phing/Task.php');
  11. }
  12. require_once mageekguy\atoum\phing\task\path;
  13. class atoumTask extends task
  14. {
  15. private $runner = false;
  16. private $fileSets = array();
  17. private $bootstrap = null;
  18. private $codeCoverage = false;
  19. private $codeCoverageReportPath = null;
  20. private $codeCoverageReportUrl = null;
  21. private $codeCoverageTreemapPath = null;
  22. private $codeCoverageTreemapUrl = null;
  23. private $codeCoverageXunitPath = null;
  24. private $codeCoverageCloverPath = null;
  25. private $atoumPharPath = null;
  26. private $atoumAutoloaderPath = null;
  27. private $phpPath = null;
  28. private $showProgress = true;
  29. private $showDuration = true;
  30. private $showMemory = true;
  31. private $showCodeCoverage = true;
  32. private $showMissingCodeCoverage = true;
  33. private $maxChildren = 0;
  34. public function __construct(atoum\runner $runner = null)
  35. {
  36. $this->setRunner($runner);
  37. }
  38. public function setRunner(atoum\runner $runner = null)
  39. {
  40. $this->runner = $runner;
  41. return $this;
  42. }
  43. public function getRunner()
  44. {
  45. if ($this->runner === null)
  46. {
  47. $this->runner = new atoum\runner();
  48. }
  49. return $this->runner;
  50. }
  51. public function codeCoverageEnabled()
  52. {
  53. return ($this->codeCoverage === true || $this->codeCoverageReportPath !== null || $this->codeCoverageTreemapPath !== null);
  54. }
  55. public function createFileSet()
  56. {
  57. $this->fileSets[] = $fileSet = new Fileset();
  58. return $fileSet;
  59. }
  60. private function getFiles()
  61. {
  62. $files = array();
  63. foreach ($this->fileSets as $fs)
  64. {
  65. $ds = $fs->getDirectoryScanner($this->project);
  66. $dir = $fs->getDir($this->project);
  67. $srcFiles = $ds->getIncludedFiles();
  68. foreach ($srcFiles as $file)
  69. {
  70. $files[] = $dir . FileSystem::getFileSystem()->getSeparator() . $file;
  71. }
  72. }
  73. return $files;
  74. }
  75. public function main()
  76. {
  77. if ($this->codeCoverage && extension_loaded('xdebug') === false)
  78. {
  79. throw new exception('AtoumTask depends on Xdebug being installed to gather code coverage information');
  80. }
  81. if ($this->atoumPharPath !== null)
  82. {
  83. require_once $this->atoumPharPath;
  84. }
  85. else if ($this->atoumAutoloaderPath !== null)
  86. {
  87. require_once $this->atoumAutoloaderPath;
  88. }
  89. else if (class_exists('mageekguy\atoum\scripts\runner', false) === false)
  90. {
  91. throw new exception('Unknown class mageekguy\\atoum\\scripts\\runner, consider setting atoumPharPath parameter');
  92. }
  93. atoum\scripts\runner::disableAutorun();
  94. foreach ($this->getFiles() as $file)
  95. {
  96. include_once $file;
  97. }
  98. return $this->execute();
  99. }
  100. public function execute()
  101. {
  102. $report = $this->configureDefaultReport();
  103. $runner = $this->getRunner();
  104. $runner->addReport($report);
  105. if ($this->phpPath !== null)
  106. {
  107. $this->runner->setPhpPath($this->phpPath);
  108. }
  109. if ($this->bootstrap !== null)
  110. {
  111. $this->runner->setBootstrapFile($this->bootstrap);
  112. }
  113. if ($this->maxChildren > 0)
  114. {
  115. $this->runner->setMaxChildrenNumber($this->maxChildren);
  116. }
  117. if ($this->codeCoverageEnabled() === false)
  118. {
  119. $this->runner->disableCodeCoverage();
  120. }
  121. else
  122. {
  123. $this->runner->enableCodeCoverage();
  124. if (($path = $this->codeCoverageCloverPath) !== null)
  125. {
  126. $clover = new atoum\reports\asynchronous\clover();
  127. $this->runner->addReport($this->configureAsynchronousReport($clover, $path));
  128. }
  129. $coverageReportUrl = null;
  130. if (($path = $this->codeCoverageReportPath) !== null)
  131. {
  132. $coverageHtmlField = new coverage\html(isset($this->project) === true ? $this->project->getName() : 'Code coverage report', $path);
  133. $coverageHtmlField->setRootUrl($this->codeCoverageReportUrl ?: 'file://' . $path . '/index.html');
  134. $report->addField($coverageHtmlField);
  135. }
  136. if (($path = $this->codeCoverageTreemapPath) !== null)
  137. {
  138. $report->addField($this->configureCoverageTreemapField($path, $coverageReportUrl));
  139. }
  140. }
  141. if (($path = $this->codeCoverageXunitPath) !== null)
  142. {
  143. $xUnit = new atoum\reports\asynchronous\xunit();
  144. $this->runner->addReport($this->configureAsynchronousReport($xUnit, $path));
  145. }
  146. $score = $this->runner->run();
  147. $failures = ($score->getUncompletedMethodNumber() + $score->getFailNumber() + $score->getErrorNumber() + $score->getExceptionNumber() + $score->getRuntimeExceptionNumber());
  148. if ($failures > 0)
  149. {
  150. throw new BuildException("Tests did not pass");
  151. }
  152. return $this;
  153. }
  154. public function configureDefaultReport(realtime\phing $report = null)
  155. {
  156. $report = $report ?: new realtime\phing();
  157. $report->addWriter(new atoum\writers\std\out());
  158. if ($this->showProgress === true)
  159. {
  160. $report->showProgress();
  161. }
  162. else
  163. {
  164. $report->hideProgress();
  165. }
  166. if ($this->showDuration === true)
  167. {
  168. $report->showDuration();
  169. }
  170. else
  171. {
  172. $report->hideDuration();
  173. }
  174. if ($this->showMemory === true)
  175. {
  176. $report->showMemory();
  177. }
  178. else
  179. {
  180. $report->hideMemory();
  181. }
  182. if ($this->showCodeCoverage === true)
  183. {
  184. $report->showCodeCoverage();
  185. }
  186. else
  187. {
  188. $report->hideCodeCoverage();
  189. }
  190. if ($this->showMissingCodeCoverage === true)
  191. {
  192. $report->showMissingCodeCoverage();
  193. }
  194. else
  195. {
  196. $report->hideMissingCodeCoverage();
  197. }
  198. return $report;
  199. }
  200. public function configureAsynchronousReport(reports\asynchronous $report, $path)
  201. {
  202. $report->addWriter(new atoum\writers\file($path));
  203. return $report;
  204. }
  205. public function configureCoverageTreemapField($coverageTreemapPath, $coverageReportUrl = null)
  206. {
  207. $coverageTreemapField = new coverage\treemap(isset($this->project) ? $this->project->getName() : 'Code coverage treemap', $coverageTreemapPath);
  208. $coverageTreemapField->setTreemapUrl($this->codeCoverageTreemapUrl ?: 'file://' . $coverageTreemapPath . '/index.html');
  209. if ($coverageReportUrl !== null)
  210. {
  211. $coverageTreemapField->setHtmlReportBaseUrl($coverageReportUrl);
  212. }
  213. return $coverageTreemapField;
  214. }
  215. public function setBootstrap($bootstrap)
  216. {
  217. $this->bootstrap = (string) $bootstrap;
  218. return $this;
  219. }
  220. public function getBootstrap()
  221. {
  222. return $this->bootstrap;
  223. }
  224. public function setCodeCoverage($codeCoverage)
  225. {
  226. $this->codeCoverage = (boolean) $codeCoverage;
  227. return $this;
  228. }
  229. public function getCodeCoverage()
  230. {
  231. return $this->codeCoverage;
  232. }
  233. public function setAtoumPharPath($atoumPharPath)
  234. {
  235. $this->atoumPharPath = (string) $atoumPharPath;
  236. return $this;
  237. }
  238. public function getAtoumPharPath()
  239. {
  240. return $this->atoumPharPath;
  241. }
  242. public function setPhpPath($phpPath)
  243. {
  244. $this->phpPath = (string) $phpPath;
  245. return $this;
  246. }
  247. public function getPhpPath()
  248. {
  249. return $this->phpPath;
  250. }
  251. public function setShowCodeCoverage($showCodeCoverage)
  252. {
  253. $this->showCodeCoverage = (boolean) $showCodeCoverage;
  254. return $this;
  255. }
  256. public function getShowCodeCoverage()
  257. {
  258. return $this->showCodeCoverage;
  259. }
  260. public function setShowDuration($showDurationReport)
  261. {
  262. $this->showDuration = (boolean) $showDurationReport;
  263. return $this;
  264. }
  265. public function getShowDuration()
  266. {
  267. return $this->showDuration;
  268. }
  269. public function setShowMemory($showMemoryReport)
  270. {
  271. $this->showMemory = (boolean) $showMemoryReport;
  272. return $this;
  273. }
  274. public function getShowMemory()
  275. {
  276. return $this->showMemory;
  277. }
  278. public function setShowMissingCodeCoverage($showMissingCodeCoverage)
  279. {
  280. $this->showMissingCodeCoverage = (boolean) $showMissingCodeCoverage;
  281. return $this;
  282. }
  283. public function getShowMissingCodeCoverage()
  284. {
  285. return $this->showMissingCodeCoverage;
  286. }
  287. public function setShowProgress($showProgress)
  288. {
  289. $this->showProgress = (boolean) $showProgress;
  290. return $this;
  291. }
  292. public function getShowProgress()
  293. {
  294. return $this->showProgress;
  295. }
  296. public function setAtoumAutoloaderPath($atoumAutoloaderPath)
  297. {
  298. $this->atoumAutoloaderPath = (string) $atoumAutoloaderPath;
  299. return $this;
  300. }
  301. public function getAtoumAutoloaderPath()
  302. {
  303. return $this->atoumAutoloaderPath;
  304. }
  305. public function setCodeCoverageReportPath($codeCoverageReportPath)
  306. {
  307. $this->codeCoverageReportPath = (string) $codeCoverageReportPath;
  308. return $this;
  309. }
  310. public function getCodeCoverageReportPath()
  311. {
  312. return $this->codeCoverageReportPath;
  313. }
  314. public function setCodeCoverageTreemapPath($codeCoverageTreemapPath)
  315. {
  316. $this->codeCoverageTreemapPath = (string) $codeCoverageTreemapPath;
  317. return $this;
  318. }
  319. public function setCodeCoverageTreemapUrl($codeCoverageTreemapUrl)
  320. {
  321. $this->codeCoverageTreemapUrl = (string) $codeCoverageTreemapUrl;
  322. return $this;
  323. }
  324. public function setCodeCoverageReportUrl($codeCoverageReportUrl)
  325. {
  326. $this->codeCoverageReportUrl = (string) $codeCoverageReportUrl;
  327. return $this;
  328. }
  329. public function getCodeCoverageReportUrl()
  330. {
  331. return $this->codeCoverageReportUrl;
  332. }
  333. public function setMaxChildren($maxChildren)
  334. {
  335. $this->maxChildren = (int) $maxChildren;
  336. return $this;
  337. }
  338. public function getMaxChildren()
  339. {
  340. return $this->maxChildren;
  341. }
  342. public function setCodeCoverageXunitPath($codeCoverageXunitPath)
  343. {
  344. $this->codeCoverageXunitPath = (string) $codeCoverageXunitPath;
  345. return $this;
  346. }
  347. public function getCodeCoverageXunitPath()
  348. {
  349. return $this->codeCoverageXunitPath;
  350. }
  351. public function setCodeCoverageCloverPath($codeCoverageCloverPath)
  352. {
  353. $this->codeCoverageCloverPath = (string) $codeCoverageCloverPath;
  354. return $this;
  355. }
  356. public function getCodeCoverageCloverPath()
  357. {
  358. return $this->codeCoverageCloverPath;
  359. }
  360. }