Without passing a value to justReturn() the target function will return nothing (null).
returnArg()
This other when-related method is used to make the target function return one of the received arguments, by default the first.
Note that if the target function does not receive the desired argument, returnArg() throws an exception:
justEcho()
Similar to justReturn(), it makes the mocked function echo some value instead of returning it.
echoArg()
Similar to returnArg(), it makes the mocked function echo some received argument instead of returning it.
alias()
The last of the when-related methods allows to make a function behave just like another callback. The replacing function can be anything that can be run: a core function or a custom one, a class method, a closure...
Functions\when('echo_the_first')->echoArg(); // is the same of ->echoArg(1)
Functions\when('echo_the_second')->echoArg(2);
echo_the_first('A', 'B', 'C'); // echoes "A"
echo_the_second('A', 'B', 'C'); // echoes "B"
Functions\when('duplicate')->alias(function($value) {
return "Was ".$value.", now is ".($value * 2);
});
Functions\when('bigger')->alias('strtoupper');
echo duplicate(1); // echoes "Was 1, now is 2"
echo bigger('was lower'); // echoes "WAS LOWER"