No Role Named '' in phpunit
Opened this issue · 8 comments
There is a problem in $user->syncRoles($data['type']); it always return no role named.. when you try to access the roles dd(Role::all()) it return all roles..
Why wouldn't Role::all()
not return all roles? It's not based on the currently logged-in user.
in unit testing.. when i dump Role::all(), it returns all the roles but when the phpunit reads $user->syncRoles($data['type']) to sync the roles.. it returns error.. like no role named 'admin' when admin role is already present in Role::all()
i found the error here in Tests\Feature\Frontend\RegistrationTest::a_user_can_register_an_account
It sounds like it's probably a problem with your local environment, if there was a problem with this code we would have found it a long time ago. Unless you can tell me how to reproduce it with a fresh installation.
local environment? it seems that i cant find any similar problems here..
I'll be honest I don't understand the issue.
oh.. i mean if run the phpunit to test my code.. i got error in this line..
/** @test */
public function a_user_can_register_an_account()
{
$this->post('/register', [
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => 'OC4Nzu270N!QBVi%U%qX',
'password_confirmation' => 'OC4Nzu270N!QBVi%U%qX',
'terms' => '1',
])->assertRedirect(route(homeRoute())); //ERROR WHEN REGISTERING
$user = resolve(UserService::class)
->where('email', 'john@example.com')
->firstOrFail();
$this->assertSame($user->name, 'John Doe');
$this->assertTrue(Hash::check('OC4Nzu270N!QBVi%U%qX', $user->password));
}
\App\Domains\Auth\Services\UserService
/**
* @param array $data
*
* @return User
* @throws GeneralException
* @throws \Throwable
*/
public function store(array $data = []): User
{
DB::beginTransaction();
try {
$user = $this->createUser([
'type' => $data['type'],
'name' => $data['name'],
'email' => $data['email'],
'password' => $data['password'],
'email_verified_at' => isset($data['email_verified']) && $data['email_verified'] === '1' ? now() : null,
'active' => isset($data['active']) && $data['active'] === '1',
]);
$user->syncRoles($data['type']); //ERROR IN THIS LINE
} catch (Exception $e) {
it says No Role Named error.. but if i do this..
/** @test */
public function a_user_can_register_an_account()
{
dd(Role::all());
$this->post('/register', [
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => 'OC4Nzu270N!QBVi%U%qX',
'password_confirmation' => 'OC4Nzu270N!QBVi%U%qX',
'terms' => '1',
])->assertRedirect(route(homeRoute())); //ERROR WHEN REGISTERING
$user = resolve(UserService::class)
->where('email', 'john@example.com')
->firstOrFail();
$this->assertSame($user->name, 'John Doe');
$this->assertTrue(Hash::check('OC4Nzu270N!QBVi%U%qX', $user->password));
}
it returns all roles.. so i dont understand why it has an error No Role when there are roles in the DB
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.