Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / 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/Framework.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         {
85             throw new BuildException("NativePHPUnitTask requires PHPUnit version >= 3.2.0", $this->getLocation());
86         }
87
88         require_once 'PHPUnit/Util/Filter.php';
89
90         // point PHPUnit_MAIN_METHOD define to non-existing method
91         if (!defined('PHPUnit_MAIN_METHOD'))
92         {
93             define('PHPUnit_MAIN_METHOD', 'PHPUnitTask::undefined');
94         }
95     }
96
97     public function main()
98     {
99         if (!is_dir(realpath($this->testdirectory))) {
100             throw new BuildException("NativePHPUnitTask requires a Test Directory path given, '".$this->testdirectory."' given.");
101         }
102         set_include_path(realpath($this->testdirectory) . PATH_SEPARATOR . get_include_path());
103
104         $printer = new NativePhpunitPrinter();
105
106         $arguments = array(
107             'configuration' => $this->configuration,
108             'coverageClover' => $this->coverageClover,
109             'junitLogfile' => $this->junitlogfile,
110             'printer' => $printer,
111         );
112
113         require_once "PHPUnit/TextUI/TestRunner.php";
114         $runner = new PHPUnit_TextUI_TestRunner();
115         $suite = $runner->getTest($this->test, $this->testfile, true);
116
117         try {
118             $result = $runner->doRun($suite, $arguments);
119             /* @var $result PHPUnit_Framework_TestResult */
120
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());
124             }
125
126             $this->log("PHPUnit Success: ".count($result->passed())." tests passed, no ".
127                 "failures (".$result->skippedCount()." skipped, ".$result->notImplementedCount()." not implemented)");
128
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);
135                 unset($content);
136             }
137
138         } catch(\Exception $e) {
139             throw new BuildException("NativePhpunitTask failed: ".$e->getMessage());
140         }
141     }
142 }
143
144 class NativePhpunitPrinter extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
145 {
146     private $_messages = array();
147
148     public function write($buffer)
149     {
150         // do nothing
151     }
152
153     public function getMessages()
154     {
155         return $this->_messages;
156     }
157
158     /**
159      * An error occurred.
160      *
161      * @param  PHPUnit_Framework_Test $test
162      * @param  Exception              $e
163      * @param  float                  $time
164      */
165     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
166     {
167         $this->_messages[] = "Test ERROR: ".$test->getName().": ".$e->getMessage();
168     }
169
170     /**
171      * A failure occurred.
172      *
173      * @param  PHPUnit_Framework_Test                 $test
174      * @param  PHPUnit_Framework_AssertionFailedError $e
175      * @param  float                                  $time
176      */
177     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
178     {
179         $this->_messages[] = "Test FAILED: ".$test->getName().": ".$e->getMessage();
180     }
181
182     /**
183      * Incomplete test.
184      *
185      * @param  PHPUnit_Framework_Test $test
186      * @param  Exception              $e
187      * @param  float                  $time
188      */
189     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
190     {
191
192     }
193
194     /**
195      * Skipped test.
196      *
197      * @param  PHPUnit_Framework_Test $test
198      * @param  Exception              $e
199      * @param  float                  $time
200      * @since  Method available since Release 3.0.0
201      */
202     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
203     {
204
205     }
206
207     /**
208      * A test suite started.
209      *
210      * @param  PHPUnit_Framework_TestSuite $suite
211      * @since  Method available since Release 2.2.0
212      */
213     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
214     {
215
216     }
217
218     /**
219      * A test suite ended.
220      *
221      * @param  PHPUnit_Framework_TestSuite $suite
222      * @since  Method available since Release 2.2.0
223      */
224     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
225     {
226
227     }
228
229     /**
230      * A test started.
231      *
232      * @param  PHPUnit_Framework_Test $test
233      */
234     public function startTest(PHPUnit_Framework_Test $test)
235     {
236
237     }
238
239     /**
240      * A test ended.
241      *
242      * @param  PHPUnit_Framework_Test $test
243      * @param  float                  $time
244      */
245     public function endTest(PHPUnit_Framework_Test $test, $time)
246     {
247
248     }
249 }