Auth::attempt PHPUnit 测试失败

Auth::attempt fails with PHPUnit TESTING

我有一个非常奇怪的行为,我看不出它是从哪里来的。我用 Laravel 文档构建了一个身份验证系统。当我保存用户并尝试从应用程序登录时,它工作正常。然而,当我在我的 AuthControllerTest 中做同样的事情时,Auth::attempt 失败了。

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

谢谢

记住密码是加密的,所以需要直接从User实例中获取密码,如:

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

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