lithium\test\Mocker::overwriteFunction PHP Method

overwriteFunction() public static method

Will overwrite namespaced functions.
public static overwriteFunction ( string | boolean $name, closure | boolean $callback = null ) : void
$name string | boolean Fully namespaced function, or `false` to reset functions.
$callback closure | boolean Callback to be called, or `false` to reset this function.
return void
    public static function overwriteFunction($name, $callback = null)
    {
        if ($name === false) {
            static::$_functionResults = array();
            return static::$_functionCallbacks = array();
        }
        if ($callback === false) {
            static::$_functionResults[$name] = array();
            return static::$_functionCallbacks[$name] = false;
        }
        static::$_functionCallbacks[$name] = $callback;
        if (function_exists($name)) {
            return;
        }
        $function = new ReflectionFunction($callback);
        $pos = strrpos($name, '\\');
        eval(self::_dynamicCode('mockFunction', 'function', array('namespace' => substr($name, 0, $pos), 'function' => substr($name, $pos + 1), 'args' => static::_methodParams($function), 'stringArgs' => static::_stringMethodParams($function))));
        return;
    }

Usage Example

Ejemplo n.º 1
0
 public function testFunctionSuccessful()
 {
     Mocker::overwriteFunction('app\\extensions\\file_get_contents', function () {
         return 'foo';
     });
     \app\extensions\file_get_contents();
     $chain = Mocker::chain('app\\extensions\\file_get_contents');
     $this->assertTrue($chain->called('app\\extensions\\file_get_contents')->with()->success());
 }
All Usage Examples Of lithium\test\Mocker::overwriteFunction