Reputation: 19725
I am coding a Laravel package, and I have written functional testing. For that, I need to seed users, using factories.
In my config, I have :
'user' => [
'table' => 'users',
'primary_key' => 'id',
'foreign_key' => 'user_id',
'model' => App\Models\User::class,
],
But in my test package, App\Models\User is not defined, because it is only defined in main laravel project.
So, I tried to use use Illuminate\Foundation\Auth\User
instead of App\Models\User
in my tests, but I get a message :
InvalidArgumentException: Unable to locate factory for [Illuminate\Foundation\Auth\User].
Which I understand. But I cannot find a way to solve my issue a this point.
In my package's ServiceProvider, I have :
$this->publishes([__DIR__ . '/../database/seeders' => $this->app->databasePath().'/seeders'], 'laravel-tournaments');
$this->publishes([__DIR__.'/../database/factories' => $this->app->databasePath().'/factories'], 'laravel-tournaments');
$this->publishes([__DIR__.'/../config/laravel-tournaments.php' => config_path('laravel-tournaments.php')], 'laravel-tournaments');
Any idea ?
Upvotes: -1
Views: 158
Reputation: 19725
I found an answer examinating Laravel Permission package.
For functional testing, inside test folder, they put a TestModels/ folder with a copy of User class.
<?php
namespace Spatie\Permission\Tests\TestModels;
use Spatie\Permission\Traits\HasRoles;
class User extends UserWithoutHasRoles
{
use HasRoles;
}
With trait hasRoles
:
<?php
namespace Spatie\Permission\Tests\TestModels;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\Authorizable;
class UserWithoutHasRoles extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable;
use Authorizable;
protected $fillable = ['email'];
public $timestamps = false;
protected $table = 'users';
}
All the models than depends on User should also be duplicated there
Upvotes: 0