Ho pensato di dover chiedere qui qualche aiuto per il mio problema. Ho passato la serata intera con questo. Ho un metodo di accesso in UsersController
come questo:CakePHP 2.3 - Unit test Utente Login
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect(array('controller' => 'reservations', 'action' => 'index'));
} else {
$this->Session->setFlash(__('Login error.'), 'flashError');
}
}
}
ho cercato di testare questo con PHPUnit, in modo da poter essere sicuri che solo gli utenti validi possono accedere → dopo un login riuscito saranno reindirizzati ad una specifica pagina. Ecco il mio metodo testLogin
in UsersControllerTest
classe:
function testLogin() {
$UsersController = $this->generate('Users', array(
'components' => array(
'Auth' => array('user')
),
)
);
$UsersController->Auth->expects($this->any())
->method('user')
->with('id')
->will($this->returnValue(2));
$data = array('User' => array(
'student_number' => 1111111,
'password' => 'qwerty'
));
//$UsersController->Auth->login($data['User']);
$this->testAction('/users/login', array('data' => $data, 'method' => 'get'));
$url = parse_url($this->headers['Location']);
$this->assertEquals($url['path'], '/reservations');
}
sto ancora imparando le basi di unit testing con CakePHP. Ottengo questo errore:
PHPUNIT_FRAMEWORK_ERROR_NOTICE
Undefined index: Location
Test case: UsersControllerTest(testLogin)
Non ho idea di cosa provoca questo ... Cosa c'è di sbagliato con il mio metodo di prova e come dovrebbe essere scritto?
Grazie!
'$ questo-> intestazioni' non ha una chiave' Posizione'. Del resto ... da dove vengono le intestazioni "$ this-> headers"? –
Forse stai cercando l'intestazione 'response'? [CakeResponse - Setting headers] (http://book.cakephp.org/2.0/en/controllers/request-response.html#setting-headers) – thaJeztah