7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to kontakt@beberlei.de so I can send you a copy immediately.
14 require_once 'PHPUnit/Autoload.php';
17 * A more flexible and powerful PHPUnit Task than the native Phing one.
19 * Plus forward compability for PHPUnit 3.5 and later is ensured by using the PHPUnit Test Runner instead of implementing one.
21 * @author Benjamin Eberlei <kontakt@beberlei.de>
23 class NativePhpunitTask extends Task
27 private $testdirectory;
28 private $configuration = null;
29 private $coverageClover = null;
30 private $junitlogfile = null;
31 private $haltonfailure = true;
32 private $haltonerror = true;
34 public function setTestdirectory($directory) {
35 $this->testdirectory = $directory;
38 public function setTest($test) {
42 public function setTestfile($testfile) {
43 $this->testfile = $testfile;
46 public function setJunitlogfile($junitlogfile) {
47 if (strlen($junitlogfile) == 0) {
51 $this->junitlogfile = $junitlogfile;
54 public function setConfiguration($configuration) {
55 if (strlen($configuration) == 0) {
56 $configuration = NULL;
59 $this->configuration = $configuration;
62 public function setCoverageClover($coverageClover) {
63 if (strlen($coverageClover) == 0) {
64 $coverageClover = NULL;
67 $this->coverageClover = $coverageClover;
70 public function setHaltonfailure($haltonfailures) {
71 $this->haltonfailure = $haltonfailures;
74 public function setHaltonerror($haltonerrors) {
75 $this->haltonerror = $haltonerrors;
78 public function init()
80 require_once "PHPUnit/Runner/Version.php";
81 $version = PHPUnit_Runner_Version::id();
83 if (version_compare($version, '3.4.0') < 0) {
84 throw new BuildException("NativePHPUnitTask requires PHPUnit version >= 3.2.0", $this->getLocation());
87 require_once 'PHPUnit/Util/Filter.php';
89 // point PHPUnit_MAIN_METHOD define to non-existing method
90 if (!defined('PHPUnit_MAIN_METHOD')) {
91 define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined');
95 public function main()
97 if (!is_dir(realpath($this->testdirectory))) {
98 throw new BuildException("NativePHPUnitTask requires a Test Directory path given, '".$this->testdirectory."' given.");
100 set_include_path(realpath($this->testdirectory) . PATH_SEPARATOR . get_include_path());
102 $printer = new NativePhpunitPrinter();
105 'configuration' => $this->configuration,
106 'coverageClover' => $this->coverageClover,
107 'junitLogfile' => $this->junitlogfile,
108 'printer' => $printer,
111 $runner = new PHPUnit_TextUI_TestRunner();
112 $suite = $runner->getTest($this->test, $this->testfile, true);
115 $result = $runner->doRun($suite, $arguments);
116 /* @var $result PHPUnit_Framework_TestResult */
118 if ( ($this->haltonfailure && $result->failureCount() > 0) || ($this->haltonerror && $result->errorCount() > 0) ) {
119 throw new BuildException("PHPUnit: ".$result->failureCount()." Failures and ".$result->errorCount()." Errors, ".
120 "last failure message: ".$printer->getMessages());
123 $this->log("PHPUnit Success: ".count($result->passed())." tests passed, no ".
124 "failures (".$result->skippedCount()." skipped, ".$result->notImplementedCount()." not implemented)");
126 // Hudson for example doesn't like the backslash in class names
127 if (file_exists($this->coverageClover)) {
128 $this->log("Generated Clover Coverage XML to: ".$this->coverageClover);
129 $content = file_get_contents($this->coverageClover);
130 $content = str_replace("\\", ".", $content);
131 file_put_contents($this->coverageClover, $content);
135 } catch(\Exception $e) {
136 throw new BuildException("NativePhpunitTask failed: ".$e->getMessage());
141 class NativePhpunitPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
143 private $_messages = array();
145 public function write($buffer)
150 public function getMessages()
152 return $this->_messages;
158 * @param PHPUnit_Framework_Test $test
159 * @param Exception $e
162 public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
164 $this->_messages[] = "Test ERROR: ".$test->getName().": ".$e->getMessage();
168 * A failure occurred.
170 * @param PHPUnit_Framework_Test $test
171 * @param PHPUnit_Framework_AssertionFailedError $e
174 public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
176 $this->_messages[] = "Test FAILED: ".$test->getName().": ".$e->getMessage();
182 * @param PHPUnit_Framework_Test $test
183 * @param Exception $e
186 public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
194 * @param PHPUnit_Framework_Test $test
195 * @param Exception $e
197 * @since Method available since Release 3.0.0
199 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
205 * A test suite started.
207 * @param PHPUnit_Framework_TestSuite $suite
208 * @since Method available since Release 2.2.0
210 public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
216 * A test suite ended.
218 * @param PHPUnit_Framework_TestSuite $suite
219 * @since Method available since Release 2.2.0
221 public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
229 * @param PHPUnit_Framework_Test $test
231 public function startTest(PHPUnit_Framework_Test $test)
239 * @param PHPUnit_Framework_Test $test
242 public function endTest(PHPUnit_Framework_Test $test, $time)