Question

I am trying to setup unit tests for all of my HTTP requests. Every request requires authentication, and with my app it requires authentication via cookie & DB query.

I have a preDispatch method in a parent controller that looks like this:

        $this->cookie = Cookie::readCookie();
        if (is_null($this->cookie))
        {
            return $this->failResponseView();
        }

        $this->dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');

        //Does not have authoriziation
        if (!$this->hasAppAccess())
        {
            return $this->failResponseView();
        }

This has been working fine as far as the app is concerned. But running phpunit fails everytime because the cookie can't be read, or the response is being written before it is read.

This is me mirroring what I do in the regular app, in my test setup method:

$this->_cookie = new Cookie(array('access_token' => $profile['token']));
$this->_cookie->setCookie();

However, I receive this Exception when the code reaches this point. My question is, how can I fake, or bypass my cookie authentication when running phpunit to make sure all of these authenticated requests work?

Cannot modify header information - headers already sent by (output started at D:
\www\app\vendor\phpunit\phpunit\PHPUnit\Util\Printer.php:172)

UPDATE

It looks like since the PHPUnit\Util\Printer is outputting to STDOUT (see above), it's not liking that I am trying to write a cookie. Running this allowed full execution

phpunit --stderr

So I am able to call the setCookie() method, and it executes fine. But when I get to the point where it does Cookie::readCookie(), even though it's already been set, it can't read it. It returns null.

So question is still pretty much the same. What do I do to test this app if it uses cookie authentication?

Was it helpful?

Solution

Ugh, it's always something simple. In my setup method, I can just do this...

$_COOKIE[$name]= $this->_cookie->getData();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top