Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp 2.0 mock auth

I am developing an app in Cake 2.2, and am currently writing some unit tests. The model tests are working fine, i have some issues with the controller tests in general.

The situation: - most of the actions require a user login - there are custom components and vendor libraries - I have fixtures for all tables

The problem: - Many actions use the "$this->Auth->user()" method to get the user-data - If i write a test for that action, the user-data is (obviously) not existant

My (not working) solution: - I tried to mock the auth component, so that it would contain the user-method and always return a static array containing the user data, but it returns null, here's the code:

$Days = $this->generate('Days', array('components' => 'Auth'));
$Days->Auth->expects($this->once())->method('user')->will($this->returnValue(array(..userdata etc.)));
$result = $this->testAction('/days/settings');

It just keeps saying that the method was not called (altough the settings method inside the Days controller calls it exactly one time). What is wrong with the mockup? Hmmm..

Any help would be appreciated!

-edit- The code of the method that is being tested:

public function myDays()
{
   $user = $this->Auth->user();
   $days = $this->Day->find('all', array('conditions' => array('user_id' => $user['id'], 'active' => 1)));
   $this->set('days', $days);
}
like image 304
AlexFr. Avatar asked Nov 14 '22 01:11

AlexFr.


1 Answers

Maybe using:

$Days->Auth->staticExpects(

since user() is an static method.

like image 70
aarkerio Avatar answered Jan 25 '23 05:01

aarkerio