Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / tests / NativePhpunitTask.php
1 <?php
2 /**
3  * Native PHPUnit Task
4  *
5  * LICENSE
6  *
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.
12  */
13
14 require_once 'PHPUnit/Autoload.php';
15
16 /**
17  * A more flexible and powerful PHPUnit Task than the native Phing one.
18  *
19  * Plus forward compability for PHPUnit 3.5 and later is ensured by using the PHPUnit Test Runner instead of implementing one.
20  *
21  * @author Benjamin Eberlei <kontakt@beberlei.de>
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         if (strlen($junitlogfile) == 0) {
48             $junitlogfile = NULL;
49         }
50
51         $this->junitlogfile = $junitlogfile;
52     }
53
54     public function setConfiguration($configuration) {
55         if (strlen($configuration) == 0) {
56             $configuration = NULL;
57         }
58
59         $this->configuration = $configuration;
60     }
61
62     public function setCoverageClover($coverageClover) {
63         if (strlen($coverageClover) == 0) {
64             $coverageClover = NULL;
65         }
66
67         $this->coverageClover = $coverageClover;
68     }
69
70     public function setHaltonfailure($haltonfailures) {
71         $this->haltonfailure = $haltonfailures;
72     }
73
74     public function setHaltonerror($haltonerrors) {
75         $this->haltonerror = $haltonerrors;
76     }
77
78     public function init()
79     {
80         require_once "PHPUnit/Runner/Version.php";
81         $version = PHPUnit_Runner_Version::id();
82
83         if (version_compare($version, '3.4.0') < 0) {
84             throw new BuildException("NativePHPUnitTask requires PHPUnit version >= 3.2.0", $this->getLocation());
85         }
86
87         require_once 'PHPUnit/Util/Filter.php';
88
89         // point PHPUnit_MAIN_METHOD define to non-existing method
90         if (!defined('PHPUnit_MAIN_METHOD')) {
91             define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined');
92         }
93     }
94
95     public function main()
96     {
97         if (!is_dir(realpath($this->testdirectory))) {
98             throw new BuildException("NativePHPUnitTask requires a Test Directory path given, '".$this->testdirectory."' given.");
99         }
100         set_include_path(realpath($this->testdirectory) . PATH_SEPARATOR . get_include_path());
101
102         $printer = new NativePhpunitPrinter();
103
104         $arguments = array(
105             'configuration' => $this->configuration,
106             'coverageClover' => $this->coverageClover,
107             'junitLogfile' => $this->junitlogfile,
108             'printer' => $printer,
109         );
110
111         $runner = new PHPUnit_TextUI_TestRunner();
112         $suite = $runner->getTest($this->test, $this->testfile, true);
113
114         try {
115             $result = $runner->doRun($suite, $arguments);
116             /* @var $result PHPUnit_Framework_TestResult */
117
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());
121             }
122
123             $this->log("PHPUnit Success: ".count($result->passed())." tests passed, no ".
124                 "failures (".$result->skippedCount()." skipped, ".$result->notImplementedCount()." not implemented)");
125
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);
132                 unset($content);
133             }
134
135         } catch(\Exception $e) {
136             throw new BuildException("NativePhpunitTask failed: ".$e->getMessage());
137         }
138     }
139 }
140
141 class NativePhpunitPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
142 {
143     private $_messages = array();
144
145     public function write($buffer)
146     {
147         // do nothing
148     }
149
150     public function getMessages()
151     {
152         return $this->_messages;
153     }
154
155     /**
156      * An error occurred.
157      *
158      * @param  PHPUnit_Framework_Test $test
159      * @param  Exception              $e
160      * @param  float                  $time
161      */
162     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
163     {
164         $this->_messages[] = "Test ERROR: ".$test->getName().": ".$e->getMessage();
165     }
166
167     /**
168      * A failure occurred.
169      *
170      * @param  PHPUnit_Framework_Test                 $test
171      * @param  PHPUnit_Framework_AssertionFailedError $e
172      * @param  float                                  $time
173      */
174     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
175     {
176         $this->_messages[] = "Test FAILED: ".$test->getName().": ".$e->getMessage();
177     }
178
179     /**
180      * Incomplete test.
181      *
182      * @param  PHPUnit_Framework_Test $test
183      * @param  Exception              $e
184      * @param  float                  $time
185      */
186     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
187     {
188
189     }
190
191     /**
192      * Skipped test.
193      *
194      * @param  PHPUnit_Framework_Test $test
195      * @param  Exception              $e
196      * @param  float                  $time
197      * @since  Method available since Release 3.0.0
198      */
199     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
200     {
201
202     }
203
204     /**
205      * A test suite started.
206      *
207      * @param  PHPUnit_Framework_TestSuite $suite
208      * @since  Method available since Release 2.2.0
209      */
210     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
211     {
212
213     }
214
215     /**
216      * A test suite ended.
217      *
218      * @param  PHPUnit_Framework_TestSuite $suite
219      * @since  Method available since Release 2.2.0
220      */
221     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
222     {
223
224     }
225
226     /**
227      * A test started.
228      *
229      * @param  PHPUnit_Framework_Test $test
230      */
231     public function startTest(PHPUnit_Framework_Test $test)
232     {
233
234     }
235
236     /**
237      * A test ended.
238      *
239      * @param  PHPUnit_Framework_Test $test
240      * @param  float                  $time
241      */
242     public function endTest(PHPUnit_Framework_Test $test, $time)
243     {
244
245     }
246 }