BatchTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * $Id: BatchTest.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/types/FileSet.php';
  22. /**
  23. * Scans a list of files given by the fileset attribute, extracts valid test cases
  24. *
  25. * @author Michiel Rook <michiel.rook@gmail.com>
  26. * @version $Id: BatchTest.php 905 2010-10-05 16:28:03Z mrook $
  27. * @package phing.tasks.ext.phpunit
  28. * @since 2.1.0
  29. */
  30. class BatchTest
  31. {
  32. /** the list of filesets containing the testcase filename rules */
  33. private $filesets = array();
  34. /** the reference to the project */
  35. private $project = NULL;
  36. /** the classpath to use with Phing::__import() calls */
  37. private $classpath = NULL;
  38. /** names of classes to exclude */
  39. private $excludeClasses = array();
  40. /** name of the batchtest/suite */
  41. protected $name = "Phing Batchtest";
  42. /**
  43. * Create a new batchtest instance
  44. *
  45. * @param Project the project it depends on.
  46. */
  47. public function __construct(Project $project)
  48. {
  49. $this->project = $project;
  50. }
  51. /**
  52. * Sets the name of the batchtest/suite
  53. */
  54. public function setName($name)
  55. {
  56. $this->name = $name;
  57. }
  58. /**
  59. * Sets the classes to exclude
  60. */
  61. public function setExclude($exclude)
  62. {
  63. $this->excludeClasses = explode(" ", $exclude);
  64. }
  65. /**
  66. * Sets the classpath
  67. */
  68. public function setClasspath(Path $classpath)
  69. {
  70. if ($this->classpath === null)
  71. {
  72. $this->classpath = $classpath;
  73. }
  74. else
  75. {
  76. $this->classpath->append($classpath);
  77. }
  78. }
  79. /**
  80. * Creates a new Path object
  81. */
  82. public function createClasspath()
  83. {
  84. $this->classpath = new Path();
  85. return $this->classpath;
  86. }
  87. /**
  88. * Returns the classpath
  89. */
  90. public function getClasspath()
  91. {
  92. return $this->classpath;
  93. }
  94. /**
  95. * Add a new fileset containing the XML results to aggregate
  96. *
  97. * @param FileSet the new fileset containing XML results.
  98. */
  99. public function addFileSet(FileSet $fileset)
  100. {
  101. $this->filesets[] = $fileset;
  102. }
  103. /**
  104. * Iterate over all filesets and return the filename of all files.
  105. *
  106. * @return array an array of filenames
  107. */
  108. private function getFilenames()
  109. {
  110. $filenames = array();
  111. foreach ($this->filesets as $fileset)
  112. {
  113. $ds = $fileset->getDirectoryScanner($this->project);
  114. $ds->scan();
  115. $files = $ds->getIncludedFiles();
  116. foreach ($files as $file)
  117. {
  118. $filenames[] = $ds->getBaseDir() . "/" . $file;
  119. }
  120. }
  121. return $filenames;
  122. }
  123. /**
  124. * Checks wheter $input is a PHPUnit Test
  125. */
  126. private function isTestCase($input)
  127. {
  128. return is_subclass_of($input, 'PHPUnit_Framework_TestCase') || is_subclass_of($input, 'PHPUnit_Framework_TestSuite');
  129. }
  130. /**
  131. * Filters an array of classes, removes all classes that are not test cases or test suites,
  132. * or classes that are declared abstract
  133. */
  134. private function filterTests($input)
  135. {
  136. $reflect = new ReflectionClass($input);
  137. return $this->isTestCase($input) && (!$reflect->isAbstract());
  138. }
  139. /**
  140. * Returns an array of test cases and test suites that are declared
  141. * by the files included by the filesets
  142. *
  143. * @return array an array of tests.
  144. */
  145. protected function elements()
  146. {
  147. $filenames = $this->getFilenames();
  148. $declaredClasses = array();
  149. foreach ($filenames as $filename)
  150. {
  151. $definedClasses = PHPUnitUtil::getDefinedClasses($filename, $this->classpath);
  152. foreach($definedClasses as $definedClass) {
  153. $this->project->log("(PHPUnit) Adding $definedClass (from $filename) to tests.", Project::MSG_DEBUG);
  154. }
  155. $declaredClasses = array_merge($declaredClasses, $definedClasses);
  156. }
  157. $elements = array_filter($declaredClasses, array($this, "filterTests"));
  158. return $elements;
  159. }
  160. /**
  161. * Returns a testsuite containing all the tests in this batch
  162. *
  163. * @return PHPUnit_Framework_TestSuite
  164. */
  165. public function getTestSuite()
  166. {
  167. $suite = new PHPUnit_Framework_TestSuite($this->name);
  168. foreach ($this->elements() as $test)
  169. {
  170. $testClass = new ReflectionClass($test);
  171. $suite->addTestSuite($testClass);
  172. }
  173. return $suite;
  174. }
  175. }