5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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
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);
52 PHPUnit_Util_Filesystem::collectEndAndAddToBlacklist();
55 require_once 'PHP/CodeCoverage.php';
56 require_once 'PHP/Timer.php';
59 * Wrapper to run .phpt test cases.
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.14
67 * @link http://www.phpunit.de/
68 * @since Class available since Release 3.1.4
70 class PHPUnit_Extensions_PhptTestCase implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
73 * The filename of the .phpt file.
80 * Options for PEAR_RunTest.
84 protected $options = array();
87 * Constructs a test case with the given filename.
89 * @param string $filename
90 * @param array $options
92 public function __construct($filename, array $options = array())
94 if (!is_string($filename)) {
95 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
98 if (!is_file($filename)) {
99 throw new PHPUnit_Framework_Exception(
101 'File "%s" does not exist.',
107 $this->filename = $filename;
108 $this->options = $options;
112 * Counts the number of test cases executed by run(TestResult result).
116 public function count()
122 * Runs a test and collects its result in a TestResult instance.
124 * @param PHPUnit_Framework_TestResult $result
125 * @param array $options
126 * @return PHPUnit_Framework_TestResult
128 public function run(PHPUnit_Framework_TestResult $result = NULL, array $options = array())
130 if (!class_exists('PEAR_RunTest', FALSE)) {
131 throw new PHPUnit_Framework_Exception('Class PEAR_RunTest not found.');
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']);
139 $pearDestructorObjectListCount = 0;
142 if ($result === NULL) {
143 $result = new PHPUnit_Framework_TestResult;
146 $coverage = $result->getCollectCodeCoverageInformation();
147 $options = array_merge($options, $this->options);
149 if (!isset($options['include_path'])) {
150 $options['include_path'] = get_include_path();
154 $options['coverage'] = TRUE;
156 $options['coverage'] = FALSE;
159 $currentErrorReporting = error_reporting(E_ERROR | E_WARNING | E_PARSE);
160 $runner = new PEAR_RunTest(new PHPUnit_Extensions_PhptTestCase_Logger, $options);
163 $runner->xdebug_loaded = TRUE;
165 $runner->xdebug_loaded = FALSE;
168 $result->startTest($this);
172 $buffer = $runner->run($this->filename, $options);
173 $time = PHP_Timer::stop();
175 error_reporting($currentErrorReporting);
177 $base = basename($this->filename);
178 $path = dirname($this->filename);
179 $coverageFile = $path . DIRECTORY_SEPARATOR . str_replace(
180 '.phpt', '.xdebug', $base
182 $diffFile = $path . DIRECTORY_SEPARATOR . str_replace(
183 '.phpt', '.diff', $base
185 $expFile = $path . DIRECTORY_SEPARATOR . str_replace(
186 '.phpt', '.exp', $base
188 $logFile = $path . DIRECTORY_SEPARATOR . str_replace(
189 '.phpt', '.log', $base
191 $outFile = $path . DIRECTORY_SEPARATOR . str_replace(
192 '.phpt', '.out', $base
194 $phpFile = $path . DIRECTORY_SEPARATOR . str_replace(
195 '.phpt', '.php', $base
198 if (file_exists($phpFile)) {
199 PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(
204 if (is_object($buffer) && $buffer instanceof PEAR_Error) {
207 new RuntimeException($buffer->getMessage()),
212 else if ($buffer == 'SKIPPED') {
213 $result->addFailure($this, new PHPUnit_Framework_SkippedTestError, 0);
216 else if ($buffer != 'PASSED') {
219 PHPUnit_Framework_ComparisonFailure::diffEqual(
220 file_get_contents($expFile),
221 file_get_contents($outFile)
227 foreach (array($diffFile, $expFile, $logFile, $phpFile, $outFile) as $file) {
228 if (file_exists($file)) {
233 if ($coverage && file_exists($coverageFile)) {
234 eval('$coverageData = ' . file_get_contents($coverageFile) . ';');
235 unset($coverageData[$phpFile]);
237 $result->getCodeCoverage()->append($coverageData, $this);
238 unlink($coverageFile);
241 $result->endTest($this, $time);
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']);
248 $count = count($GLOBALS['_PEAR_destructor_object_list']) - $pearDestructorObjectListCount;
250 for ($i = 0; $i < $count; $i++) {
251 array_pop($GLOBALS['_PEAR_destructor_object_list']);
259 * Returns the name of the test case.
263 public function getName()
265 return $this->toString();
269 * Returns a string representation of the test case.
273 public function toString()
275 return $this->filename;