canadianlover
1 week ago

User factory generates collection instead of authenticable trait

Posted 1 week ago by canadianlover

I have the following test

    public function test_only_administrators_can_add_files() {
        $brian = create('App\User', ['name' => 'Brain Hull']);
        $user = create("App\User");
        $file = create('App\Document');

        $this->signIn($brian);
        $this->post('/documents/store', $file->toArray())->assertStatus(201);

        $this->signIn($user);
        $this->post('/documents/store', $file->toArray())->assertStatus(403);

    }

My User model ilooks like this:

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements Authenticatable
{
    use AuthenticableTrait;




    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

My User factory looks like this:

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
    ];
});

I am using Laravel 6 and I have not changed anything to Laravel's user system. For some reason when I run my test I gedt the following error:

1) Tests\Feature\FileTest::test_only_administrators_can_add_files
TypeError: Argument 1 passed to Illuminate\Foundation\Testing\TestCase::actingAs() must implement interface Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Database\Eloquent\Collection given, called in /home/vagrant/Code/documents/tests/TestCase.php on line 15

/home/vagrant/Code/documents/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithAuthentication.php:16
/home/vagrant/Code/documents/tests/TestCase.php:15
/home/vagrant/Code/documents/tests/Feature/FileTest.php:32

My User.php file extends the rewquired class, but for some reason my create method is returning a collection instead of authenticible.

Here is my TestCase.php file:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected function signIn($user = null) {

        $user = $user ?: create('App\User');

        $this->actingAs($user);

        return $this;
    }

}

What's wrong?

Please sign in or create an account to participate in this conversation.