PHPUnit Mock Function
PHPUnit extension to Mock PHP internal functions using Runkit., (*1)
Requirements
*This is an optional requirement. Runkit doesn't currently support the override of internal functions (exit, die etc)., (*2)
Installation
Using composer, add the following to the composer.json file:, (*3)
{
"require": {
"qubit05/phpunit-mockfunction": "1.*"
}
}
Example
ExampleClass.php
, (*4)
<?php
class ExampleClass
{
public function doExample()
{
return date();
}
}
ExampleClassTest.php
, (*5)
<?php
class ExampleClassTest extends \PHPUnit_Framework_TestCase
{
public function testExample()
{
$param = 'Y-m-d H:i:s';
$value = 'non date value';
$mockFunction = new PHPUnit_Extensions_MockFunction('date', $this);
$mockFunction->expects($this->once())
->with($this->equalTo($param))
->will($this->returnValue($value));
$exampleClass = new ExampleClass();
$this->assertEquals($value, $exampleClass->doExample($param));
}
}
Acknowledgement
When this class was created, some inspiration was taken from tcz/phpunit-mockfunction. The two classes are similar but not the same., (*6)