Hello!
I created a model called Permissions, and when I noticed the typo, I renamed both the Permissons.php & the class, as well as the factory I had created for that model, but now when I run my tests, I get an error saying that PermissionFactory was not found. Any ideas on how I can solve this issue?
App\Models\Permission
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Permission extends Model
{
use HasFactory;
public $timestamps = false;
public function role()
{
return $this->belongsTo(Role::class);
}
}
Database\Factories
<?php
namespace Database\Factories;
use App\Models\Permission;
use Illuminate\Database\Eloquent\Factories\Factory;
class PermissionFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Permission::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'permission' => $this->faker->name(),
'value' => 1,
'role_id' => Role::factory()
];
}
}
The test methods, that fails is: testRoleCanHavePermission & testAccessPermissionRelationshipThroughUser
Test\Feature\Roles
<?php
namespace Tests\Feature\Roles;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class UserRolesTest extends TestCase
{
use RefreshDatabase;
/**
* Tests that a user can have roles
*
* @return void
*/
public function testUserCanHaveRoles()
{
$user = User::factory()->hasAttached(Role::factory()->count(2))->create();
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->roles);
$this->assertCount(2, $user->roles);
}
/**
* Tests that a role can have permissions
*
* @returns void
*/
public function testRoleCanHavePermission()
{
$role = Role::factory()->has(Permission::factory()->count(2))->create();
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $role->permissions);
$this->assertCount(2, $role->permissions);
}
/**
* Tests that we can access the permissions of the role through the user
*
* returns void
*/
public function testAccessPermissionRelationshipThroughUser()
{
$user = User::factory()->hasAttached(Role::factory()->has(Permission::factory()->count(2))->count(2))->create();
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->roles);
$this->assertCount(2, $user->roles);
foreach ($user->roles as $role) {
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $role->permissions);
$this->assertCount(2, $role->permissions);
}
}
/**
* Tests that the migrations migrate the expected columns of the table 'roles'
*
* returns void
*/
public function testRoleTableHasExpectedColumns()
{
$this->assertTrue(
Schema::hasColumns('roles', [
'id','name'
]), 1);
}
/**
* Tests that the migrations migrate the expected columns of the table 'role_users'
*
* returns void
*/
public function testRoleUserTableHasExpectedColumns()
{
$this->assertTrue(
Schema::hasColumns('role_user', [
'id','user_id', 'role_id'
]), 1);
}
/**
* Tests that the migrations migrate the expected columns of the table 'permissions'
*
* returns void
*/
public function testPermissionsTableHasExpectedColumns()
{
$this->assertTrue(
Schema::hasColumns('permissions', [
'id','permission', 'role_id', 'value'
]), 1);
}
}