5

I have figured out how to mock the Auth Component when testing my controllers, but am struggling to mock the Auth Component when testing my components. I am using cakephp2.0 and phpUnit.

When I use the ::generate() I am getting Error: Call to undefined method TestCalendarController::generate.

Is there a way to mock the Auth Component user() function? Or do I need to rewrite the component to avoid using it?

Thanks!

CalendarComponentTest

App::uses('Controller', 'Controller');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
App::uses('ComponentCollection', 'Controller');
App::uses('CalendarComponent', 'Controller/Component');
App::uses('AuthComponent', 'Controller/Component');

class TestCalendarController extends Controller {

}

class CalendarComponentTest extends CakeTestCase {
    public $CalendarComponent = null;
    public $Controller = null;

public function setUp() {
        parent::setUp();
        // Setup our component and fake test controller
        $Collection = new ComponentCollection();
        $this->CalendarComponent = new CalendarComponent($Collection);
        $CakeRequest = new CakeRequest();
        $CakeResponse = new CakeResponse();
        $this->Controller = new TestCalendarController($CakeRequest, $CakeResponse);
        $this->CalendarComponent->startup($this->Controller);
}

//Here I am trying to mock the Auth component. I've tried a number of different things, and I'm not getting anything to work.
public function testAdjust() {
    $TestCalendar = $this->Controller->generate('TestCalendar', array(
        'components' => array(
            'Auth' => array('user')
        )
    ));
    $TestCalendar->Auth->staticExpects($this->any())
        ->method('user')
        ->will($this->returnValue(array('id'=>1, 'timezone'=>'America/Los_Angeles', 'type'=>'student')));

    // Test our adjust method with different parameter settings
    $this->CalendarComponent->calculate_parameters();



}

 public function tearDown() {
      parent::tearDown();
      // Clean up after we're done
      unset($this->CalendarComponent);
      unset($this->Controller);
  }


} 
1
  • I'm interested in the solution for the controller - can you give a short example or link for that? Oct 5, 2018 at 8:15

1 Answer 1

1

I have the same issue and found a possible solution, at least it works for me.

To get some hints, I pointed my attention to the test cases for cakephp itself and in particular the one for the AuthComponent https://github.com/cakephp/cakephp/blob/master/lib/Cake/Test/Case/Controller/Component/AuthComponentTest.php

It seems to contain mocking of other components, for example:

$this->Auth->Session = $this->getMock('SessionComponent', array('renew'), array(), '', false);

In your case you should use something like:

$this->CalendarComponent->Auth = $this->getMock('Auth', array('user'));
$this->CalendarComponent->Auth->expects($this->any())->method('user')->with('id')->will($this->returnValue($user_id));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.