沃梦达 / 编程问答 / php问题 / 正文

CakePHP 2.3 - 单元测试用户登录

CakePHP 2.3 - Unit testing User Login(CakePHP 2.3 - 单元测试用户登录)

本文介绍了CakePHP 2.3 - 单元测试用户登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我必须在这里寻求帮助来解决我的问题.我花了一整个晚上的时间.我在 UsersController 中有一个登录方法,如下所示:

I thought I have to ask here some help to my problem. I've spend whole evening with this. I have a login method in UsersController like this:

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 对此进行测试,因此我可以确定只有有效用户才能登录 → 成功登录后,他们将被重定向到特定页面.这是我在 UsersControllerTest 类中的 testLogin 方法:

I trying to test this with PHPUnit, so I can be sure that only valid users can login → after a successful login they will be redirected to a specific page. Here's my testLogin method in UsersControllerTest class:

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 进行单元测试的基础知识.我收到此错误:

I am still learning the basics of unit testing with CakePHP. I get this error:

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

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

I have no idea what causes this... What's wrong with my test method and how it should be written?

谢谢!

推荐答案

我使用以下代码得到了这个:

I got this working with the following code:

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

这篇关于CakePHP 2.3 - 单元测试用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:CakePHP 2.3 - 单元测试用户登录

基础教程推荐