5

我想我必须在这里向我的问题寻求一些帮助。我已经花了整个晚上的时间。我有一个这样的登录方法UsersController

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' );
            }
        }
    }

我尝试使用PHPUnit对此进行测试,因此我可以确定只有有效用户才能登录 → 成功登录后,他们将被重定向到特定页面。这是我testLoginUsersControllerTest课堂上的方法:

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' );
    }

我仍在学习使用 CakePHP 进行单元测试的基础知识。我收到此错误:

PHPUNIT_FRAMEWORK_ERROR_NOTICE
Undefined index: Location
Test case: UsersControllerTest(testLogin)

我不知道是什么原因造成的......我的测试方法有什么问题以及应该如何编写?

谢谢!

4

2 回答 2

2

我使用以下代码进行了此操作:

function testLogin() {

        //mock user
        $this->Users = $this->generate( 'Users', array(
                'components' => array(
                    'Security' => array( '_validatePost' ),
                )
            ) );

        //create user data array with valid info
        $data = array();
        $data['User']['student_number'] = 1234567;
        $data['User']['password'] = '[valid password here]';

        //test login action
        $result = $this->testAction( "/users/login", array(
                "method" => "post",
                "return" => "contents",
                "data" => $data
            )
        );

        $foo[] = $this->view;
        //debug($foo);

        //test successful login
        $this->assertNotNull( $this->headers['Location'] );
        $this->assertContains( 'reservations', $this->headers['Location'] );
        $this->assertNotContains( '"/users/login" id="UserLoginForm"', $foo );

        //logout mocked user
        $this->Users->Auth->logout();
    }
于 2013-04-03T05:03:58.257 回答
0

我使用这个测试用例来覆盖 cakeAuth调用Session并检查登录是否成功。

这更像是我在测试中使用的通用解决方案。用于在用户登录后将值放入会话中,并检查登录是否成功。

<?php
App::uses('UsersController', 'Controller');
App::uses('AuthComponent', 'Controller/Component');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');

$_SERVER['HTTP_USER_AGENT'] = '';

class stubSession {
  public $data = array();

  public function write($key, $value){
    $this->data[$key] = $value;
  }

  public function read($key){
    if(array_key_exists($key, $this->data)){
      return $this->data[$key];
    } 
  }

  public function renew() {

  }

  public function setFlash(){

  }

  public function delete() {

  }

  public function check(){

  }
}

class stubRequest {
  public $data = array();

  function __construct($data) {
    $this->data = $data;
  }

  public function is() {
    return true;
  }

  public function clientIp(){

  }
}

class stubAuthComponent extends AuthComponent{

  public static $_session;

  public function __construct($args, $session){
    parent::__construct($args);
    $this->Session = $session;
    self::$_session = $session; 
    $this->request = new CakeRequest();
    $this->response = new CakeResponse();
  }

  public function loggedIn() {
    return self::$_session->read(self::$sessionKey) != array();
  }

  public function logout(){

  }

  public static function user($key) {
    $user = self::$_session->read(self::$sessionKey);
    return $user[$key];
  }
}

class UsersControllerTest extends UsersController {  

  function __construct($data){
    $this->User = ClassRegistry::init('User');
    $this->Session = new stubSession();
    $this->Auth = new stubAuthComponent(new ComponentCollection(), $this->Session); 
    $this->request = new stubRequest($data);
  }

  public function redirect(){

  }
}

class TestUsersController extends CakeTestCase {

  public function testLogin(){
    $_POST = array('User' => array('email' => 'mail@someemail.com', 'username' => 'mail@someemail.com', 'password' => 'abcd1234'));
    $usersController = new UsersControllerTest($_POST);
    $usersController->login();
    $login = $usersController->Auth->loggedIn();
    //debug($usersController->Session->data); //you can test the session key value in here
    $this->assertEquals($login, true);
  }
}
于 2016-08-24T11:06:41.937 回答