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.
14 * @since Class available since Release 3.1.4
16 class PHPUnit_Extensions_PhptTestCase implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
26 private $settings = array(
34 'error_append_string=',
35 'error_prepend_string=',
39 'magic_quotes_runtime=0',
42 'output_buffering=Off',
44 'report_zend_debug=0',
47 'xdebug.default_enable=0'
51 * Constructs a test case with the given filename.
53 * @param string $filename
55 * @throws PHPUnit_Framework_Exception
57 public function __construct($filename)
59 if (!is_string($filename)) {
60 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
63 if (!is_file($filename)) {
64 throw new PHPUnit_Framework_Exception(
66 'File "%s" does not exist.',
72 $this->filename = $filename;
76 * Counts the number of test cases executed by run(TestResult result).
80 public function count()
86 * Runs a test and collects its result in a TestResult instance.
88 * @param PHPUnit_Framework_TestResult $result
90 * @return PHPUnit_Framework_TestResult
92 public function run(PHPUnit_Framework_TestResult $result = null)
94 $sections = $this->parse();
95 $code = $this->render($sections['FILE']);
97 if ($result === null) {
98 $result = new PHPUnit_Framework_TestResult;
101 $php = PHPUnit_Util_PHP::factory();
104 $settings = $this->settings;
106 $result->startTest($this);
108 if (isset($sections['INI'])) {
109 $settings = array_merge($settings, $this->parseIniSection($sections['INI']));
112 if (isset($sections['SKIPIF'])) {
113 $jobResult = $php->runJob($sections['SKIPIF'], $settings);
115 if (!strncasecmp('skip', ltrim($jobResult['stdout']), 4)) {
116 if (preg_match('/^\s*skip\s*(.+)\s*/i', $jobResult['stdout'], $message)) {
117 $message = substr($message[1], 2);
122 $result->addFailure($this, new PHPUnit_Framework_SkippedTestError($message), 0);
130 $jobResult = $php->runJob($code, $settings);
131 $time = PHP_Timer::stop();
133 if (isset($sections['EXPECT'])) {
134 $assertion = 'assertEquals';
135 $expected = $sections['EXPECT'];
137 $assertion = 'assertStringMatchesFormat';
138 $expected = $sections['EXPECTF'];
141 $output = preg_replace('/\r\n/', "\n", trim($jobResult['stdout']));
142 $expected = preg_replace('/\r\n/', "\n", trim($expected));
145 PHPUnit_Framework_Assert::$assertion($expected, $output);
146 } catch (PHPUnit_Framework_AssertionFailedError $e) {
147 $result->addFailure($this, $e, $time);
148 } catch (Throwable $t) {
149 $result->addError($this, $t, $time);
150 } catch (Exception $e) {
151 $result->addError($this, $e, $time);
155 $result->endTest($this, $time);
161 * Returns the name of the test case.
165 public function getName()
167 return $this->toString();
171 * Returns a string representation of the test case.
175 public function toString()
177 return $this->filename;
183 * @throws PHPUnit_Framework_Exception
185 private function parse()
190 foreach (file($this->filename) as $line) {
191 if (preg_match('/^--([_A-Z]+)--/', $line, $result)) {
192 $section = $result[1];
193 $sections[$section] = '';
195 } elseif (empty($section)) {
196 throw new PHPUnit_Framework_Exception('Invalid PHPT file');
199 $sections[$section] .= $line;
202 if (!isset($sections['FILE']) ||
203 (!isset($sections['EXPECT']) && !isset($sections['EXPECTF']))) {
204 throw new PHPUnit_Framework_Exception('Invalid PHPT file');
211 * @param string $code
215 private function render($code)
223 "'" . dirname($this->filename) . "'",
224 "'" . $this->filename . "'"
231 * Parse --INI-- section key value pairs and return as array.
237 protected function parseIniSection($content)
239 return preg_split('/\n|\r/', $content, -1, PREG_SPLIT_NO_EMPTY);