When testing with phpunit, I want to assert a function call:
Given a Class:
Class TimeWrapper {
public function time() {
return time();
}
}
And Its unittest:
Class TimeWrapperTest extends PHPUnit_FrameworkTestCase {
public function testTime() {
//Pseudocode as example of a possible solution:
$this->assertCallsFunction("time");
}
}
I am specifically looking for a way to test calling of global functions.
FWIW: with rspec, I use Message Expectations. I am looking to achieve something similar, or exactly similar in PHPUnit.
If the goal is to verify that
TimeWrappercalls the built-in PHP functiontime, you’ll need to use the runkit extension. This will allow you to replace the built-in function with your own version that will record the call. You’ll need to enable therunkit.internal_overridesetting inphp.inito allow you to rename internal functions.If you cannot use the extension or just want to avoid that kind of trickery, you could modify
TimeWrapperto allow you to override the function that gets called at runtime.Use the test case above without the calls to
runkit_function_renameand passnew_timeto theTimeWrapperconstructor. The downside here is that you’ll pay a (probably tiny) performance penalty in production on each call toTimeWrapper::time.