function logFunc($name) { // copy function to fuinction_o runkit_function_copy($name, $name . "_o"); // runkit_function_redefine($name,"","logger_simple(); if(!empty(getargs())) return {$name}_o(getargs()); else return {$name}_o();"); // redefine function and internally call the original function (function_o) runkit_function_redefine($name, "", "return logger();"); }
public static function timeInit() { if (!isset(self::$_timeStart)) { runkit_function_copy('time', 'time_original'); runkit_function_redefine('time', '', 'return CMTest_TH::time();'); } self::$_timeStart = time_original(); self::$timeDelta = 0; }
function _wpe_replace_function($name, $args) { if (function_exists($name)) { // is possible that it doesn't! Then don't emit errors. $call_args = preg_replace("#=[^,]+#", "", $args); runkit_function_copy($name, "_wpe_old_{$name}"); runkit_function_redefine($name, $args, "return _wpe_new_{$name}({$call_args});"); } }
private function replaceFunction() { $funcName = $this->funcName; $this->parameters = $this->getParameters($funcName); runkit_function_copy($funcName, $this->tmpFuncName); $result = runkit_function_redefine($this->funcName, $this->parameters, "return MockFunctionContainer::call('{$funcName}', func_get_args());"); if (!$result) { throw new Exception("Error trying to replace function"); } MockFunctionContainer::register($funcName, $this); }
/** * Revert previously overridden function * * @param string $func */ protected function runkitRevert($func) { $this->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$override_functions)) { throw new \RuntimeException("Function '{$func}' is not marked as mocked"); } unset(self::$override_functions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); }
/** * Overrides native PHP functions. * @return void */ protected function overrideNativeFuncs() { if (Daemon::supported(Daemon::SUPPORT_RUNKIT_INTERNAL_MODIFY)) { function define($k, $v) { if (defined($k)) { runkit_constant_redefine($k, $v); } else { runkit_constant_add($k, $v); } } $this->override('define'); function header() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return call_user_func_array([Daemon::$context, 'header'], func_get_args()); } $this->override('header'); function is_uploaded_file() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return call_user_func_array([Daemon::$context, 'isUploadedFile'], func_get_args()); } $this->override('is_uploaded_file'); function move_uploaded_file() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return call_user_func_array([Daemon::$context, 'moveUploadedFile'], func_get_args()); } $this->override('move_uploaded_file'); function headers_sent(&$file = null, &$line = null) { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return Daemon::$context->headers_sent($file, $line); } //$this->override('headers_sent'); function headers_list() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return Daemon::$context->headers_list(); } $this->override('headers_list'); function setcookie() { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return call_user_func_array([Daemon::$context, 'setcookie'], func_get_args()); } $this->override('setcookie'); /** * @param callable $cb */ function register_shutdown_function($cb) { if (!Daemon::$context instanceof \PHPDaemon\Request\Generic) { return false; } return Daemon::$context->registerShutdownFunction($cb); } $this->override('register_shutdown_function'); runkit_function_copy('create_function', 'create_function_native'); runkit_function_redefine('create_function', '$arg,$body', 'return \\PHPDaemon\\Core\\Daemon::$process->createFunction($arg,$body);'); } }
/** * Overrides native PHP functions. * @return void */ public function overrideNativeFuncs() { if (Daemon::supported(Daemon::SUPPORT_RUNKIT_INTERNAL_MODIFY)) { runkit_function_rename('header', 'header_native'); function header() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return call_user_func_array(array(Daemon::$req, 'header'), func_get_args()); } } runkit_function_rename('is_uploaded_file', 'is_uploaded_file_native'); function is_uploaded_file() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return call_user_func_array(array(Daemon::$req, 'isUploadedFile'), func_get_args()); } } runkit_function_rename('move_uploaded_file', 'move_uploaded_file_native'); function move_uploaded_file() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return call_user_func_array(array(Daemon::$req, 'moveUploadedFile'), func_get_args()); } } runkit_function_rename('headers_sent', 'headers_sent_native'); function headers_sent() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return Daemon::$req->headers_sent(); } } runkit_function_rename('headers_list', 'headers_list_native'); function headers_list() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return Daemon::$req->headers_list(); } } runkit_function_rename('setcookie', 'setcookie_native'); function setcookie() { if (Daemon::$req && Daemon::$req instanceof HTTPRequest) { return call_user_func_array(array(Daemon::$req, 'setcookie'), func_get_args()); } } register_shutdown_function(array($this, 'shutdown')); runkit_function_rename('register_shutdown_function', 'register_shutdown_function_native'); function register_shutdown_function($cb) { if (Daemon::$req) { return Daemon::$req->registerShutdownFunction($cb); } } runkit_function_copy('create_function', 'create_function_native'); runkit_function_redefine('create_function', '$arg,$body', 'return __create_function($arg,$body);'); function __create_function($arg, $body) { static $cache = array(); static $maxCacheSize = 64; static $sorter; if ($sorter === NULL) { $sorter = function ($a, $b) { if ($a->hits == $b->hits) { return 0; } return $a->hits < $b->hits ? 1 : -1; }; } $crc = crc32($arg . "" . $body); if (isset($cache[$crc])) { ++$cache[$crc][1]; return $cache[$crc][0]; } if (sizeof($cache) >= $maxCacheSize) { uasort($cache, $sorter); array_pop($cache); } $cache[$crc] = array($cb = eval('return function(' . $arg . '){' . $body . '};'), 0); return $cb; } } }
protected function _redirectCallToFunction() { $newOrigFuncName = $this->_functionName . $this->_origFuncSuffix; $spyFuncName = $this->_functionName . $this->_spyFuncSuffix; runkit_function_add($spyFuncName, '', $this->_getSpyFunctionBody()); if (!function_exists($newOrigFuncName)) { runkit_function_copy($this->_functionName, $newOrigFuncName); } //keep memory address of function to prevent seg fault runkit_function_redefine($this->_functionName, '', $this->_getReplaceFunctionBody()); }
/** * Removes the mock function implementation and restores * the previously declared implementation * @access public */ function restore() { runkit_function_remove($this->_function) or trigger_error('Error removing mock function', E_USER_ERROR); runkit_function_copy($this->_tmp_function, $this->_function) or trigger_error('Error restoring real function', E_USER_ERROR); runkit_function_remove($this->_tmp_function) or trigger_error('Error removing tmp function', E_USER_ERROR); }
/** * Creates runkit function to be used for mocking, taking care of callback to this object. * * Also temporary renames the original function if there is. */ protected function createFunction() { if (function_exists($this->function_name)) { $this->restore_name = 'restore_' . $this->function_name . '_' . $this->id . '_' . uniqid(); runkit_function_copy($this->function_name, $this->restore_name); runkit_function_redefine($this->function_name, '', $this->getCallback()); } else { runkit_function_add($this->function_name, '', $this->getCallback()); } $this->active = true; }
<?php function f() { echo sprintf("Using __FUNCTION__: %s() called<br>", __FUNCTION__); } runkit_function_copy('f', 'g'); f(); g();
throw new RuntimeException('Decoding failed: Maximum stack depth exceeded'); break; case JSON_ERROR_CTRL_CHAR: throw new RuntimeException('Decoding failed: Unexpected control character found'); break; case JSON_ERROR_SYNTAX: throw new RuntimeException('Decoding failed: Syntax error'); break; default: throw new RuntimeException('Decoding error message: ' . json_last_error_msg()); break; } return null; } } runkit_function_copy('json_encode', '__json_encode__'); runkit_function_remove('json_encode'); if (!(function_exists('json_encode') || is_callable('json_encode'))) { function json_encode($value, $options = JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) { $encode = __json_encode__($value, $options); switch (json_last_error()) { case JSON_ERROR_NONE: return $encode; break; case JSON_ERROR_DEPTH: throw new RuntimeException('Decoding failed: Maximum stack depth exceeded'); break; case JSON_ERROR_CTRL_CHAR: throw new RuntimeException('Decoding failed: Unexpected control character found'); break;
public static function restoreFunction($functionName) { yTest_debugCC("restoreFunction {$functionName}"); // [RS] runkit will crash if you try to call runkit_function_rename() $res = runkit_function_remove(strtolower($functionName)); yTest_assert($res); $res = runkit_function_copy(strtolower(self::getOriginalFunctionName($functionName)), strtolower($functionName)); yTest_assert($res); $res = runkit_function_remove(strtolower(self::getOriginalFunctionName($functionName))); yTest_assert($res); }
/** * Mock a PHP function. * * @param String $name Function name * @param String $mock Replacement code for the function * * @return void */ protected function mock_function($name, $mock) { if (function_exists($name . self::FUNCTION_ID) === FALSE) { runkit_function_copy($name, $name . self::FUNCTION_ID); } runkit_function_redefine($name, '', $mock); }