3
* This file is part of PHPUnit.
5
* (c) Sebastian Bergmann <sebastian@phpunit.de>
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
12
* Runner for PHPT test cases.
15
* @subpackage Extensions_PhptTestCase
16
* @author Sebastian Bergmann <sebastian@phpunit.de>
17
* @copyright Sebastian Bergmann <sebastian@phpunit.de>
18
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
19
* @link http://www.phpunit.de/
20
* @since Class available since Release 3.1.4
22
class PHPUnit_Extensions_PhptTestCase implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
32
private $settings = array(
40
'error_append_string=',
41
'error_prepend_string=',
45
'magic_quotes_runtime=0',
48
'output_buffering=Off',
50
'report_zend_debug=0',
53
'xdebug.default_enable=0'
57
* Constructs a test case with the given filename.
59
* @param string $filename
60
* @throws PHPUnit_Framework_Exception
62
public function __construct($filename)
64
if (!is_string($filename)) {
65
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
68
if (!is_file($filename)) {
69
throw new PHPUnit_Framework_Exception(
71
'File "%s" does not exist.',
77
$this->filename = $filename;
81
* Counts the number of test cases executed by run(TestResult result).
85
public function count()
91
* Runs a test and collects its result in a TestResult instance.
93
* @param PHPUnit_Framework_TestResult $result
94
* @return PHPUnit_Framework_TestResult
96
public function run(PHPUnit_Framework_TestResult $result = null)
98
$sections = $this->parse();
99
$code = $this->render($sections['FILE']);
101
if ($result === null) {
102
$result = new PHPUnit_Framework_TestResult;
105
$php = PHPUnit_Util_PHP::factory();
109
$result->startTest($this);
111
if (isset($sections['SKIPIF'])) {
112
$jobResult = $php->runJob($sections['SKIPIF'], $this->settings);
114
if (!strncasecmp('skip', ltrim($jobResult['stdout']), 4)) {
115
if (preg_match('/^\s*skip\s*(.+)\s*/i', $jobResult['stdout'], $message)) {
116
$message = substr($message[1], 2);
121
$result->addFailure($this, new PHPUnit_Framework_SkippedTestError($message), 0);
129
$jobResult = $php->runJob($code, $this->settings);
130
$time = PHP_Timer::stop();
132
if (isset($sections['EXPECT'])) {
133
$assertion = 'assertEquals';
134
$expected = $sections['EXPECT'];
136
$assertion = 'assertStringMatchesFormat';
137
$expected = $sections['EXPECTF'];
140
$output = preg_replace('/\r\n/', "\n", trim($jobResult['stdout']));
141
$expected = preg_replace('/\r\n/', "\n", trim($expected));
144
PHPUnit_Framework_Assert::$assertion($expected, $output);
145
} catch (PHPUnit_Framework_AssertionFailedError $e) {
146
$result->addFailure($this, $e, $time);
147
} catch (Exception $e) {
148
$result->addError($this, $e, $time);
152
$result->endTest($this, $time);
158
* Returns the name of the test case.
162
public function getName()
164
return $this->toString();
168
* Returns a string representation of the test case.
172
public function toString()
174
return $this->filename;
179
* @throws PHPUnit_Framework_Exception
181
private function parse()
186
foreach (file($this->filename) as $line) {
187
if (preg_match('/^--([_A-Z]+)--/', $line, $result)) {
188
$section = $result[1];
189
$sections[$section] = '';
191
} elseif (empty($section)) {
192
throw new PHPUnit_Framework_Exception('Invalid PHPT file');
195
$sections[$section] .= $line;
198
if (!isset($sections['FILE']) ||
199
(!isset($sections['EXPECT']) && !isset($sections['EXPECTF']))) {
200
throw new PHPUnit_Framework_Exception('Invalid PHPT file');
207
* @param string $code
210
private function render($code)
218
"'" . dirname($this->filename) . "'",
219
"'" . $this->filename . "'"