views:

239

answers:

1

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.

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.

+2  A: 

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

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.

Cheers, Alex