Search code examples
laravelauthenticationphpunitlaravel-jetstream

Laravel Auth::attemp() is not working, even with valid credentials


This is the unit test which I want to run. This is always failing. I have tried with wrong data. Still it fails and with correct data still it fails. Please someone help me out from this.

If anyone wants to explore more: http://github.com/PawanRoy1997/forum.git

Php Unit Test:

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

Output:

➜  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) ✗ 

Solution

  • Actually, I just figured it out that I need to hash the password while creating the user. I forgot to mention that I have updated Laravel then all of this started. In laravel 8.51, Auth::attempt checks for hashed passwords only. But in laravel 8.50 and before, it allows password to be not hashed.

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