Unit testing with session variables

I am trying to unit test some functions that depend on session variables. Basically a whole heap of functions depend on the context that is stored in your session.

For user ID Yii::app()->user->id = 5 seems to work at the start of the unit test seems to work, but for merchantID I get:


CException: Property "WebUser.merchantID" is not defined.

I tried defining merchantID in WebUser but that caused some other problems, I also tried:




$this->setState('merchantID', 5);



But that doesn’t seem to be setting the value properly.

Finally, I made a getMerchantID function and tried to set up a mock in PHPUnit:




    private function setupWebUserMock($merchant_id){

        $stub = $this->getMock('WebUser');

        $stub->expects($this->any())->method('getMerchantID')->will($this->returnValue($merchant_id));

        Yii::app()->user = $stub;

    }



But Yii::app()->user is read only apparently.

What is the usual way to mock session data in unit tests?