Source for file Default.php
Documentation is available at Default.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* This file is part of the PEAR Testing_DocTest package.
* LICENSE: This source file is subject to the MIT license that is available
* through the world-wide-web at the following URI:
* http://opensource.org/licenses/mit-license.php
* @package Testing_DocTest
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2008 David JEAN LOUIS
* @license http://opensource.org/licenses/mit-license.php MIT License
* @link http://pear.php.net/package/Testing_DocTest
* @since File available since release 0.1.0
require_once 'Testing/DocTest/RunnerInterface.php';
* DocTest Runner default class.
* require_once 'Testing/DocTest.php';
* require_once 'Testing/DocTest/TestCase.php';
* $test = new Testing_DocTest_TestCase();
* $test->code = 'echo "Foobar!";';
* $test->expectedValue = ' foobar !';
* $r = new Testing_DocTest_Runner_Default();
* var_dump($test->state === Testing_DocTest_TestCase::STATE_PASSED);
* $test->flags |= Testing_DocTest::FLAG_NORMALIZE_WHITESPACE;
* var_dump($test->state === Testing_DocTest_TestCase::STATE_PASSED);
* $test->flags |= Testing_DocTest::FLAG_CASE_INSENSITIVE;
* var_dump($test->state === Testing_DocTest_TestCase::STATE_PASSED);
* $test->expectedValue = ' f[...]bar !';
* $test->flags |= Testing_DocTest::FLAG_ELLIPSIS;
* var_dump($test->state === Testing_DocTest_TestCase::STATE_PASSED);
* $test->flags |= Testing_DocTest::FLAG_SKIP;
* var_dump($test->state === Testing_DocTest_TestCase::STATE_SKIPPED);
* require_once 'Testing/DocTest.php';
* require_once 'Testing/DocTest/TestCase.php';
* $test = new Testing_DocTest_TestCase();
* $test->code = 'echo nonExistantFunc();';
* $test->expectedValue = 'foo';
* $runner = new Testing_DocTest_Runner_Default();
* var_dump($test->actualValue);
* // string([...]) "[...]Fatal error: Call to undefined function [...]"
* @package Testing_DocTest
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2008 David JEAN LOUIS
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Testing_DocTest
* @since Class available since release 0.1.0
* Run the test provided by comparing the expected result with the actual
* result returned by the test code.
* Each test is run in its own php process.
* @param object $testCase Testing_DocTest_TestCase instance to run
* @throws Testing_DocTest_Exception
public function run(Testing_DocTest_TestCase $testCase)
if ($testCase->parsingError ) {
$codetpl = "<?php\n%s\n";
if ($testCase->file !== null ) {
$codetpl .= " require_once '{$testCase->file }';\n";
if ($testCase->tmplCode ) {
$tmplfile= $testCase->tmplCode;
} elseif (isset ($testCase->_shellOptions ['template_code'])
&& $testCase->_shellOptions ['template_code']
$tmplfile= $testCase->_shellOptions ['template_code'];
if (($skipCode = $testCase->skipIfCode ) !== null ) {
$skipCode = trim($skipCode);
$ret = $this->_exec (sprintf('<?php echo %s; ?>', $skipCode));
if ($ret['code'] == ! 0 || strlen($ret['output']) > 1 ) {
'skip-condition in test "'. $testCase->name .
'" must be a boolean expression, '. 'got: ' . $skipCode
$skip = $ret['code'] === 0 && trim($ret['output']) === '1';
if (!empty ($testCase->iniSettings )) {
$options = $this->_formatIniSettings ($testCase->iniSettings );
sprintf($codetpl, $testCase->setupCode , $testCase->code ),
if ($ret['code'] !== 0 ) {
$testCase->actualValue = trim($ret['output']);
$testCase->actualValue = $ret['output'];
if ($this->_compare ($testCase)) {
if ($ret['code'] !== 0 ) {
if (($cleanCode = $testCase->cleanCode ) !== null ) {
$cleanCode = trim($cleanCode);
$ret = $this->_exec (sprintf('<?php %s; ?>', $cleanCode));
if ($ret['code'] == ! 0 ) {
'cleaning code failed in ' . 'test "' .
$testCase->name . '": ' . $cleanCode
// _formatIniSettings() {{{
* Given an array of directive=>value this method return the commandline
* string to pass to the php interpreter.
* @param array $iniSettings an array of ini settings
private function _formatIniSettings (array $iniSettings)
foreach ($iniSettings as $k=> $v) {
if (substr(PHP_OS , 0 , 3 ) == 'WIN') {
// XXX check why windows does not like escapeshellarg
$ret .= ' -d' . $k . '=' . $v;
* Compare the expected result with the actual result.
* @param object $test a Testing_DocTest_TestCase instance
private function _compare (Testing_DocTest_TestCase $test)
$exp = trim($test->expectedValue , "\n");
$act = trim($test->actualValue , "\n");
array ("\n", "\r\n", '[...]'),
array ('', '', '__ellipsis__'), $exp
$act = str_replace(array ("\n", "\r\n", '[...]'), '', $act);
* Run given php code in a subprocess and return an array as follows:
* 'code' => true|false, // return code of the process
* 'output' => 'string' // output of the process (stdout or stderr)
* @param string $code the php code to execute
* @param string $options additionnal options to pass to php
* @param Testing_DocTest_TestCase $testCase aa
* @throws Testing_DocTest_Exception if the process cannot be opened
Testing_DocTest_TestCase $testCase=null
if (isset ($testCase, $testCase->_shellOptions ['php_wrapper'])
&& $testCase->_shellOptions ['php_wrapper']
//Needed for general framework setup
$php = $testCase->_shellOptions ['php_wrapper'];
$php = substr('@php_bin@', 0 , 1 ) == '@' ? 'php ' : 'php';
if (substr(PHP_OS , 0 , 3 ) == 'WIN') {
0 => array ('pipe', 'r'), // stdin
1 => array ('pipe', 'w'), // stdout
2 => array ('pipe', 'w') // stderr
// try to open proc and raise an exception if it fails
$process = proc_open($php, $descriptors, $pipes);
// will contain script output
// hide errors from interrupted syscalls
$output .= "\n ** ERROR: process timed out **\n";
if (false === ($data = fgets($pipes[1 ]))) {
// nothing on stdout, try stderr
//if (false === ($data = fgets($pipes[2]))) {
// close stdout and stderr
// get return code by closing the process
return array ('code' => proc_close($process), 'output' => $output);
Documentation generated on Mon, 11 Mar 2019 15:52:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|