123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- if (version_compare(PHPUnit_Runner_Version::id(), '3.5.0') >=0) {
- require_once 'PHPUnit/Autoload.php';
- }
- require_once 'PHPUnit/Util/ErrorHandler.php';
- require_once 'PHPUnit/Util/Filter.php';
- require_once 'phing/tasks/ext/coverage/CoverageMerger.php';
- require_once 'phing/system/util/Timer.php';
- 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 $lastFailureMessage = "";
- private $formatters = array();
-
- private $codecoverage = false;
-
- private $project = NULL;
- private $groups = array();
- private $excludeGroups = array();
-
- private $useCustomErrorHandler = true;
- public function __construct(Project $project, $groups = array(), $excludeGroups = array())
- {
- $this->project = $project;
- $this->groups = $groups;
- $this->excludeGroups = $excludeGroups;
- $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 static function handleError($level, $message, $file, $line)
- {
- $isFiltered = false;
- if (version_compare(PHPUnit_Runner_Version::id(), '3.5.0') >=0) {
- $isFiltered = PHP_CodeCoverage::getInstance()->filter()->isFiltered(
- $file, array(), true
- );
- } else {
- $isFiltered = PHPUnit_Util_Filter::isFiltered($file, true, true);
- }
- if (!$isFiltered) {
- return PHPUnit_Util_ErrorHandler::handleError($level, $message, $file, $line);
- }
- }
-
-
- public function run(PHPUnit_Framework_TestSuite $suite)
- {
- $res = new PHPUnit_Framework_TestResult();
- if ($this->codecoverage)
- {
- $res->collectCodeCoverageInformation(TRUE);
- }
-
- $res->addListener($this);
- foreach ($this->formatters as $formatter)
- {
- $res->addListener($formatter);
- }
-
-
- if ($this->useCustomErrorHandler)
- {
- $oldErrorHandler = set_error_handler(array('PHPUnitTestRunner', 'handleError'), E_ALL | E_STRICT);
- }
-
- $suite->run($res, false, $this->groups, $this->excludeGroups);
-
- foreach ($this->formatters as $formatter)
- {
- $formatter->processResult($res);
- }
-
-
- if ($this->useCustomErrorHandler)
- {
- restore_error_handler();
- }
-
- if ($this->codecoverage)
- {
- $coverage = $res->getCodeCoverage();
-
- $summary = $coverage->getSummary();
- CoverageMerger::merge($this->project, $summary);
- }
-
- 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 getLastFailureMessage()
- {
- return $this->lastFailureMessage;
- }
-
- public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
- {
- $this->lastFailureMessage = "Test ERROR (" . $test->getName() . "): " . $e->getMessage();
- }
-
- public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
- {
- $this->lastFailureMessage = "Test FAILURE (" . $test->getName() . "): " . $e->getMessage();
- }
-
- public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
- {
- $this->lastFailureMessage = "Test INCOMPLETE (" . $test->getName() . "): " . $e->getMessage();
- }
-
- public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
- {
- $this->lastFailureMessage = "Test SKIPPED (" . $test->getName() . "): " . $e->getMessage();
- }
-
- public function testStarted($testName)
- {
- }
-
- public function testEnded($testName)
- {
- }
-
- public function testFailed($status, PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e)
- {
- }
-
- protected function runFailed($message)
- {
- throw new BuildException($message);
- }
-
- public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
- {
- }
-
- public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
- {
- }
-
- public function startTest(PHPUnit_Framework_Test $test)
- {
- }
-
- public function endTest(PHPUnit_Framework_Test $test, $time)
- {
- }
- }
|