开发者

PHPUnit, superglobal vars. How to setup test such that $_SERVER and $_GET are properly set?

I am writing 开发者_如何学JAVAsome PHP code to parse incoming URLs. It relies heavily on PHP globals such as $_SERVER and $_GET. I am thinking of manually setting these up in PHPUnit unit test setUp() function. Is there a better way of doing this?


An idea would be to wrap $_SERVER and $_GET into classes instead of accessing them directly, so that you do not have to rely on them altogether. You see this approach frequently in the common frameworks. This way you can stub or mock the request easily without having to setup the state of the superglobals.

An example of such a class would be here: https://codereview.stackexchange.com/questions/73515/http-request-response-class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