Using PHPUnit to test cookies and sessions, how?(使用 PHPUnit 测试 cookie 和会话,如何?)
问题描述
使用 PHPUnit 可以很容易地测试原始 PHP 代码,但是那些严重依赖 cookie 的代码呢?会话可能是一个很好的例子.
With PHPUnit it's quite easy to test raw PHP code, but what about code that heavily relies on cookies? Sessions could be a good example.
有没有一种方法不需要我在测试期间使用数据设置 $_COOKIE
?这感觉像是一种 hacky 的做事方式.
Is there a method that doesn't require me to setup $_COOKIE
with data during my test? It feels like a hacky way of doing things.
推荐答案
这是代码的常见问题,尤其是遗留的 PHP 代码.常用的技术是进一步抽象相关对象中的 COOKIE/SESSION 变量,并使用控制反转技术将这些依赖关系拉入范围.
This is a common problem with code, especially lagacy PHP code. The common technique used is to further abstract the COOKIE/SESSION variables in related objects and using inversion of control technique(s) to pull those dependencies into scope.
http://martinfowler.com/articles/injection.html
现在在执行测试之前,您将实例化 Cookie/Session 对象的模拟版本并提供默认数据.
Now before you execute a test you would instantiate a mock version of a Cookie/Session object and provide default data.
我想,通过在执行测试之前简单地覆盖超级全局值,可以使用遗留代码实现相同的效果.
I imagine, the same effect can be achieved with legacy code by simply overriding the super global value before executing the test.
干杯,亚历克斯
这篇关于使用 PHPUnit 测试 cookie 和会话,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHPUnit 测试 cookie 和会话,如何?


基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01