redefineMethod() public static method

There are two already defined variables that you can use in fake code: $mm_func_args = func_get_args(); $params is array of references to supplied arguments (func_get_args() does not contain refs in PHP5) You can use SoftMocks::callOriginal(...) for accessing original function/method as well Example: class A { public function b($c, &$d) { var_dump($c, $d); } } SoftMocks::redefineMethod(A::class, 'b', '$e, &$f', '$f = "hello";'); $a = 2; (new A())->b(1, $a); // nothing is printed here, so we intercepted the call var_dump($a); // string(5) "hello"
public static redefineMethod ( string $class, string $method, string $functionArgs, string $fakeCode, boolean $strict = true )
$class string
$method string Method of class to be intercepted
$functionArgs string List of argument names
$fakeCode string Code that will be eval'ed instead of function code
$strict boolean If strict=false then method of declaring class will also be mocked
コード例 #1
0
ファイル: SoftMocksExample.php プロジェクト: badoo/soft-mocks
 public static function applyMocks()
 {
     \QA\SoftMocks::redefineConstant('TEST_CONSTANT_WITH_VALUE_42', 43);
     \QA\SoftMocks::redefineConstant('\\Example::STATIC_DO_SMTH_RESULT', 'Example::STATIC_DO_SMTH_RESULT value changed');
     \QA\SoftMocks::redefineFunction('someFunc', '$a', 'return 55 + $a;');
     \QA\SoftMocks::redefineMethod(Example::class, 'doSmthStatic', '', 'return "Example::doSmthStatic() redefined";');
     \QA\SoftMocks::redefineMethod(Example::class, 'doSmthDynamic', '', 'return "Example->doSmthDynamic() redefined";');
 }
コード例 #2
0
ファイル: ExampleTest.php プロジェクト: badoo/soft-mocks
 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]));
 }