예제 #1
0
파일: Executor.php 프로젝트: oncesk/runkit
 /**
  * @param RunkitFunction $function
  *
  * @return boolean
  */
 public function addFunction(\Runkit\RunkitFunction $function)
 {
     if (!function_exists('runkit_function_add')) {
         return false;
     }
     return runkit_function_add($function->getName(), (string) $function->getArguments(), $function->getCode()->get());
 }
예제 #2
0
 /**
  *	Reimplements the given function.
  */
 public static function reimplementFunction($function, $arguments, $code)
 {
     $originalFunction = "__original_{$function}";
     if (!function_exists($originalFunction)) {
         runkit_function_rename($function, $originalFunction);
     }
     runkit_function_add($function, $arguments, $code);
 }
예제 #3
0
 /**
  * Enable
  *
  * @return \MockFunction
  */
 public function enable()
 {
     if (!$this->active) {
         $this->active = true;
         runkit_function_rename($this->functionName, $this->functionAlias);
         runkit_function_add($this->functionName, '', $this->getMockCode());
     }
     return $this;
 }
예제 #4
0
 /**
  * Mock
  *
  * @return \MockFunction
  */
 public function mock()
 {
     if ($this->_mock) {
         return $this;
     }
     $this->_mock = true;
     runkit_function_rename($this->_function_name, $this->_function_alias);
     runkit_function_add($this->_function_name, '', $this->getMockCode());
     return $this;
 }
예제 #5
0
 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());
 }
예제 #6
0
 protected function updateState($type, $id, $state)
 {
     $parts = explode('|', $id);
     switch ($type) {
         case self::STATIC_METHOD:
         case self::DYNAMIC_METHOD:
             $className = $parts[1];
             $methodName = $parts[2];
             $savedName = self::SAVED_PREFIX . $methodName;
             if ($state) {
                 // enable
                 is_callable("{$className}::{$methodName}");
                 $flags = RUNKIT_ACC_PUBLIC | ($type == self::STATIC_METHOD ? RUNKIT_ACC_STATIC : 0);
                 runkit_method_rename($className, $methodName, $savedName);
                 // save the original method
                 runkit_method_add($className, $methodName, '', $this->getExecuteCallCode($type, $id, $type === self::DYNAMIC_METHOD), $flags);
             } else {
                 // diable
                 runkit_method_remove($className, $methodName);
                 // remove the redefined instance
                 runkit_method_rename($className, $savedName, $methodName);
                 // restore the original
             }
             break;
         case self::GLOBAL_FUNCTION:
             $functionName = $parts[1];
             list($namespace, $baseName) = self::parseGlobalFunctionName($functionName);
             $functionName = $namespace . $baseName;
             $savedName = $namespace . self::SAVED_PREFIX . $baseName;
             if ($state) {
                 // enable
                 $tempName = "WikiaMockProxyTempFuncName";
                 // workaround for namespaces functions
                 runkit_function_rename($functionName, $savedName);
                 runkit_function_add($tempName, '', $this->getExecuteCallCode($type, $id));
                 runkit_function_rename($tempName, $functionName);
             } else {
                 // disable
                 runkit_function_remove($functionName);
                 // remove the redefined instance
                 runkit_function_rename($savedName, $functionName);
                 // restore the original
             }
             break;
     }
 }
예제 #7
0
        // replace strtotime()
        runkit_function_rename("strtotime", "original_strtotime");
        runkit_function_add("strtotime", '$time, $now = 0', <<<'FUNC'
	        if (isset($now)
	                && !empty($now))
	            return original_strtotime($time, $now);
	        else
	            return original_strtotime($time, time());
FUNC
);
        // replace date()
        runkit_function_rename("date", "original_date");
        runkit_function_add("date", '$format, $timestamp = 0', <<<'FUNC'
	        if (isset($timestamp)
	                && !empty($timestamp))
	            return original_date($format, $timestamp);
	        else
	            return original_date($format, time());
FUNC
);
        return true;
    };
    if (extension_loaded("uopz")) {
        ini_set("uopz.overloads", 1);
        return $travelByUopz;
    } else {
        if (extension_loaded("runkit")) {
            ini_set("runkit.internal_override", 1);
            return $travelByRunkit;
        } else {
            return $noop;
        }
예제 #8
0
 /**
  * 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;
 }
예제 #9
0
 public function mockSCC()
 {
     runkit_function_rename('stream_context_create', 'TEST_SAVED_stream_context_create');
     runkit_function_add('stream_context_create', 'Array $options = [], Array $params = []', 'EWay_Test\\Request\\AccessCodeTest::$sccOptions = $options;' . 'return EWay_Test\\Request\\AccessCodeTest::$sccReturn;');
 }
예제 #10
0
 /**
  * Redefine function in user space
  *
  * @param string $function
  * @param array $args
  * @param string $body
  */
 public function redefineFunction($function, $args, $body)
 {
     $this->checkFunctionExists($function);
     $backupFunction = $this->getBackupName($function);
     try {
         $this->checkFunctionDoesNotExist($backupFunction);
     } catch (PStub_RunkitAdapter_Exception $e) {
         throw new PStub_RunkitAdapter_Exception('Function ' . $function . ' can not be redefined more than once');
     }
     runkit_function_rename($function, $backupFunction);
     runkit_function_add($function, $args, $body);
 }
예제 #11
0
 public static function replaceFunction($functionName, $args, $code)
 {
     yTest_debugCC("replaceFunction {$functionName}({$args}) with code:\n" . $code);
     // [RS] runkit will crash if you try to call runkit_function_rename()
     $res = runkit_function_copy($functionName, strtolower(self::getOriginalFunctionName($functionName)));
     yTest_assert($res);
     $res = runkit_function_remove($functionName);
     yTest_assert($res);
     $res = runkit_function_add(strtolower($functionName), $args, $code);
     yTest_assert($res);
 }