| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | require_once 'PHPUnit/Framework.php'; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | class NativePhpunitTask extends Task |
|---|
| 24 | { |
|---|
| 25 | private $test; |
|---|
| 26 | private $testfile; |
|---|
| 27 | private $testdirectory; |
|---|
| 28 | private $configuration = null; |
|---|
| 29 | private $coverageClover = null; |
|---|
| 30 | private $junitlogfile = null; |
|---|
| 31 | private $haltonfailure = true; |
|---|
| 32 | private $haltonerror = true; |
|---|
| 33 | |
|---|
| 34 | public function setTestdirectory($directory) { |
|---|
| 35 | $this->testdirectory = $directory; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | public function setTest($test) { |
|---|
| 39 | $this->test = $test; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | public function setTestfile($testfile) { |
|---|
| 43 | $this->testfile = $testfile; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | public function setJunitlogfile($junitlogfile) { |
|---|
| 47 | $this->junitlogfile = $junitlogfile; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | public function setConfiguration($configuration) { |
|---|
| 51 | $this->configuration = $configuration; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public function setCoverageClover($coverageClover) { |
|---|
| 55 | $this->coverageClover = $coverageClover; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | public function setHaltonfailure($haltonfailures) { |
|---|
| 59 | $this->haltonfailure = $haltonfailures; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | public function setHaltonerror($haltonerrors) { |
|---|
| 63 | $this->haltonerror = $haltonerrors; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | public function init() |
|---|
| 67 | { |
|---|
| 68 | require_once "PHPUnit/Runner/Version.php"; |
|---|
| 69 | $version = PHPUnit_Runner_Version::id(); |
|---|
| 70 | |
|---|
| 71 | if (version_compare($version, '3.4.0') < 0) |
|---|
| 72 | { |
|---|
| 73 | throw new BuildException("NativePHPUnitTask requires PHPUnit version >= 3.2.0", $this->getLocation()); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | require_once 'PHPUnit/Util/Filter.php'; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | if (!defined('PHPUnit_MAIN_METHOD')) |
|---|
| 80 | { |
|---|
| 81 | define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined'); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | public function main() |
|---|
| 86 | { |
|---|
| 87 | if (!is_dir(realpath($this->testdirectory))) { |
|---|
| 88 | throw new BuildException("NativePHPUnitTask requires a Test Directory path given, '".$this->testdirectory."' given."); |
|---|
| 89 | } |
|---|
| 90 | set_include_path(realpath($this->testdirectory) . PATH_SEPARATOR . get_include_path()); |
|---|
| 91 | |
|---|
| 92 | $printer = new NativePhpunitPrinter(); |
|---|
| 93 | |
|---|
| 94 | $arguments = array( |
|---|
| 95 | 'configuration' => $this->configurationFile, |
|---|
| 96 | 'coverageClover' => $this->coverageClover, |
|---|
| 97 | 'junitLogfile' => $this->junitlogfile, |
|---|
| 98 | 'printer' => $printer, |
|---|
| 99 | ); |
|---|
| 100 | |
|---|
| 101 | require_once "PHPUnit/TextUI/TestRunner.php"; |
|---|
| 102 | $runner = new PHPUnit_TextUI_TestRunner(); |
|---|
| 103 | $suite = $runner->getTest($this->test, $this->testfile, true); |
|---|
| 104 | |
|---|
| 105 | try { |
|---|
| 106 | $result = $runner->doRun($suite, $arguments); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | if ( ($this->haltonfailure && $result->failureCount() > 0) || ($this->haltonerror && $result->errorCount() > 0) ) { |
|---|
| 110 | throw new BuildException("PHPUnit: ".$result->failureCount()." Failures and ".$result->errorCount()." Errors, ". |
|---|
| 111 | "last failure message: ".$printer->getMessages()); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | $this->log("PHPUnit Success: ".count($result->passed())." tests passed, no ". |
|---|
| 115 | "failures (".$result->skippedCount()." skipped, ".$result->notImplementedCount()." not implemented)"); |
|---|
| 116 | |
|---|
| 117 | } catch(\Exception $e) { |
|---|
| 118 | throw new BuildException("NativePhpunitTask failed: ".$e->getMessage()); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | class NativePhpunitPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener |
|---|
| 124 | { |
|---|
| 125 | private $_messages = array(); |
|---|
| 126 | |
|---|
| 127 | public function write($buffer) |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | public function getMessages() |
|---|
| 133 | { |
|---|
| 134 | return $this->_messages; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) |
|---|
| 145 | { |
|---|
| 146 | $this->_messages[] = "Test ERROR: ".$test->getName().": ".$e->getMessage(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) |
|---|
| 157 | { |
|---|
| 158 | $this->_messages[] = "Test FAILED: ".$test->getName().": ".$e->getMessage(); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) |
|---|
| 169 | { |
|---|
| 170 | |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) |
|---|
| 182 | { |
|---|
| 183 | |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | public function startTestSuite(PHPUnit_Framework_TestSuite $suite) |
|---|
| 193 | { |
|---|
| 194 | |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | public function endTestSuite(PHPUnit_Framework_TestSuite $suite) |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | public function startTest(PHPUnit_Framework_Test $test) |
|---|
| 214 | { |
|---|
| 215 | |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | public function endTest(PHPUnit_Framework_Test $test, $time) |
|---|
| 225 | { |
|---|
| 226 | |
|---|
| 227 | } |
|---|
| 228 | } |
|---|