Qa\SoftMocks::callOriginal PHP Method

callOriginal() public static method

public static callOriginal ( $callable, $args, $class = null )
    public static function callOriginal($callable, $args, $class = null)
    {
        if (is_array($callable)) {
            if (is_object($callable[0])) {
                $obj = $callable[0];
                if (!$class) {
                    $class = get_class($obj);
                }
            } else {
                $class = $callable[0];
            }
            $method = $callable[1];
        } else {
            if (is_scalar($callable) && strpos($callable, '::') !== false) {
                list($class, $method) = explode("::", $callable);
            } else {
                return call_user_func_array($callable, $args);
            }
        }
        try {
            $Rm = new \ReflectionMethod($class, $method);
            if ($Rm->isUserDefined()) {
                self::$temp_disable = true;
                // we can only mock and check for mocks for user defined methods
            }
        } catch (\ReflectionException $e) {
            // do nothing, it is ok in this case because it means that mock disabling is not needed
        }
        if (isset($obj)) {
            return self::callMethod($obj, $class, $method, $args);
        } else {
            return self::callStaticMethod($class, $method, $args);
        }
    }

Usage Example

Example #1
0
 public function testMethod()
 {
     \QA\SoftMocks::redefineMethod(self::class, 'exampleFact', '$n', 'return -1;');
     $this->assertEquals(-1, $this->exampleFact(4));
     $this->assertEquals(-4, \QA\SoftMocks::callOriginal([$this, 'exampleFact'], [4]));
 }