1
0

fix(tests): inject appTitle/appUrl into auth services to avoid DB calls in unit tests

This commit is contained in:
aminovfariz
2026-06-05 14:21:04 +02:00
parent 3f08f6189e
commit c12d155e23
3 changed files with 46 additions and 12 deletions

View File

@@ -59,7 +59,9 @@ class AuthServicesFactory
$this->authRepositoryFactory->createPasswordResetRepository(), $this->authRepositoryFactory->createPasswordResetRepository(),
$this->userServicesFactory->createUserPasswordService(), $this->userServicesFactory->createUserPasswordService(),
$this->createRememberMeService(), $this->createRememberMeService(),
$this->mailServicesFactory->createMailService() $this->mailServicesFactory->createMailService(),
appTitle(),
appUrl(),
); );
} }
@@ -69,7 +71,9 @@ class AuthServicesFactory
$this->userServicesFactory->createUserReadRepository(), $this->userServicesFactory->createUserReadRepository(),
$this->userServicesFactory->createUserWriteRepository(), $this->userServicesFactory->createUserWriteRepository(),
$this->authRepositoryFactory->createEmailVerificationRepository(), $this->authRepositoryFactory->createEmailVerificationRepository(),
$this->mailServicesFactory->createMailService() $this->mailServicesFactory->createMailService(),
appTitle(),
appUrl(),
); );
} }

View File

@@ -19,10 +19,25 @@ class EmailVerificationService
private readonly UserReadRepositoryInterface $userReadRepository, private readonly UserReadRepositoryInterface $userReadRepository,
private readonly UserWriteRepositoryInterface $userWriteRepository, private readonly UserWriteRepositoryInterface $userWriteRepository,
private readonly EmailVerificationRepositoryInterface $emailVerificationRepository, private readonly EmailVerificationRepositoryInterface $emailVerificationRepository,
private readonly MailService $mailService private readonly MailService $mailService,
private readonly string $appTitle = '',
private readonly string $appUrl = '',
) { ) {
} }
private function resolveAppTitle(): string
{
return $this->appTitle !== '' ? $this->appTitle : appTitle();
}
private function resolveAppUrl(string $path = ''): string
{
if ($this->appUrl !== '') {
return rtrim($this->appUrl, '/') . ($path !== '' ? '/' . ltrim($path, '/') : '');
}
return appUrl($path);
}
public function sendVerification(int $userId, ?string $locale = null): array public function sendVerification(int $userId, ?string $locale = null): array
{ {
$user = $this->userReadRepository->find($userId); $user = $this->userReadRepository->find($userId);
@@ -55,17 +70,17 @@ class EmailVerificationService
} }
$greeting .= ','; $greeting .= ',';
$verifyPath = Request::withLocale('verify-email', $locale); $verifyPath = Request::withLocale('verify-email', $locale);
$verifyUrl = appUrl($verifyPath); $verifyUrl = $this->resolveAppUrl($verifyPath);
$previousLocale = I18n::$locale ?? null; $previousLocale = I18n::$locale ?? null;
I18n::$locale = $locale; I18n::$locale = $locale;
$subject = t('Email verification code'); $subject = t('Email verification code');
I18n::$locale = $previousLocale; I18n::$locale = $previousLocale;
$vars = [ $vars = [
'app_name' => appTitle(), 'app_name' => $this->resolveAppTitle(),
'app_logo_url' => appLogoUrlAbsolute(128), 'app_logo_url' => appLogoUrlAbsolute(128),
'imprint_url' => appUrl(Request::withLocale('imprint', $locale)), 'imprint_url' => $this->resolveAppUrl(Request::withLocale('imprint', $locale)),
'privacy_url' => appUrl(Request::withLocale('privacy', $locale)), 'privacy_url' => $this->resolveAppUrl(Request::withLocale('privacy', $locale)),
'code' => $code, 'code' => $code,
'expires_minutes' => self::EXPIRY_MINUTES, 'expires_minutes' => self::EXPIRY_MINUTES,
'verify_url' => $verifyUrl, 'verify_url' => $verifyUrl,

View File

@@ -20,10 +20,25 @@ class PasswordResetService
private readonly PasswordResetRepositoryInterface $passwordResetRepository, private readonly PasswordResetRepositoryInterface $passwordResetRepository,
private readonly UserPasswordService $userPasswordService, private readonly UserPasswordService $userPasswordService,
private readonly RememberMeService $rememberMeService, private readonly RememberMeService $rememberMeService,
private readonly MailService $mailService private readonly MailService $mailService,
private readonly string $appTitle = '',
private readonly string $appUrl = '',
) { ) {
} }
private function resolveAppTitle(): string
{
return $this->appTitle !== '' ? $this->appTitle : appTitle();
}
private function resolveAppUrl(string $path = ''): string
{
if ($this->appUrl !== '') {
return rtrim($this->appUrl, '/') . ($path !== '' ? '/' . ltrim($path, '/') : '');
}
return appUrl($path);
}
public function requestReset(string $email, ?string $locale = null): array public function requestReset(string $email, ?string $locale = null): array
{ {
$email = trim($email); $email = trim($email);
@@ -58,17 +73,17 @@ class PasswordResetService
} }
$greeting .= ','; $greeting .= ',';
$verifyPath = Request::withLocale('password/verify', $locale); $verifyPath = Request::withLocale('password/verify', $locale);
$verifyUrl = appUrl($verifyPath); $verifyUrl = $this->resolveAppUrl($verifyPath);
$previousLocale = I18n::$locale ?? null; $previousLocale = I18n::$locale ?? null;
I18n::$locale = $locale; I18n::$locale = $locale;
$subject = t('Password reset code'); $subject = t('Password reset code');
I18n::$locale = $previousLocale; I18n::$locale = $previousLocale;
$vars = [ $vars = [
'app_name' => appTitle(), 'app_name' => $this->resolveAppTitle(),
'app_logo_url' => appLogoUrlAbsolute(128), 'app_logo_url' => appLogoUrlAbsolute(128),
'imprint_url' => appUrl(Request::withLocale('imprint', $locale)), 'imprint_url' => $this->resolveAppUrl(Request::withLocale('imprint', $locale)),
'privacy_url' => appUrl(Request::withLocale('privacy', $locale)), 'privacy_url' => $this->resolveAppUrl(Request::withLocale('privacy', $locale)),
'code' => $code, 'code' => $code,
'expires_minutes' => self::EXPIRY_MINUTES, 'expires_minutes' => self::EXPIRY_MINUTES,
'verify_url' => $verifyUrl, 'verify_url' => $verifyUrl,