If you need to write a test code for a code like ↓
class Standard_Function extensions\Utility{
/**
* @param array [1,2] (fixed)
* @return array
*/
public static function get_Shuffle($array){
return shuffle($array);
}
}
We believe that the test will not pass unless the return value of the PHP standard function shuffle is fixed.
I tried to replace the shuffle with expectMock, but it didn't work.
I am currently forcing it to move by sort before asserting.
In this example, if you were to write a test, how would you write a test?
I would appreciate it if you could let me know.
As Yukihiro Honda has indicated, I think it is possible to overwrite the built-in function using override_function.
override_function('shuffle', '$array', 'return$array;');
However, be aware that this requires the installation of the APD (Advanced PHP Debugger) module.
In addition, Is it possible to overwrite a function in PHP-Stack Overflow suggests a method of overriding a function using namespace.
If the test object belongs to a namespace, you can use it by defining the function you want to mock in the namespace.
namespace Your\Name\Space{
function shuffle($array){
return $array;
}
}
In any case, it is important to note that it is not "one-time only" and that overwriting it will affect other tests.(You may not have to worry about that.)
(Some libraries seem to have made the latter easier to use. php-mock/php-mock: Mock built-in PHP functions (e.g.time(), exec() or land()))
© 2024 OneMinuteCode. All rights reserved.