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