/NormalUnitTests.php

https://github.com/pear/phpuc · PHP · 118 lines · 98 code · 16 blank · 4 comment · 12 complexity · 61a3b98e4c7bb733de525cc1946ba7f4 MD5 · raw file

  1. <?php
  2. class NormalUnitTests {
  3. public function build(Package $p) {
  4. ob_start();
  5. echo '<?xml version="1.0" encoding="UTF-8"?>';
  6. ?>
  7. <project name="<?php print $p->package; ?>" basedir="<?php print $p->source; ?>/<?php print $p->package; ?>" default="build">
  8. <target name="php-codesniffer">
  9. <exec executable="phpcs" dir="${basedir}" output="<?php print $p->jenkins; ?>/projects/<?php print $p->package; ?>/build/logs/checkstyle.xml">
  10. <arg line="--report=checkstyle --standard=PEAR --ignore=tests <?php print $p->source; ?>/<?php print $p->package; ?>"/>
  11. </exec>
  12. </target>
  13. <target name="phpmd">
  14. <exec executable="phpmd" dir="${basedir}">
  15. <arg line="${basedir} xml codesize,unusedcode,naming --reportfile <?php print $p->jenkins; ?>/projects/<?php print $p->package; ?>/build/logs/pmd.xml"/>
  16. </exec>
  17. </target>
  18. <target name="phpcpd">
  19. <exec executable="phpcpd" dir="${basedir}">
  20. <arg line="${basedir} --log-pmd <?php print $p->jenkins; ?>/projects/<?php print $p->package; ?>/build/logs/cpd.xml"/>
  21. </exec>
  22. </target>
  23. <target name="phpunit">
  24. <exec executable="phpunit" dir="${basedir}" failonerror="on">
  25. <?php if (extension_loaded('xdebug')) { ?>
  26. <arg line="-d error_reporting='E_ALL &amp; ~E_STRICT &amp; ~E_DEPRECATED'
  27. --log-junit build/logs/junit.xml
  28. <?php print $this->getBootstrap($p); ?>
  29. --coverage-clover build/logs/clover.xml
  30. --coverage-html build/coverage
  31. <?php print $this->getTestPath($p); ?>" />
  32. <?php } else { ?>
  33. <arg line="-d error_reporting='E_ALL &amp; ~E_STRICT &amp; ~E_DEPRECATED'
  34. --log-junit build/logs/junit.xml <?php print $this->getBootstrap($p); ?>
  35. <?php print $this->getTestPath($p); ?>" />
  36. <?php } ?>
  37. </exec>
  38. </target>
  39. <?php if ($p->pyrus) { ?>
  40. <target name="package">
  41. <!-- Todo: refactor this, not everyone lives in /home/clockwerx -->
  42. <exec executable="php" dir="${basedir}">
  43. <arg line="-d error_reporting='E_ALL &amp; ~E_STRICT &amp; ~E_DEPRECATED' /home/clockwerx/phpuc/make-package.php ${basedir}" />
  44. </exec>
  45. <exec executable="php" dir="${basedir}" failonerror="on">
  46. <arg line="<?php print $p->pyrus ?> package -o <?php print $p->jenkins; ?>/projects/<?php print $p->package; ?>/build/package/trunk.tar.gz" />
  47. </exec>
  48. <exec executable="svn" dir="${basedir}">
  49. <arg line="revert package.xml" />
  50. </exec>
  51. </target>
  52. <?php } ?>
  53. <?php if ($p->pyrus) { ?>
  54. <target name="build" depends="php-codesniffer,phpmd,phpcpd,phpunit,package" />
  55. <?php } else { ?>
  56. <target name="build" depends="php-codesniffer,phpmd,phpcpd,phpunit" />
  57. <?php } ?>
  58. </project>
  59. <?php
  60. return ob_get_clean();
  61. }
  62. public function getTestPath(Package $p) {
  63. $path = $p->source . "/" . $p->package . "/tests/AllTests.php";
  64. if (file_exists($path)) {
  65. return $path;
  66. }
  67. $path = $p->source . "/" . $p->package . "/tests/";
  68. if (file_exists($path)) {
  69. return $path;
  70. }
  71. return false;
  72. }
  73. /** @todo Refactor this when 10 other packages have bootstraps? */
  74. public function getBootstrap(Package $p) {
  75. if ($p->package == "Mail_Queue") {
  76. return "--bootstrap " . $p->source . "/" . $p->package . "/tests/TestInit.php";
  77. }
  78. return "";
  79. }
  80. /**
  81. * Iterate over a given directory to find all PHPUnit tests, and phpt tests
  82. */
  83. public function collect_directories(RecursiveDirectoryIterator $dir) {
  84. $dirs = array();
  85. foreach ($dir as $file) {
  86. if (!is_dir($file)) {
  87. continue;
  88. }
  89. $test_dir = (string)$file . '/tests/AllTests.php';
  90. if (file_exists($test_dir)) {
  91. $dirs[] = $file;
  92. continue;
  93. }
  94. $test_dir = (string)$file . '/tests/';
  95. if (file_exists($test_dir)) {
  96. $dirs[] = $file;
  97. continue;
  98. }
  99. }
  100. return $dirs;
  101. }
  102. }