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
精彩评论