使用PHPUnit TESTING,Auth :: attempt失败

时间:2015-01-28 18:39:35

标签: php laravel laravel-4 phpunit

我有一种非常奇怪的行为,我不知道它可能来自哪里。我用Laravel文档构建了一个身份验证系统。当我保存用户并尝试从应用程序登录时,它工作正常。但是,当我在AuthControllerTest中执行相同操作时,Auth ::尝试失败。

AuthControllerTest.php

<?php
use tests\helpers\Factory;

class AuthControllerTest extends ApiTester
{
    use Factory;

    public function setUp()
    {
        parent::setUp();
       Artisan::call('migrate');
        Route::enableFilters();
        Session::start();
    }

/*
This test fails
I have a 401 instead of a 200 HTTP response code
*/
    /** @test */
    public function it_should_log_in_user()
    {
        User::create([
            'email'          => 'testing@testing.com',
            'password'       => 'testing'
        ]);

        $credentials = [
            'email'    => 'testing@testing.com',
            'password' => 'testing'
        ];
    //dd(User::count() // I have 1 record
        $this->getJson('login', 'POST', $credentials);

        $this->assertResponseOk();
    }

    /** @test */
    public function it_should_throws_exception_if_login_fails()
    {
        User::create([
            'email'    => 'testing@testing.com',
            'password' => 'testing'
        ]);
        $this->getJson('login', 'POST', [
            'email'    => 'testing@testing.com',
            'password' => 'test'
        ]);
        $this->assertResponseStatus(401);
    }

    /** @test */
    public function it_should_log_out_user()
    {
        $user = User::create([
            'email'    => 'testing@testing.com',
            'password' => 'password'
        ]);
        $this->be($user);

        $this->getJson('logout', 'POST');
        $this->assertResponseStatus(204);
    }

    /**
     * Generate Alert mock
     * @return array
     */
    protected function getStub()
    {
        return [
        ];
    }
}

AuthController.php

<?php


use Arato\Transformers\UserTransformer;
use controllers\ApiController;

class AuthController extends ApiController
{
    protected $userTransformer;

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

    public function login()
    {
        $rules = [
            'email'    => ['required', 'email'],
            'password' => ['required', 'alphaNum']
        ];

        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()) {
            return $this->respondUnauthorized();
        }

        $userData = [
            'email'    => Input::get('email'),
            'password' => Input::get('password')
        ];

        if (Auth::attempt($userData)) {
            return $this->respond([
                'data' => $this->userTransformer->transform(Auth::user())
            ]);
        } else {
            return $this->respondUnauthorized();
        }
    }

    public function logout()
    {
        Auth::logout();

        return $this->respondNoContent();
    }
}

谢谢

1 个答案:

答案 0 :(得分:2)

请记住,密码是加密的,因此您需要直接从User实例获取密码,例如:

        $user = User::create([
            'email'          => 'testing@testing.com',
            'password'       => Hash::make('testing')
        ]);

        $credentials = [
            'email'    => $user->email
            'password' => $user->password
        ];