1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
<?php
/**
* $Id: 3ed9accb8a370bd5d76c5caec723d2cf3f894a80 $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
require_once 'phing/tasks/ext/coverage/CoverageMerger.php';
require_once 'phing/system/util/Timer.php';
/**
* Simple Testrunner for PHPUnit that runs all tests of a testsuite.
*
* @author Michiel Rook <mrook@php.net>
* @version $Id: 3ed9accb8a370bd5d76c5caec723d2cf3f894a80 $
* @package phing.tasks.ext.phpunit
* @since 2.1.0
*/
class PHPUnitTestRunner extends PHPUnit_Runner_BaseTestRunner implements PHPUnit_Framework_TestListener
{
const SUCCESS = 0;
const FAILURES = 1;
const ERRORS = 2;
const INCOMPLETES = 3;
const SKIPPED = 4;
private $retCode = 0;
private $lastErrorMessage = '';
private $lastFailureMessage = '';
private $lastIncompleteMessage = '';
private $lastSkippedMessage = '';
private $formatters = array();
private $codecoverage = null;
private $project = NULL;
private $groups = array();
private $excludeGroups = array();
private $processIsolation = false;
private $useCustomErrorHandler = true;
public function __construct(Project $project, $groups = array(), $excludeGroups = array(), $processIsolation = false)
{
$this->project = $project;
$this->groups = $groups;
$this->excludeGroups = $excludeGroups;
$this->processIsolation = $processIsolation;
$this->retCode = self::SUCCESS;
}
public function setCodecoverage($codecoverage)
{
$this->codecoverage = $codecoverage;
}
public function setUseCustomErrorHandler($useCustomErrorHandler)
{
$this->useCustomErrorHandler = $useCustomErrorHandler;
}
public function addFormatter($formatter)
{
$this->formatters[] = $formatter;
}
public function handleError($level, $message, $file, $line)
{
return PHPUnit_Util_ErrorHandler::handleError($level, $message, $file, $line);
}
/**
* Run a test
*/
public function run(PHPUnit_Framework_TestSuite $suite)
{
$res = new PHPUnit_Framework_TestResult();
if ($this->codecoverage)
{
$whitelist = CoverageMerger::getWhiteList($this->project);
$this->codecoverage->filter()->addFilesToWhiteList($whitelist);
$res->setCodeCoverage($this->codecoverage);
}
$res->addListener($this);
foreach ($this->formatters as $formatter)
{
$res->addListener($formatter);
}
/* Set PHPUnit error handler */
if ($this->useCustomErrorHandler)
{
$oldErrorHandler = set_error_handler(array($this, 'handleError'), E_ALL | E_STRICT);
}
$suite->run($res, false, $this->groups, $this->excludeGroups, $this->processIsolation);
foreach ($this->formatters as $formatter)
{
$formatter->processResult($res);
}
/* Restore Phing error handler */
if ($this->useCustomErrorHandler)
{
restore_error_handler();
}
if ($this->codecoverage)
{
CoverageMerger::merge($this->project, $this->codecoverage->getData());
}
if ($res->errorCount() != 0)
{
$this->retCode = self::ERRORS;
}
else if ($res->failureCount() != 0)
{
$this->retCode = self::FAILURES;
}
else if ($res->notImplementedCount() != 0)
{
$this->retCode = self::INCOMPLETES;
}
else if ($res->skippedCount() != 0)
{
$this->retCode = self::SKIPPED;
}
}
public function getRetCode()
{
return $this->retCode;
}
public function getLastErrorMessage()
{
return $this->lastErrorMessage;
}
public function getLastFailureMessage()
{
return $this->lastFailureMessage;
}
public function getLastIncompleteMessage()
{
return $this->lastIncompleteMessage;
}
public function getLastSkippedMessage()
{
return $this->lastSkippedMessage;
}
protected function composeMessage($message, PHPUnit_Framework_Test $test, Exception $e)
{
$message = "Test $message (" . $test->getName() . " in class " . get_class($test) . "): " . $e->getMessage();
if ($e instanceof PHPUnit_Framework_ExpectationFailedException && $e->getComparisonFailure()) {
$message .= "\n" . $e->getComparisonFailure()->getDiff();
}
return $message;
}
/**
* An error occurred.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
$this->lastErrorMessage = $this->composeMessage("ERROR", $test, $e);
}
/**
* A failure occurred.
*
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_AssertionFailedError $e
* @param float $time
*/
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
$this->lastFailureMessage = $this->composeMessage("FAILURE", $test, $e);
}
/**
* Incomplete test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
$this->lastIncompleteMessage = $this->composeMessage("INCOMPLETE", $test, $e);
}
/**
* Skipped test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
* @since Method available since Release 3.0.0
*/
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
$this->lastSkippedMessage = $this->composeMessage("SKIPPED", $test, $e);
}
/**
* Risky test
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
}
/**
* A test started.
*
* @param string $testName
*/
public function testStarted($testName)
{
}
/**
* A test ended.
*
* @param string $testName
*/
public function testEnded($testName)
{
}
/**
* A test failed.
*
* @param integer $status
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_AssertionFailedError $e
*/
public function testFailed($status, PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e)
{
}
/**
* Override to define how to handle a failed loading of
* a test suite.
*
* @param string $message
*/
protected function runFailed($message)
{
throw new BuildException($message);
}
/**
* A test suite started.
*
* @param PHPUnit_Framework_TestSuite $suite
* @since Method available since Release 2.2.0
*/
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
/**
* A test suite ended.
*
* @param PHPUnit_Framework_TestSuite $suite
* @since Method available since Release 2.2.0
*/
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
}
/**
* A test started.
*
* @param PHPUnit_Framework_Test $test
*/
public function startTest(PHPUnit_Framework_Test $test)
{
}
/**
* A test ended.
*
* @param PHPUnit_Framework_Test $test
* @param float $time
*/
public function endTest(PHPUnit_Framework_Test $test, $time)
{
if ($test instanceof PHPUnit_Framework_TestCase) {
if (!$test->hasPerformedExpectationsOnOutput()) {
echo $test->getActualOutput();
}
}
}
}
|