<?php
require_once 'PHPUnit/Autoload.php';
class NativePhpunitTask extends Task {
private $test;
private $testfile;
private $testdirectory;
private $configuration = null;
private $coverageClover = null;
private $junitlogfile = null;
private $haltonfailure = true;
private $haltonerror = true;
public function setTestdirectory($directory) {
$this->testdirectory = $directory;
}
public function setTest($test) {
$this->test = $test;
}
public function setTestfile($testfile) {
$this->testfile = $testfile;
}
public function setJunitlogfile($junitlogfile) {
if (strlen($junitlogfile) == 0) {
$junitlogfile = NULL;
}
$this->junitlogfile = $junitlogfile;
}
public function setConfiguration($configuration) {
if (strlen($configuration) == 0) {
$configuration = NULL;
}
$this->configuration = $configuration;
}
public function setCoverageClover($coverageClover) {
if (strlen($coverageClover) == 0) {
$coverageClover = NULL;
}
$this->coverageClover = $coverageClover;
}
public function setHaltonfailure($haltonfailures) {
$this->haltonfailure = $haltonfailures;
}
public function setHaltonerror($haltonerrors) {
$this->haltonerror = $haltonerrors;
}
public function init() {
require_once "PHPUnit/Runner/Version.php";
$version = PHPUnit_Runner_Version::id();
if (version_compare($version, '3.4.0') < 0) {
throw new BuildException("NativePHPUnitTask requires PHPUnit version >= 3.2.0", $this
->getLocation());
}
require_once 'PHPUnit/Util/Filter.php';
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined');
}
}
public function main() {
if (!is_dir(realpath($this->testdirectory))) {
throw new BuildException("NativePHPUnitTask requires a Test Directory path given, '" . $this->testdirectory . "' given.");
}
set_include_path(realpath($this->testdirectory) . PATH_SEPARATOR . get_include_path());
$printer = new NativePhpunitPrinter();
$arguments = array(
'configuration' => $this->configuration,
'coverageClover' => $this->coverageClover,
'junitLogfile' => $this->junitlogfile,
'printer' => $printer,
);
$runner = new PHPUnit_TextUI_TestRunner();
$suite = $runner
->getTest($this->test, $this->testfile, true);
try {
$result = $runner
->doRun($suite, $arguments);
if ($this->haltonfailure && $result
->failureCount() > 0 || $this->haltonerror && $result
->errorCount() > 0) {
throw new BuildException("PHPUnit: " . $result
->failureCount() . " Failures and " . $result
->errorCount() . " Errors, " . "last failure message: " . $printer
->getMessages());
}
$this
->log("PHPUnit Success: " . count($result
->passed()) . " tests passed, no " . "failures (" . $result
->skippedCount() . " skipped, " . $result
->notImplementedCount() . " not implemented)");
if (file_exists($this->coverageClover)) {
$this
->log("Generated Clover Coverage XML to: " . $this->coverageClover);
$content = file_get_contents($this->coverageClover);
$content = str_replace("\\", ".", $content);
file_put_contents($this->coverageClover, $content);
unset($content);
}
} catch (\Exception $e) {
throw new BuildException("NativePhpunitTask failed: " . $e
->getMessage());
}
}
}
class NativePhpunitPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener {
private $_messages = array();
public function write($buffer) {
}
public function getMessages() {
return $this->_messages;
}
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
$this->_messages[] = "Test ERROR: " . $test
->getName() . ": " . $e
->getMessage();
}
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
$this->_messages[] = "Test FAILED: " . $test
->getName() . ": " . $e
->getMessage();
}
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
}
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
}
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) {
}
}