Setting up a mock response, function is called from mocked classes

@param $className
@param $instanceId
@param $methodName
@param $args
@param $action
@param $returns
511346
protected static function _setWhenMockResponse(
        $className,
        $instanceId,
        $methodName,
        $args,
        $action,
        $returns
    ) {
        $args = serialize($args);

        $returnValues = array();
        $returnValues[$className][$instanceId][$methodName][$args]['response'] = ['action' => $action, 'value' => $returns];

        self::_addChainedResponse($returnValues);
    }
1094872
protected function execute($instance, $action)
    {
        $app = $this;
        $wei = $this->wei;
        $middleware = $this->getMiddleware($instance, $action);

        $callback = function () use ($instance, $action, $app) {
            $instance->before($app->request, $app->response);

            $method = $app->getActionMethod($action);
            $response = $instance->$method($app->request, $app->response);

            $instance->after($app->request, $response);

            return $response;
        };

        $next = function () use (&$middleware, &$next, $callback, $wei) {
            $config = array_splice($middleware, 0, 1);
            if ($config) {
                $class = key($config);
                $service = new $class(array('wei' => $wei) + $config[$class]);
                $result = $service($next);
            } else {
                $result = $callback();
            }

            return $result;
        };

        return $this->handleResponse($next())->send();
    }
1288166
public static function invoke( $oInstance, $methodName, $args=null ){

		//TODO tratar los args.
		$reflectionMethod = new \ReflectionMethod( get_class( $oInstance ) , $methodName);
		$value = $reflectionMethod->invoke( $oInstance, $args );
			
		return $value;
	}
196803
private static void invokeMethod(final Object instance, final Method method, final Object[] args)
   {
      final boolean accessability = method.isAccessible();

      try
      {
         method.setAccessible(true);
         method.invoke(instance, args);
      }
      catch (Exception e)
      {
         InjectionException.rethrow(e);
      }
      finally
      {
         method.setAccessible(accessability);
      }
   }
952364
public static void callMethod(final Object obj, final String methodName, final Object... args) {
		try {
			callMethod(obj, methodName, false, args);
		} catch (InvocationTargetException e) {
			JK.throww(e);
		}
	}
1285640
private function callAction($actionReader, $apiRequest, $action, $args)
    {
        $actionExecutor = new ActionExecutor($actionReader);

        /** @var ActionResponseModel $actionResponse */
        $actionResponse = $actionExecutor->execute($apiRequest, $action, $args);

        $ack = new AckModel();

        $class = '';
        if (is_object($actionResponse->getResponse())) {
            $class = get_class($actionResponse->getResponse());
        }

        $ack
            ->setPkgUuid($actionResponse->getPkgUuid())
            ->setSuccess(true)
            ->setServerCode(200)
            ->setLocation($apiRequest->getPath())
            ->setLocationParams($actionResponse->getParams())
            ->setRequestArgs($actionResponse->getArgs())
            ->setPayloadClass($class)
            ->setPayload($actionResponse->getResponse());

        return $ack;
    }
1005001
public function newInstance($className, array $args = array())
    {
        return $this->getApplication()->search(ProviderInterface::IDENTIFIER)->newInstance($className, $args);
    }
700390
public function deleteInstance($selfedit = "false", $instanceName=null, $returnurl = null) {
        $this->selfedit = $selfedit;

        if ($selfedit == "true") {
            $this->moufManager = MoufManager::getMoufManager();
        } else {
            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
        }

        $this->moufManager->removeComponent($instanceName);
        $this->moufManager->rewriteMouf();

        if ($returnurl) {
            header("Location:".$returnurl);
        } else {
            header("Location: .?selfedit=".$selfedit);
        }
    }
1084911
protected function call($instance, $route, $method)
	{
		$parameters = $route->parametersWithoutNulls();

		return $instance->callAction($method, $parameters);
	}
1303857
public function invoke($instance, string $method, array $data)
    {
        // get the parameters
        $parameters = $this->resolveMethodParameters($instance, $method, $data, true);

        // get the reflection of method
        $reflection = new ReflectionMethod(get_class($instance), $method);

        // allow access
        $reflection->setAccessible(true);

        // execute the method
        return $reflection->invokeArgs($instance, $parameters);
    }
1367621
private function call($method, array $args)
    {
        $request = $this->rpcProtocol->encodeRequest($method, $args);

        $response = $this->transport->request($request);

        $response = $this->rpcProtocol->decodeValue($response);

        if ($response instanceof \Exception) {
            throw $response;
        }

        return $response;
    }
864435
protected static function _createChainResponse(
        $mockClassInstanceId,
        $mockClassType,
        $chainedMethodsBefore,
        $currentMethod,
        $args
    ) {

        $currentMethodName = key($currentMethod);
        $rReturnValues = &self::getMockHierarchyResponse(
            $chainedMethodsBefore,
            $mockClassType,
            $mockClassInstanceId
        );

        // Check current method exist in return values chain
        $serializedArgs = serialize($args);

        if ( ! isset($rReturnValues[$currentMethodName][$serializedArgs]['response']))
        {
            $serializedArgs = static::checkMatchingArguments(
                $rReturnValues[$currentMethodName],
                $args
            );

            if (is_null($serializedArgs)) {
                return null;
            }
        }

        return self::generateResponse(
            $rReturnValues[$currentMethodName][$serializedArgs]['response'],
            $args
        );
    }
703107
static function getInstance($methods, $patternUri, $action, $name = null)
    {
        if (!self::$instance) {
            self::$instance = new self($methods, $patternUri, $action, $name);
        }
        
        return self::$instance;
    }
900696
private static function createInstance($classname, array $arguments = array())
    {
        $reflection_obj = new ReflectionClass($classname);
        if ($reflection_obj->getMethod('__construct')->isPublic()) {
            self::$_instances[ $classname ] = call_user_func_array(array($reflection_obj, 'newInstance'), $arguments);
            if (
                method_exists(self::$_instances[ $classname ], 'init') &&
                is_callable(array(self::$_instances[ $classname ], 'init'))
            ) {
                self::$_instances[ $classname ]->init();
            }
        } else {
            self::$_instances[ $classname ] = new $classname;
            if (
                method_exists(self::$_instances[ $classname ], 'init') &&
                is_callable(array(self::$_instances[ $classname ], 'init'))
            ) {
                call_user_func_array(array(self::$_instances[ $classname ], 'init'), $arguments);
            }
        }
    }
293870
public function request($classname)
    {
        $instance = $this->instance($classname);
        return ($instance != null ? $instance->request() : null);
    }
1180999
private function addChainedMethodResponse($chainedMethodsBefore, $currentMethod, $action, $lastValue, $mockClassInstanceId)
    {
        $response = [];
        $rResponse = &$response;

        $currentMethodName = key($currentMethod);

        foreach ($chainedMethodsBefore as $chainedMethod)
        {
            $chainedMethodName = key($chainedMethod);
            $chainedMethodArgs = $chainedMethod[$chainedMethodName];

            $serializedChainedMethodArgs = serialize($chainedMethodArgs);
            $rResponse[$chainedMethodName][$serializedChainedMethodArgs] = [];
            $rResponse = &$rResponse[$chainedMethodName][$serializedChainedMethodArgs];
        }

        $rResponse[$currentMethodName][serialize(current($currentMethod))] = ['response' => ['action' => $action, 'value' => $lastValue, 'counter' => 0]];

        ShortifyPunit::addChainedResponse(array(get_class($this->mockClass) => [$mockClassInstanceId => $response]));
    }
350583
public static <T> T invoke(String className, String methodName, Object[] args) {
		return invoke(className, methodName, false, args);
	}