CakePHP 2.1: Creating a Layout Controller with Session :: read ()

I am new to phpunit and unit testing in general. I am trying to convert a large application to cakephp 2.0 and do 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 identifier that has elements.

however, I get an error because Mock seems to cause errors on other Session :: read ('AuthCode') calls in various beforeFilter calls;

The wait is not fulfilled for the method name is equal when called 1 time (a) Parameter 0 for calling SessionComponent :: read ('AuthCode') does not match the expected value. Failed to claim that two lines are equal.

As I said, I'm 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');
    }
}
+3
source share
2 answers

I decided to just write the sessions manually. Like this being done, this is a SessionComponentTest in the kernel.

https://github.com/cakephp/cakephp/blob/master/lib/Cake/Test/Case/Controller/Component/SessionComponentTest.php

+1
source

You must access the Aut session variables with the Auth component, not with the Session component.

Instead

if($this->Session->read('Auth.Account.id')) {

to try

if ($this->Auth->user('Account.id')) {

The same applies to your elements :: find call.

The failure of the Auth component still remains.

class PagesControllerTestCase extends CakeTestCase {

it should be

class PagesControllerTestCase extends ControllerTestCase {

and then in your test:

$PagesController = $this->generate('Pages', array(
    'methods'=>array('support'),
    'components' => array(
        'Auth' => array('user')
     ),
));

$PagesController->Auth->staticExpects($this->exactly(2))
    ->method('user')
    ->with('Account.id')
    ->will($this->returnValue(144));
+1
source

All Articles