Example #1
0
 function teardown()
 {
     global $conf;
     // restore purge
     if (is_null($this->purge)) {
         unset($_REQUEST['purge']);
     } else {
         $_REQUEST['purge'] = $this->purge;
     }
     // restore $conf['cachedir'] if necessary
     if (!$this->cachedir) {
         unset($conf['cachedir']);
     }
     // restore io_functions
     runkit_function_rename('io_makefiledir', 'xhtml_htmlphp_test_io_makefiledir');
     if (function_exists('io_makefiledir_real')) {
         runkit_function_rename('io_makefiledir_real', 'io_makefiledir');
     }
     runkit_function_rename('io_savefile', 'xhtml_htmlphp_test_io_savefile');
     if (function_exists('io_savefile_real')) {
         runkit_function_rename('io_savefile_real', 'io_savefile');
     }
     // restore GeSHi::parse_code
     runkit_method_remove('GeSHi', 'parse_code');
     runkit_method_rename('GeSHi', 'parse_code_real', 'parse_code');
     parent::setup();
 }
Example #2
0
 /**
  * Modifies composer class loader.
  */
 public function activate()
 {
     class_exists('Composer\\Autoload\\ClassLoader');
     if (method_exists('Composer\\Autoload\\ClassLoader', self::ORIG_LOAD_CLASS)) {
         return;
     }
     runkit_method_rename('Composer\\Autoload\\ClassLoader', 'loadClass', self::ORIG_LOAD_CLASS);
     runkit_method_copy('Composer\\Autoload\\ClassLoader', 'loadClass', 'Guise\\ClassLoader');
 }
Example #3
0
 /**
  * Reverse a previously applied mutation to the given file
  *
  * @param array $mutation
  */
 public function reverseMutation(array $mutation)
 {
     if (runkit_method_remove($mutation['class'], $mutation['method']) == false) {
         throw new \Exception('runkit_method_remove() failed attempting to remove ' . $mutation['class'] . '::' . $mutation['method']);
     }
     if (runkit_method_rename($mutation['class'], $mutation['method'] . $this->_methodPreserveCode, $mutation['method']) == false) {
         throw new \Exception('runkit_method_rename() failed renaming from ' . $mutation['class'] . '::' . $mutation['method'] . $this->_methodPreserveCode . ' to ' . $mutation['class'] . '::' . $mutation['method'] . ' (mutation reversal)');
     }
 }
 /**
  * Clean-up function.
  *
  * Removes mocked method and restores the original was there is any.
  * Also removes the reference to the object from self::$instances.
  */
 public function restore()
 {
     if ($this->active) {
         list($class, $method) = $this->getClassAndMethod();
         runkit_method_remove($class, $method);
         runkit_method_rename($class, $this->restore_name, $method);
         $this->active = false;
     }
     parent::restore();
 }
Example #5
0
 /**
  * Reverse a previously applied mutation to the given file
  *
  * @param MutantInterface $mutant
  *
  * @throws \Exception
  */
 public function reverseMutation(MutantInterface $mutant)
 {
     $class = $mutant->getClassName();
     $method = $mutant->getMethodName();
     if (runkit_method_remove($class, $method) == false) {
         throw new \Exception('runkit_method_remove() failed attempting to remove ' . $class . '::' . $method);
     }
     if (runkit_method_rename($class, $method . $this->_methodPreserveCode, $method) == false) {
         throw new \Exception('runkit_method_rename() failed renaming from ' . $class . '::' . $method . $this->_methodPreserveCode . ' to ' . $class . '::' . $method . ' (mutation reversal)');
     }
 }
Example #6
0
 /**
  * Restore
  *
  * @return \MockFunction
  */
 public function restore()
 {
     if (!$this->_mock) {
         return $this;
     }
     $this->_mock = false;
     foreach ($this->_methods as $method) {
         runkit_method_remove($this->_class_name, $method);
         runkit_method_rename($this->_class_name, "_origin_" . $method, $method);
     }
     return $this;
 }
Example #7
0
 public static function hook($name)
 {
     runkit_method_rename(get_called_class(), $name, "_hook_{$name}");
 }
Example #8
0
 /**
  * @param string $className
  * @param string $name
  * @param string $newName
  *
  * @return boolean
  */
 public function renameMethod($className, $name, $newName)
 {
     if (!function_exists('runkit_method_rename')) {
         return false;
     }
     return runkit_method_rename($className, $name, $newName);
 }
