Can anyone help?
Oct 15, 2021
2
Level 2
Laravel Testing reading many to many relationships
Ok so Im new to testing but I am trying to test if I can read from a db table which shares a many to many with another but only where certain conditions are met, IE Can I get all users who have role ID 1 or 2 .
My setup: -Roles and Users are the 2 tables which share a many to many connection.
- Roles is being filled through the use of a RoleSeeder:
public function run()
{
DB::table('roles')->delete();
$roles = [
['id' => 1, 'name' => 'superuser'],
['id' => 2, 'name' => 'staff'],
['id' => 3, 'name' => 'venueadmin'],
['id' => 4, 'name' => 'venuestaff'],
];
Role::insert($roles);
}
- Users is being filled through this seeder:
public function run()
{
DB::table('users')->delete();
DB::table('role_user')->delete();
User::factory()
->count(100)
->create();
$roles = Role::all();
User::all()->each(function ($user) use ($roles) {
$user->roles()->attach(
$roles->random(rand(1, 2))->pluck('id')->toArray()
);
});
}
- the UsersController is being called via API
- The index function of UsersController looks like this:
public function index()
{
$users = User::whereHas('roles', function ($query) {
$query->where('role_id', '1')
->orWhere('role_id','2');
})->with('roles')->get();
return $users;
}
Now, stop me if I'm wrong but here are my thought processes/assumptions:
- Before I can read anything from Users, I need to enter some test data. This means calling the User factory.
- This will only bring back users, not their relationships with roles
- The users factory (I think) is not able to establish the relationship I need, only the seeder will
- I can't call seeder from the test (Or can I?).
So, how can I enter a test user, with one or two roles and read it back testing that only users with certain roles are grabbed? Here is what I had so far in the test:
$this->withoutExceptionHandling();
$this->postJson('/api/users', [
'firstname' => 'Fake',
'lastname' => 'Person',
'email' => '[email protected]',
'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
'remember_token' => Str::random(10),
'roles' => '1','2'
]);
$user = $this->getJson('/api/users');
dd($user);
That DD returned null
Please or to participate in this conversation.