]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Runner/BaseTestRunner.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Runner / BaseTestRunner.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category   Testing
38  * @package    PHPUnit
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 2.0.0
45  */
46
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49 require_once 'PHPUnit/Runner/StandardTestSuiteLoader.php';
50
51 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
52
53 /**
54  * Base class for all test runners.
55  *
56  * @category   Testing
57  * @package    PHPUnit
58  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
59  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
60  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
61  * @version    Release: 3.3.17
62  * @link       http://www.phpunit.de/
63  * @since      Class available since Release 2.0.0
64  * @abstract
65  */
66 abstract class PHPUnit_Runner_BaseTestRunner implements PHPUnit_Framework_TestListener
67 {
68     const STATUS_PASSED     = 0;
69     const STATUS_SKIPPED    = 1;
70     const STATUS_INCOMPLETE = 2;
71     const STATUS_FAILURE    = 3;
72     const STATUS_ERROR      = 4;
73     const SUITE_METHODNAME  = 'suite';
74
75     /**
76      * An error occurred.
77      *
78      * @param  PHPUnit_Framework_Test $test
79      * @param  Exception              $e
80      * @param  float                  $time
81      */
82     public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
83     {
84         $this->testFailed(self::STATUS_ERROR, $test, $e);
85     }
86
87     /**
88      * A failure occurred.
89      *
90      * @param  PHPUnit_Framework_Test                 $test
91      * @param  PHPUnit_Framework_AssertionFailedError $e
92      * @param  float                                  $time
93      */
94     public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
95     {
96         $this->testFailed(self::STATUS_FAILURE, $test, $e);
97     }
98
99     /**
100      * Incomplete test.
101      *
102      * @param  PHPUnit_Framework_Test $test
103      * @param  Exception              $e
104      * @param  float                  $time
105      */
106     public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
107     {
108         $this->testFailed(self::STATUS_INCOMPLETE, $test, $e);
109     }
110
111     /**
112      * Skipped test.
113      *
114      * @param  PHPUnit_Framework_Test $test
115      * @param  Exception              $e
116      * @param  float                  $time
117      * @since  Method available since Release 3.0.0
118      */
119     public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
120     {
121         $this->testFailed(self::STATUS_SKIPPED, $test, $e);
122     }
123
124     /**
125      * A testsuite started.
126      *
127      * @param  PHPUnit_Framework_TestSuite $suite
128      * @since  Method available since Release 2.2.0
129      */
130     public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
131     {
132     }
133
134     /**
135      * A testsuite ended.
136      *
137      * @param  PHPUnit_Framework_TestSuite $suite
138      * @since  Method available since Release 2.2.0
139      */
140     public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
141     {
142     }
143
144     /**
145      * A test started.
146      *
147      * @param  PHPUnit_Framework_Test  $test
148      */
149     public function startTest(PHPUnit_Framework_Test $test)
150     {
151         $this->testStarted($test->getName());
152     }
153
154     /**
155      * A test ended.
156      *
157      * @param  PHPUnit_Framework_Test $test
158      * @param  float                  $time
159      */
160     public function endTest(PHPUnit_Framework_Test $test, $time)
161     {
162         $this->testEnded($test->getName());
163     }
164
165     /**
166      * Returns the loader to be used.
167      *
168      * @return PHPUnit_Runner_TestSuiteLoader
169      */
170     public function getLoader()
171     {
172         return new PHPUnit_Runner_StandardTestSuiteLoader;
173     }
174
175     /**
176      * Returns the Test corresponding to the given suite.
177      * This is a template method, subclasses override
178      * the runFailed() and clearStatus() methods.
179      *
180      * @param  string  $suiteClassName
181      * @param  string  $suiteClassFile
182      * @param  boolean $syntaxCheck
183      * @return PHPUnit_Framework_Test
184      */
185     public function getTest($suiteClassName, $suiteClassFile = '', $syntaxCheck = TRUE)
186     {
187         if (is_dir($suiteClassName) && !is_file($suiteClassName . '.php') && empty($suiteClassFile)) {
188             $testCollector = new PHPUnit_Runner_IncludePathTestCollector(
189               array($suiteClassName)
190             );
191
192             $suite = new PHPUnit_Framework_TestSuite($suiteClassName);
193             $suite->addTestFiles($testCollector->collectTests(), $syntaxCheck);
194
195             return $suite;
196         }
197
198         try {
199             $testClass = $this->loadSuiteClass(
200               $suiteClassName, $suiteClassFile, $syntaxCheck
201             );
202         }
203
204         catch (Exception $e) {
205             $this->runFailed($e->getMessage());
206             return NULL;
207         }
208
209         try {
210             $suiteMethod = $testClass->getMethod(self::SUITE_METHODNAME);
211
212             if (!$suiteMethod->isStatic()) {
213                 $this->runFailed(
214                   'suite() method must be static.'
215                 );
216
217                 return NULL;
218             }
219
220             try {
221                 $test = $suiteMethod->invoke(NULL, $testClass->getName());
222             }
223
224             catch (ReflectionException $e) {
225                 $this->runFailed(
226                   sprintf(
227                     "Failed to invoke suite() method.\n%s",
228
229                     $e->getMessage()
230                   )
231                 );
232
233                 return NULL;
234             }
235         }
236
237         catch (ReflectionException $e) {
238             $test = new PHPUnit_Framework_TestSuite($testClass);
239         }
240
241         $this->clearStatus();
242
243         return $test;
244     }
245
246     /**
247      * Override to define how to handle a failed loading of
248      * a test suite.
249      *
250      * @param  string  $message
251      */
252     abstract protected function runFailed($message);
253
254     /**
255      * Returns the loaded ReflectionClass for a suite name.
256      *
257      * @param  string  $suiteClassName
258      * @param  string  $suiteClassFile
259      * @param  boolean $syntaxCheck
260      * @return ReflectionClass
261      */
262     protected function loadSuiteClass($suiteClassName, $suiteClassFile = '', $syntaxCheck = TRUE)
263     {
264         $loader = $this->getLoader();
265
266         if ($loader instanceof PHPUnit_Runner_StandardTestSuiteLoader) {
267             return $loader->load($suiteClassName, $suiteClassFile, $syntaxCheck);
268         } else {
269             return $loader->load($suiteClassName, $suiteClassFile);
270         }
271     }
272
273     /**
274      * Clears the status message.
275      *
276      */
277     protected function clearStatus()
278     {
279     }
280
281     /**
282      * A test started.
283      *
284      * @param  string  $testName
285      */
286     abstract public function testStarted($testName);
287
288     /**
289      * A test ended.
290      *
291      * @param  string  $testName
292      */
293     abstract public function testEnded($testName);
294
295     /**
296      * A test failed.
297      *
298      * @param  integer                                 $status
299      * @param  PHPUnit_Framework_Test                 $test
300      * @param  PHPUnit_Framework_AssertionFailedError $e
301      */
302     abstract public function testFailed($status, PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e);
303 }
304 ?>