Myślałem, że muszę poprosić o pomoc w moim problemie. Spędziłem z tym cały wieczór. Mam metodę logowania w UsersController
takiego:CakePHP 2.3 - Testowanie jednostek Użytkownik 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');
}
}
}
Ja próbuje przetestować to z PHPUnit, więc mogę mieć pewność, że tylko ważne użytkownicy mogą zalogować → po udanym logowaniu zostaną one przekierowane do specyficznych strona. Oto mój testLogin
metoda UsersControllerTest
klasy:
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');
}
nadal jestem nauki podstaw testów jednostkowych z CakePHP. Otrzymuję ten błąd:
PHPUNIT_FRAMEWORK_ERROR_NOTICE
Undefined index: Location
Test case: UsersControllerTest(testLogin)
nie mam pojęcia co jest tego przyczyną ... Co się stało z moim metody badania i jak to powinno być napisane?
Dzięki!
'$ this-> headers' nie ma klucza' Lokalizacja'. Jeśli o to chodzi ... skąd pochodzi '$ this-> headers'? –
Może szukasz nagłówka odpowiedzi? [CakeResponse - Ustawianie nagłówków] (http://book.cakephp.org/2.0/en/controllers/request-response.html#setting-headers) – thaJeztah