I am new to phpunit and unit testing in general. I am attempting to convert a large app to cakephp 2.0 and make unit tests for everything.
I am trying to create a mock object in which when, $this->Session->read(‘Auth.Account.id’) is called, it returns 144… this will give an account with an id that has items.
however, I get an error as the Mock seems to be erroring out on other Session::read(‘AuthCode’) calls in various beforeFilter calls;
Expectation failed for method name is equal to when invoked 1 time(s)
Parameter 0 for invocation SessionComponent::read(‘AuthCode’) does not match expected value.
Failed asserting that two strings are equal.
Like I said I am new to phpunit and unit testing… what am I doing wrong?
class PagesController extends MastersController {
public function support(){
if($this->Session->read('Auth.Account.id')) {
$items = $this->Account->Items->find('list', array('conditions'=>array('Items.account_id'=>$this->Session->read('Auth.Account.id'))));
}
$this->set(compact('items'));
}
}
class PagesControllerTestCase extends CakeTestCase {
/**
* Test Support
*
* @return void
*/
public function testSupport() {
#mock controller
$this->PagesController = $this->generate('Pages', array(
'methods'=>array('support'),
'components' => array(
'Auth',
'Session',
),
));
#mock controller expects
$this->PagesController->Session->expects(
$this->once())
->method('read') #Session:read() method will be called at least once
->with($this->equalTo('Auth.Account.id')) #when read method is called with 'Auth.Account.id' as a param
->will($this->returnValue(144)); #will return value 144
#test action
$this->testAction('support');
}
}
I’ve decided to just write the Sessions out manually. Like they do is the SessionComponentTest in the core.
https://github.com/cakephp/cakephp/blob/master/lib/Cake/Test/Case/Controller/Component/SessionComponentTest.php