3

I try to test my application for login and logout, with a jwtToken. I'm lost with Laravel Contract and Trait. I read a lot of questions/answers but the solutions cannot work on my code. I understand that contract not work like trait but it's seems to be quite linked. I have been looking for a way out since 3 days, without success.

I have this error since i put ActingAs in my test.

Can you help me please ?

My User Model :


namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Auth\UserInterface; 
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Role;
use App\Models\UserAddress;


class User extends Authenticatable implements 
    JWTSubject,
    AuthenticatableContract
{
    use Notifiable;
    use SoftDeletes;
    use Authenticatable;

I also try without implements AuthenticatableContract I also try without the first use, without the second use.

My TestCase :


namespace Tests;

use App\Models\Role;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;


abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    /**
     * @var User
     */
    protected $user;

    public static $admin;

    public static $faker;

    public function setUp(): void
    {
        parent::setUp();

        self::$faker = \Faker\Factory::create('fr_FR');


        self::$admin = \App\Models\User::with('role')
            ->select('*')
            ->join('roles', 'roles.id_role', '=', 'users.id_role')
            ->where('slug', 'admin')
            ->first()->user;

My Account Test :


namespace Tests\Feature;

use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Role;
use App\Models\User;
use Tymon\JWTAuth\JWTAuth;
use Illuminate\Auth\AuthenticationException;

use Illuminate\Contracts\Auth\Authenticatable;

[...]
 public function testLogout() {

        // $user =  User::inRandomOrder()->firstOrFail();
        // \Log::debug('user->first_name : ' . $user->first_name);

        $this->actingAs(self::$admin)
            ->json('POST', 'api/auth/logout')
            ->assertStatus(200); 
    }```
Couka56
  • 31
  • 1
  • 2

1 Answers1

0

This is incorrect:

class User extends Authenticatable implements 
    JWTSubject,
    AuthenticatableContract
{
    use Notifiable;
    use SoftDeletes;
    use Authenticatable;
}

You cannot extend Authenticatable and use Authenticatable.

Authenticatable is not a trait (as the error says). It is an abstract class and can only be extended.

You can read more on the difference between traits and abstract classes here: Difference between Trait and an Abstract Class in PHP

Removing the use statement should fix your problem.

Darryl E. Clarke
  • 7,537
  • 3
  • 26
  • 34
  • Thanks ! It's work (nearly). I didn't delete the ```use Authenticable``` in my test file. I just have to resolve an another error with the ```Interface [...] AuthenticatableContract not found```. It will be easier to resolve. – Couka56 Apr 17 '20 at 09:50