Allows mocking otherwise untestable PHP functions through the use of namespaces.
<?phpnamespaceMyNamespace;
class Tool
{
publicfunctionisString($string)
{
returnstrlen($string) > 0 && ctype_alpha($string);
}
}<?phprequire_once'PHPUnit/Extension/FunctionMocker.php';
class MyTestCase extends PHPUnit_Framework_TestCase
{
publicfunctionsetUp()
{
$this->php = PHPUnit_Extension_FunctionMocker::start($this, 'MyNamespace')
->mockFunction('strlen')
->mockFunction('ctype_alpha')
->getMock();
}
/** @runInSeparateProcess */publicfunctiontestIsStringUsesStrlenAndCtypeAlpha()
{
$this->php
->expects($this->once())
->method('strlen')
->with('foo')
->will($this->returnValue(3))
;
$this->php
->expects($this->once())
->method('ctype_alpha')
->with('foo')
->will($this->returnValue(false))
;
$tool = newMyNamespace\Tool();
$this->assertFalse($tool->isString('foo'));
}
}Use @runInSeparateProcess annotation to make sure that the mocking is reliably working in PHP >=5.4