Skip to content

[make:registration-form] Translate reasons for VerifyEmailBundle if translator available #1076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Maker/MakeRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\Validation;
use Symfony\Contracts\Translation\TranslatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\Model\VerifyEmailSignatureComponents;
use SymfonyCasts\Bundle\VerifyEmail\SymfonyCastsVerifyEmailBundle;
Expand Down Expand Up @@ -324,6 +326,10 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
}
}

if ($isTranslatorAvailable = class_exists(Translator::class)) {
$useStatements[] = TranslatorInterface::class;
}

$generator->generateController(
$controllerClassNameDetails->getFullName(),
'registration/RegistrationController.tpl.php',
Expand All @@ -348,6 +354,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'password_hasher_class_details' => ($passwordClassDetails = $generator->createClassNameDetails($passwordHasher, '\\')),
'password_hasher_variable_name' => str_replace('Interface', '', sprintf('$%s', lcfirst($passwordClassDetails->getShortName()))), // @legacy see passwordHasher conditional above
'use_password_hasher' => UserPasswordHasherInterface::class === $passwordHasher, // @legacy see passwordHasher conditional above
'translator_available' => $isTranslatorAvailable,
],
$userRepoVars
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function register(Request $request, <?= $password_hasher_class_details->g
<?php if ($will_verify_email): ?>

<?= $generator->generateRouteForControllerMethod('/verify/email', 'app_verify_email') ?>
public function verifyUserEmail(Request $request<?= $verify_email_anonymously ? sprintf(', %s %s', $repository_class_name, $repository_var) : null ?>): Response
public function verifyUserEmail(Request $request<?php if ($translator_available): ?>, TranslatorInterface $translator<?php endif ?><?= $verify_email_anonymously ? sprintf(', %s %s', $repository_class_name, $repository_var) : null ?>): Response
{
<?php if (!$verify_email_anonymously): ?>
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
Expand Down Expand Up @@ -101,7 +101,7 @@ public function verifyUserEmail(Request $request<?= $verify_email_anonymously ?
try {
$this->emailVerifier->handleEmailConfirmation($request, <?= $verify_email_anonymously ? '$user' : '$this->getUser()' ?>);
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', $exception->getReason());
$this->addFlash('verify_email_error', <?php if ($translator_available): ?>$translator->trans($exception->getReason(), [], 'VerifyEmailBundle')<?php else: ?>$exception->getReason()<?php endif ?>);

return $this->redirectToRoute('<?= $route_name ?>');
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Maker/MakeRegistrationFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,37 @@ public function getTestDetails()
$this->runRegistrationTest($runner, 'it_generates_registration_form_with_verification.php');
}),
];

yield 'it_generates_registration_form_with_verification_and_translator' => [$this->createRegistrationFormTest()
->setRequiredPhpVersion(70200)
->addExtraDependencies('symfonycasts/verify-email-bundle')
// needed for internal functional test
->addExtraDependencies('symfony/web-profiler-bundle', 'mailer', 'symfony/translation')
->run(function (MakerTestRunner $runner) {
$runner->writeFile(
'config/packages/mailer.yaml',
Yaml::dump(['framework' => [
'mailer' => ['dsn' => 'null://null'],
]])
);

$this->makeUser($runner);

$output = $runner->runMaker([
'n', // add UniqueEntity
'y', // verify user
'y', // require authentication to verify user email
'[email protected]', // from email address
'SymfonyCasts', // From Name
'n', // no authenticate after
0, // route number to redirect to
]);

$this->assertStringContainsString('Success', $output);

$this->runRegistrationTest($runner, 'it_generates_registration_form_with_verification.php');
}),
];
}

private function makeUser(MakerTestRunner $runner, string $identifier = 'email')
Expand Down