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/Framework.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)
85 throw new BuildException("NativePHPUnitTask requires PHPUnit version >= 3.2.0", $this->getLocation());
88 require_once 'PHPUnit/Util/Filter.php';
90 // point PHPUnit_MAIN_METHOD define to non-existing method
91 if (!defined('PHPUnit_MAIN_METHOD'))
93 define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined');
97 public function main()
99 if (!is_dir(realpath($this->testdirectory))) {
100 throw new BuildException("NativePHPUnitTask requires a Test Directory path given, '".$this->testdirectory."' given.");
102 set_include_path(realpath($this->testdirectory) . PATH_SEPARATOR . get_include_path());
104 $printer = new NativePhpunitPrinter();
107 'configuration' => $this->configuration,
108 'coverageClover' => $this->coverageClover,
109 'junitLogfile' => $this->junitlogfile,
110 'printer' => $printer,
113 require_once "PHPUnit/TextUI/TestRunner.php";
114 $runner = new PHPUnit_TextUI_TestRunner();
115 $suite = $runner->getTest($this->test, $this->testfile, true);
118 $result = $runner->doRun($suite, $arguments);
119 /* @var $result PHPUnit_Framework_TestResult */
121 if ( ($this->haltonfailure && $result->failureCount() > 0) || ($this->haltonerror && $result->errorCount() > 0) ) {
122 throw new BuildException("PHPUnit: ".$result->failureCount()." Failures and ".$result->errorCount()." Errors, ".
123 "last failure message: ".$printer->getMessages());
126 $this->log("PHPUnit Success: ".count($result->passed())." tests passed, no ".
127 "failures (".$result->skippedCount()." skipped, ".$result->notImplementedCount()." not implemented)");
129 // Hudson for example doesn't like the backslash in class names
130 if (file_exists($this->coverageClover)) {
131 $this->log("Generated Clover Coverage XML to: ".$this->coverageClover);
132 $content = file_get_contents($this->coverageClover);
133 $content = str_replace("\\", ".", $content);
134 file_put_contents($this->coverageClover, $content);
138 } catch(\Exception $e) {
139 throw new BuildException("NativePhpunitTask failed: ".$e->getMessage());
144 class NativePhpunitPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
146 private $_messages = array();
148 public function write($buffer)
153 public function getMessages()
155 return $this->_messages;
161 * @param PHPUnit_Framework_Test $test
162 * @param Exception $e
165 public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
167 $this->_messages[] = "Test ERROR: ".$test->getName().": ".$e->getMessage();
171 * A failure occurred.
173 * @param PHPUnit_Framework_Test $test
174 * @param PHPUnit_Framework_AssertionFailedError $e
177 public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
179 $this->_messages[] = "Test FAILED: ".$test->getName().": ".$e->getMessage();
185 * @param PHPUnit_Framework_Test $test
186 * @param Exception $e
189 public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
197 * @param PHPUnit_Framework_Test $test
198 * @param Exception $e
200 * @since Method available since Release 3.0.0
202 public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
208 * A test suite started.
210 * @param PHPUnit_Framework_TestSuite $suite
211 * @since Method available since Release 2.2.0
213 public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
219 * A test suite ended.
221 * @param PHPUnit_Framework_TestSuite $suite
222 * @since Method available since Release 2.2.0
224 public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
232 * @param PHPUnit_Framework_Test $test
234 public function startTest(PHPUnit_Framework_Test $test)
242 * @param PHPUnit_Framework_Test $test
245 public function endTest(PHPUnit_Framework_Test $test, $time)