]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Extensions/PhptTestCase.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Extensions / PhptTestCase.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 3.1.4
45  */
46
47 if (PHPUnit_Util_Filesystem::fileExistsInIncludePath('PEAR/RunTest.php')) {
48     $currentErrorReporting = error_reporting(E_ERROR | E_WARNING | E_PARSE);
49     PHPUnit_Util_Filesystem::collectStart();
50     require_once 'PEAR/RunTest.php';
51     error_reporting($currentErrorReporting);
52
53     foreach (PHPUnit_Util_Filesystem::collectEnd() as $blacklistedFile) {
54         PHPUnit_Util_Filter::addFileToFilter($blacklistedFile, 'PHPUNIT');
55     }
56 }
57
58 require_once 'PHPUnit/Framework.php';
59 require_once 'PHPUnit/Extensions/PhptTestCase/Logger.php';
60 require_once 'PHPUnit/Util/Filter.php';
61
62 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
63
64 /**
65  * Wrapper to run .phpt test cases.
66  *
67  * @category   Testing
68  * @package    PHPUnit
69  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
70  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
71  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
72  * @version    Release: 3.3.17
73  * @link       http://www.phpunit.de/
74  * @since      Class available since Release 3.1.4
75  */
76 class PHPUnit_Extensions_PhptTestCase implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
77 {
78     /**
79      * The filename of the .phpt file.
80      *
81      * @var    string
82      */
83     protected $filename;
84
85     /**
86      * Options for PEAR_RunTest.
87      *
88      * @var    array
89      */
90     protected $options = array();
91
92     /**
93      * Constructs a test case with the given filename.
94      *
95      * @param  string $filename
96      * @param  array  $options
97      */
98     public function __construct($filename, $options = array())
99     {
100         if (!is_string($filename)) {
101             throw new InvalidArgumentException;
102         }
103
104         if (!is_file($filename)) {
105             throw new RuntimeException(
106               sprintf(
107                 'File "%s" does not exist.',
108                 $filename
109               )
110             );
111         }
112
113         if (!is_array($options)) {
114             throw new InvalidArgumentException;
115         }
116
117         $this->filename = $filename;
118         $this->options  = $options;
119     }
120
121     /**
122      * Counts the number of test cases executed by run(TestResult result).
123      *
124      * @return integer
125      */
126     public function count()
127     {
128         return 1;
129     }
130
131     /**
132      * Runs a test and collects its result in a TestResult instance.
133      *
134      * @param  PHPUnit_Framework_TestResult $result
135      * @param  array                        $options
136      * @return PHPUnit_Framework_TestResult
137      */
138     public function run(PHPUnit_Framework_TestResult $result = NULL, $options = array())
139     {
140         if (!class_exists('PEAR_RunTest', FALSE)) {
141             throw new RuntimeException('Class PEAR_RunTest not found.');
142         }
143
144         if (isset($GLOBALS['_PEAR_destructor_object_list']) &&
145             is_array($GLOBALS['_PEAR_destructor_object_list']) &&
146             !empty($GLOBALS['_PEAR_destructor_object_list'])) {
147             $pearDestructorObjectListCount = count($GLOBALS['_PEAR_destructor_object_list']);
148         } else {
149             $pearDestructorObjectListCount = 0;
150         }
151
152         if ($result === NULL) {
153             $result = new PHPUnit_Framework_TestResult;
154         }
155
156         if (!is_array($options)) {
157             throw new InvalidArgumentException;
158         }
159
160         $coverage = $result->getCollectCodeCoverageInformation();
161         $options  = array_merge($options, $this->options);
162
163         if (!isset($options['include_path'])) {
164             $options['include_path'] = get_include_path();
165         }
166
167         if ($coverage) {
168             $options['coverage'] = TRUE;
169         } else {
170             $options['coverage'] = FALSE;
171         }
172
173         $currentErrorReporting = error_reporting(E_ERROR | E_WARNING | E_PARSE);
174         $runner                = new PEAR_RunTest(new PHPUnit_Extensions_PhptTestCase_Logger, $options);
175
176         if ($coverage) {
177             $runner->xdebug_loaded = TRUE;
178         } else {
179             $runner->xdebug_loaded = FALSE;
180         }
181
182         $result->startTest($this);
183
184         PHPUnit_Util_Timer::start();
185         $buffer       = $runner->run($this->filename, $options);
186         $time         = PHPUnit_Util_Timer::stop();
187         error_reporting($currentErrorReporting);
188         $base         = basename($this->filename);
189         $path         = dirname($this->filename);
190         $coverageFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.xdebug', $base);
191         $diffFile     = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.diff', $base);
192         $expFile      = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.exp', $base);
193         $logFile      = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.log', $base);
194         $outFile      = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.out', $base);
195         $phpFile      = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.php', $base);
196
197         if (is_object($buffer) && $buffer instanceof PEAR_Error) {
198             $result->addError(
199               $this,
200               new RuntimeException($buffer->getMessage()),
201               $time
202             );
203         }
204
205         else if ($buffer == 'SKIPPED') {
206             $result->addFailure($this, new PHPUnit_Framework_SkippedTestError, 0);
207         }
208
209         else if ($buffer != 'PASSED') {
210             $result->addFailure(
211               $this,
212               PHPUnit_Framework_ComparisonFailure::diffEqual(
213                 file_get_contents($expFile),
214                 file_get_contents($outFile)
215               ),
216               $time
217             );
218         }
219
220         foreach (array($diffFile, $expFile, $logFile, $phpFile, $outFile) as $file) {
221             if (file_exists($file)) {
222                 unlink($file);
223             }
224         }
225
226         if ($coverage && file_exists($coverageFile)) {
227             eval('$coverageData = ' . file_get_contents($coverageFile) . ';');
228             unset($coverageData[$phpFile]);
229
230             $result->appendCodeCoverageInformation($this, $coverageData);
231             unlink($coverageFile);
232         }
233
234         $result->endTest($this, $time);
235
236         // Do not invoke PEAR's destructor mechanism for PHP 4
237         // as it raises an E_STRICT.
238         if ($pearDestructorObjectListCount == 0) {
239             unset($GLOBALS['_PEAR_destructor_object_list']);
240         } else {
241             $count = count($GLOBALS['_PEAR_destructor_object_list']) - $pearDestructorObjectListCount;
242
243             for ($i = 0; $i < $count; $i++) {
244                 array_pop($GLOBALS['_PEAR_destructor_object_list']);
245             }
246         }
247
248         return $result;
249     }
250
251     /**
252      * Returns the name of the test case.
253      *
254      * @return string
255      */
256     public function getName()
257     {
258         return $this->toString();
259     }
260
261     /**
262      * Returns a string representation of the test case.
263      *
264      * @return string
265      */
266     public function toString()
267     {
268         return $this->filename;
269     }
270 }
271 ?>