5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.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.
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
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.1.4
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);
53 foreach (PHPUnit_Util_Filesystem::collectEnd() as $blacklistedFile) {
54 PHPUnit_Util_Filter::addFileToFilter($blacklistedFile, 'PHPUNIT');
58 require_once 'PHPUnit/Framework.php';
59 require_once 'PHPUnit/Extensions/PhptTestCase/Logger.php';
60 require_once 'PHPUnit/Util/Filter.php';
62 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
65 * Wrapper to run .phpt test cases.
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
76 class PHPUnit_Extensions_PhptTestCase implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
79 * The filename of the .phpt file.
86 * Options for PEAR_RunTest.
90 protected $options = array();
93 * Constructs a test case with the given filename.
95 * @param string $filename
96 * @param array $options
98 public function __construct($filename, $options = array())
100 if (!is_string($filename)) {
101 throw new InvalidArgumentException;
104 if (!is_file($filename)) {
105 throw new RuntimeException(
107 'File "%s" does not exist.',
113 if (!is_array($options)) {
114 throw new InvalidArgumentException;
117 $this->filename = $filename;
118 $this->options = $options;
122 * Counts the number of test cases executed by run(TestResult result).
126 public function count()
132 * Runs a test and collects its result in a TestResult instance.
134 * @param PHPUnit_Framework_TestResult $result
135 * @param array $options
136 * @return PHPUnit_Framework_TestResult
138 public function run(PHPUnit_Framework_TestResult $result = NULL, $options = array())
140 if (!class_exists('PEAR_RunTest', FALSE)) {
141 throw new RuntimeException('Class PEAR_RunTest not found.');
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']);
149 $pearDestructorObjectListCount = 0;
152 if ($result === NULL) {
153 $result = new PHPUnit_Framework_TestResult;
156 if (!is_array($options)) {
157 throw new InvalidArgumentException;
160 $coverage = $result->getCollectCodeCoverageInformation();
161 $options = array_merge($options, $this->options);
163 if (!isset($options['include_path'])) {
164 $options['include_path'] = get_include_path();
168 $options['coverage'] = TRUE;
170 $options['coverage'] = FALSE;
173 $currentErrorReporting = error_reporting(E_ERROR | E_WARNING | E_PARSE);
174 $runner = new PEAR_RunTest(new PHPUnit_Extensions_PhptTestCase_Logger, $options);
177 $runner->xdebug_loaded = TRUE;
179 $runner->xdebug_loaded = FALSE;
182 $result->startTest($this);
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);
197 if (is_object($buffer) && $buffer instanceof PEAR_Error) {
200 new RuntimeException($buffer->getMessage()),
205 else if ($buffer == 'SKIPPED') {
206 $result->addFailure($this, new PHPUnit_Framework_SkippedTestError, 0);
209 else if ($buffer != 'PASSED') {
212 PHPUnit_Framework_ComparisonFailure::diffEqual(
213 file_get_contents($expFile),
214 file_get_contents($outFile)
220 foreach (array($diffFile, $expFile, $logFile, $phpFile, $outFile) as $file) {
221 if (file_exists($file)) {
226 if ($coverage && file_exists($coverageFile)) {
227 eval('$coverageData = ' . file_get_contents($coverageFile) . ';');
228 unset($coverageData[$phpFile]);
230 $result->appendCodeCoverageInformation($this, $coverageData);
231 unlink($coverageFile);
234 $result->endTest($this, $time);
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']);
241 $count = count($GLOBALS['_PEAR_destructor_object_list']) - $pearDestructorObjectListCount;
243 for ($i = 0; $i < $count; $i++) {
244 array_pop($GLOBALS['_PEAR_destructor_object_list']);
252 * Returns the name of the test case.
256 public function getName()
258 return $this->toString();
262 * Returns a string representation of the test case.
266 public function toString()
268 return $this->filename;