Example #9
0
 /**
  * Weaves the advice handler into to class/method.
  *
  * @param   \ReflectionMethod   $method
  */
 protected function weaveHandler(\ReflectionMethod $method)
 {
     $class = $method->getDeclaringClass();
     $origMethod = $method->getName();
     $wovenMethod = sprintf('__guise_method__%s', $method->getName());
     if ($class->hasMethod($wovenMethod)) {
         return;
     }
     class_exists('Guise\\Weaver\\Handler');
     runkit_method_rename($class->getName(), $method->getName(), $wovenMethod);
     runkit_method_copy($class->getName(), $origMethod, 'Guise\\Weaver\\Handler', 'handle');
 }
Example #10
0
 public function generate()
 {
     $classes = func_get_args();
     foreach ($classes as $className) {
         $rc = new ReflectionClass($className);
         $methods = $rc->getMethods();
         $staticMethods = array();
         foreach ($methods as $method) {
             if ($method->isStatic()) {
                 $parameters = $method->getParameters();
                 $params = array();
                 $paramNames = array();
                 foreach ($parameters as $param) {
                     $paramName = "\${$param->getName()}";
                     $paramNames[] = $paramName;
                     $code = "{$paramName}";
                     if ($param->isDefaultValueAvailable()) {
                         $defaultValue = $param->getDefaultValue();
                         if (is_array($defaultValue)) {
                             $defaultValue = var_export($defaultValue, true);
                         }
                         $code .= " = {$defaultValue}";
                     }
                     $params[] = $code;
                 }
                 $code = sprintf("public static function %s(%s) {\nif(self::hasStub('%s')) {\n\$return = self::callStub('%s', array(%s)); if(\$return instanceof StubReturns) { return \$return->getValue(); } }\nreturn %s::__%s(%s); }", $method->getName(), join(', ', $params), $method->getName(), $method->getName(), join(', ', $paramNames), $className, $method->getName(), join(', ', $paramNames));
                 //echo $code;
                 //exit;
                 $staticMethods[] = $code;
             }
             runkit_method_rename($className, $method->getName(), sprintf('__%s', $method->getName()));
         }
         $code = sprintf('class Mock%s extends Mock {
             %s
         }', $className, join(PHP_EOL, $staticMethods));
         eval($code);
         runkit_class_adopt($className, "Mock{$className}");
     }
 }
Example #11
0
 /**
  * Revert redefined constructor
  *
  * @param string $class
  */
 public function revertRedefinedConstructor($class)
 {
     $this->loadClass($class);
     $backupMethod = $this->getBackupName(self::CONSTRUCTOR);
     try {
         $this->checkMethodDefined($class, $backupMethod);
     } catch (PStub_RunkitAdapter_Exception $e) {
         throw new PStub_RunkitAdapter_Exception('Constructor of the class ' . $class . ' was never redefined');
     }
     $tempMethod = $this->getTempName(self::CONSTRUCTOR);
     runkit_method_rename($class, self::CONSTRUCTOR, $tempMethod);
     runkit_method_rename($class, $backupMethod, self::CONSTRUCTOR);
     runkit_method_remove($class, $tempMethod);
 }
Example #12
0
 public static function restoreMethod($className, $methodName)
 {
     yTest_debugCC("restoreMethod {$className}::{$methodName}");
     $res = runkit_method_remove($className, strtolower($methodName));
     yTest_assert($res);
     $res = runkit_method_rename($className, strtolower(self::getOriginalMethodName($methodName)), strtolower($methodName));
     yTest_assert($res);
     //var_dump(get_class_methods($className));
 }
Example #13
0
 /**
  * Unmock a method.
  *
  * @param Callable $method Method defined in an array form
  *
  * @return void
  */
 protected function unmock_method($method)
 {
     $class_name = is_object($method[0]) ? get_class($method[0]) : $method[0];
     $method_name = $method[1];
     runkit_method_remove($class_name, $method_name);
     runkit_method_rename($class_name, $method_name . self::FUNCTION_ID, $method_name);
 }
Example #14
0
 /**
  *	Reset original definition.
  */
 public static function resetMethod($class, $method)
 {
     $originalMethod = "__original_{$method}";
     if (method_exists($class, $originalMethod)) {
         runkit_method_remove($class, $method);
         runkit_method_rename($class, $originalMethod, $method);
     }
 }
 /**
  *
  * Remove the information about the pseudo implementation of the method
  *
  * @param string $method_name
  * @return $this
  */
 public function removeMethod($method_name)
 {
     if ($this->stashedMethodExists($method_name)) {
         runkit_method_remove($this->class_name, $method_name);
         runkit_method_rename($this->class_name, $this->getStashedMethodName($method_name), $method_name);
     }
     unset($this->methods[$method_name]);
     return $this;
 }
 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;
     }
 }
Example #17
0
<?php

class Example
{
    function foo()
    {
        return "foo!\n";
    }
}
// Rename the 'foo' method to 'bar'
runkit_method_rename('Example', 'foo', 'bar');
// output renamed function
echo Example::bar();