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