使用 phpunit 实现自定义 setcookie

发布于 2024-12-22 13:36:27 字数 728 浏览 6 评论 0原文

我试图使用 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

谢谢。

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

I'm trying to override the setcookie implementation using the test_helpers PECL extension without any luck.

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

I get this error:
PHP Fatal error: Call to undefined function set_setcookie_overload()

I verified that the extension is working well with the example here: https://github.com/sebastianbergmann/php-test-helpers

Thanks.

Update: For further clarification, it sounds like the test_helpers extension allows to override in-built PHP functions like setcookie. In the link above, the author has provided an example to override the exit method using set_exit_overload(). I'm trying to do the same for setcookie() using set_setcookie_overload() without any luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2024-12-29 13:36:27

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

所以是的。 php-test-helpers 中没有 set_setcookie_overload() 。你必须找到另一种解决方法。

遗憾的是 rename_function 也无法帮助您,因为它不适用于内部函数。

test_helpers扩展的完整功能列表

The functions that are available in can be found in the source over at github. Everything starting with PHP_FUNCTION is exposed to the user to call.

So yeah. There just is no set_setcookie_overload() in php-test-helpers. You have to find another workaround.

Sadly rename_function can't help you either as it doesn't work with internal functions.

Complete function list of the test_helpers extension

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文