使用phpunit实现自定义setcookie


custom setcookie implementation with phpunit

我正在尝试使用test_helpers PECL扩展覆盖setcookie实现,没有任何运气。

    $username = 'test';
    set_setcookie_overload(function() { error_log('setcookie called'); return FALSE; });
    setcookie('username', $userName, time()+60*60*24*365, '/');
    unset_setcookie_overload();

我得到这个错误:PHP致命错误:调用未定义函数set_setcookie_overload()

我验证了扩展在这里的例子中工作得很好:https://github.com/sebastianbergmann/php-test-helpers

谢谢。

Update:为了进一步澄清,听起来像是test_helpers扩展允许覆盖内置的PHP函数,如setcookie。在上面的链接中,作者提供了一个使用set_exit_overload()覆盖退出方法的示例。我试图做同样的setcookie()使用set_setcookie_overload()没有任何运气。

可以在源代码 over at github 中找到可用的函数。所有以PHP_FUNCTION开头的内容都公开给用户调用。

是的。在php-test-helpers中没有set_setcookie_overload()。你得另想办法。

遗憾的是,rename_function也不能帮助你,因为它不能与内部功能一起工作。

test_helpers扩展的完整函数列表