Laravel Auth::attemp() 不工作,即使凭据有效
Laravel Auth::attemp() is not working, even with valid credentials
这是我想要的单元测试运行。这总是失败的。我试过错误的数据。它仍然失败并且使用正确的数据仍然失败。请有人帮我解决这个问题。
如果有人想探索更多:http://github.com/PawanRoy1997/forum.git
Php 单元测试:
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;
class UserTest extends TestCase
{
use RefreshDatabase;
/** @@test */
public function login()
{
$data = ['name'=>'some', 'email'=>'some@some.com','password'=>'password'];
User::create($data);
$this->assertTrue(Auth::attempt($data));
}
}
输出:
➜ forum git:(master) ✗ asn test
FAIL Tests\Unit\UserTest
⨯
FAIL Tests\Feature\UserTest
⨯ login
---
• Tests\Unit\UserTest >
Error
Call to a member function connection() on null
at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1571
1567▕ * @return \Illuminate\Database\Connection
1568▕ */
1569▕ public static function resolveConnection($connection = null)
1570▕ {
➜ 1571▕ return static::$resolver->connection($connection);
1572▕ }
1573▕
1574▕ /**
1575▕ * Get the connection resolver instance.
+7 vendor frames
8 tests/Unit/UserTest.php:19
Illuminate\Database\Eloquent\Model::__callStatic()
• Tests\Feature\UserTest > login
Failed asserting that false is true.
at tests/Feature/UserTest.php:19
15▕ public function login()
16▕ {
17▕ $data = ['name'=>'some', 'email'=>'some@some.com','password'=>'password'];
18▕ User::create($data);
➜ 19▕ $this->assertTrue(Auth::attempt($data));
20▕ }
21▕ }
22▕
Tests: 2 failed
Time: 0.31s
➜ forum git:(master) ✗
实际上,我只是发现在创建用户时需要对密码进行哈希处理。我忘了说我已经更新了 Laravel 然后所有这些都开始了。在 laravel 8.51 中,Auth::attempt 仅检查散列密码。但在 laravel 8.50 及之前,它允许不对密码进行哈希处理。
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
class UserTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function test_example()
{
$data = [
'name' => 'someone',
'email' => 'some@some.com',
'password' => Hash::make('password'),
'confirm_password' => 'password'
];
User::create($data);
$this->assertDatabaseCount('users', 1);
$this->assertTrue(Auth::attempt(['email' => 'some@some.com', 'password' => 'password']));
}
}
这是我想要的单元测试运行。这总是失败的。我试过错误的数据。它仍然失败并且使用正确的数据仍然失败。请有人帮我解决这个问题。
如果有人想探索更多:http://github.com/PawanRoy1997/forum.git
Php 单元测试:
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;
class UserTest extends TestCase
{
use RefreshDatabase;
/** @@test */
public function login()
{
$data = ['name'=>'some', 'email'=>'some@some.com','password'=>'password'];
User::create($data);
$this->assertTrue(Auth::attempt($data));
}
}
输出:
➜ forum git:(master) ✗ asn test
FAIL Tests\Unit\UserTest
⨯
FAIL Tests\Feature\UserTest
⨯ login
---
• Tests\Unit\UserTest >
Error
Call to a member function connection() on null
at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1571
1567▕ * @return \Illuminate\Database\Connection
1568▕ */
1569▕ public static function resolveConnection($connection = null)
1570▕ {
➜ 1571▕ return static::$resolver->connection($connection);
1572▕ }
1573▕
1574▕ /**
1575▕ * Get the connection resolver instance.
+7 vendor frames
8 tests/Unit/UserTest.php:19
Illuminate\Database\Eloquent\Model::__callStatic()
• Tests\Feature\UserTest > login
Failed asserting that false is true.
at tests/Feature/UserTest.php:19
15▕ public function login()
16▕ {
17▕ $data = ['name'=>'some', 'email'=>'some@some.com','password'=>'password'];
18▕ User::create($data);
➜ 19▕ $this->assertTrue(Auth::attempt($data));
20▕ }
21▕ }
22▕
Tests: 2 failed
Time: 0.31s
➜ forum git:(master) ✗
实际上,我只是发现在创建用户时需要对密码进行哈希处理。我忘了说我已经更新了 Laravel 然后所有这些都开始了。在 laravel 8.51 中,Auth::attempt 仅检查散列密码。但在 laravel 8.50 及之前,它允许不对密码进行哈希处理。
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
class UserTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function test_example()
{
$data = [
'name' => 'someone',
'email' => 'some@some.com',
'password' => Hash::make('password'),
'confirm_password' => 'password'
];
User::create($data);
$this->assertDatabaseCount('users', 1);
$this->assertTrue(Auth::attempt(['email' => 'some@some.com', 'password' => 'password']));
}
}