似乎在这种特殊情况下这是不可能的。
但在某些特定情况下,您可以模拟静态方法:http ://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html
class Foo
{
public static function doSomething()
{
return static::helper();
}
public static function helper()
{
return 'foo';
}
}
测试:
public function testQQQ()
{
$class = $this->getMockClass(
'Foo', /* name of class to mock */
array('helper') /* list of methods to mock */
);
$class::staticExpects($this->exactly(2))
->method('helper')
->will($this->returnValue('bar'));
$this->assertEquals(
'bar',
$class::doSomething()
);
}
结果:
$ phpunit --filter QQQ
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /var/www/.../phpunit.xml
F
Time: 1 second, Memory: 10.75Mb
There was 1 failure:
1) ...::testQQQ
Expectation failed for method name is equal to <string:helper> when invoked 2 time(s).
Method was expected to be called 2 times, actually called 1 times.
FAILURES!
Tests: 1, Assertions: 2, Failures: 1.