Level 102
You can resolve it from the container like this
$service = resolve(UserService::class);
1 like
I have a class
class UserService
{
private UserRepositoryInterface $userRepository;
private AuthRepositoryInterface $authRepository;
public function __construct(UserRepositoryInterface $userRepository, AuthRepositoryInterface $authRepository)
{
$this->userRepository = $userRepository;
$this->authRepository = $authRepository;
}
public function generateEmailVerificationToken($user): int|JsonResponse
{
$token = random_int(000000, 999999);
$this->authRepository->storeVerifyEmailToken($token, $user);
return $token;
}
}
Now, I am testing,
public function test_user_email_verified_successfully()
{
$user = User::factory()->create();
//this line giving me issue
$token = $this->userService->generateEmailVerificationToken($user);
$rawData = [
'user' => $user->email,
'token' => $token
];
$this->apiPost('/email/verify', $rawData)
->assertStatus(200)
->assertJsonStructure([
'user' => [
'id',
'name',
'email',
'role_id',
'role',
'permissions',
'created_at',
'updated_at',
]
]);
}
What is the best way to call this generateEmailVerificationToken? I tried by
$service = new UserService();
But this UserService need UserRepositoryInterface and AuthRepositoryInterface
You can resolve it from the container like this
$service = resolve(UserService::class);
Please or to participate in this conversation.