1111
1212namespace Symfony \Bundle \MakerBundle \Maker ;
1313
14+ use Doctrine \ORM \EntityManager ;
1415use Doctrine \ORM \EntityManagerInterface ;
1516use PhpParser \Builder \Param ;
1617use Symfony \Bridge \Twig \AppVariable ;
1718use Symfony \Bridge \Twig \Mime \TemplatedEmail ;
1819use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
20+ use Symfony \Bundle \FrameworkBundle \KernelBrowser ;
21+ use Symfony \Bundle \FrameworkBundle \Test \WebTestCase ;
1922use Symfony \Bundle \MakerBundle \ConsoleStyle ;
2023use Symfony \Bundle \MakerBundle \DependencyBuilder ;
2124use Symfony \Bundle \MakerBundle \Doctrine \DoctrineHelper ;
2629use Symfony \Bundle \MakerBundle \FileManager ;
2730use Symfony \Bundle \MakerBundle \Generator ;
2831use Symfony \Bundle \MakerBundle \InputConfiguration ;
32+ use Symfony \Bundle \MakerBundle \Maker \Common \CanGenerateTestsTrait ;
2933use Symfony \Bundle \MakerBundle \Maker \Common \UidTrait ;
3034use Symfony \Bundle \MakerBundle \Security \InteractiveSecurityHelper ;
3135use Symfony \Bundle \MakerBundle \Util \ClassNameDetails ;
5155use Symfony \Component \OptionsResolver \OptionsResolver ;
5256use Symfony \Component \PasswordHasher \Hasher \UserPasswordHasherInterface ;
5357use Symfony \Component \Routing \Attribute \Route ;
58+ use Symfony \Component \Routing \Route as RouteObject ;
59+ use Symfony \Component \Routing \RouterInterface ;
5460use Symfony \Component \Translation \Translator ;
5561use Symfony \Component \Validator \Constraints \Length ;
5662use Symfony \Component \Validator \Constraints \NotBlank ;
8187 */
8288class MakeResetPassword extends AbstractMaker
8389{
90+ use CanGenerateTestsTrait;
8491 use UidTrait;
8592
8693 private string $ fromEmailAddress ;
8794 private string $ fromEmailName ;
8895 private string $ controllerResetSuccessRedirect ;
96+ private ?RouteObject $ controllerResetSuccessRoute = null ;
8997 private string $ userClass ;
9098 private string $ emailPropertyName ;
9199 private string $ emailGetterMethodName ;
@@ -95,6 +103,7 @@ public function __construct(
95103 private FileManager $ fileManager ,
96104 private DoctrineHelper $ doctrineHelper ,
97105 private EntityClassGenerator $ entityClassGenerator ,
106+ private ?RouterInterface $ router = null ,
98107 ) {
99108 }
100109
@@ -115,6 +124,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
115124 ;
116125
117126 $ this ->addWithUuidOption ($ command );
127+ $ this ->configureCommandWithTestsOption ($ command );
118128 }
119129
120130 public function configureDependencies (DependencyBuilder $ dependencies ): void
@@ -172,6 +182,10 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
172182 Validator::notBlank (...)
173183 );
174184
185+ if ($ this ->router instanceof RouterInterface) {
186+ $ this ->controllerResetSuccessRoute = $ this ->router ->getRouteCollection ()->get ($ this ->controllerResetSuccessRedirect );
187+ }
188+
175189 $ io ->section ('- Email - ' );
176190 $ emailText [] = 'These are used to generate the email code. Don \'t worry, you can change them in the code later! ' ;
177191 $ io ->text ($ emailText );
@@ -187,6 +201,8 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
187201 null ,
188202 Validator::notBlank (...)
189203 );
204+
205+ $ this ->interactSetGenerateTests ($ input , $ io );
190206 }
191207
192208 public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
@@ -334,6 +350,44 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
334350 'resetPassword/twig_reset.tpl.php '
335351 );
336352
353+ // Generate PHPUnit tests
354+ if ($ this ->shouldGenerateTests ()) {
355+ $ testClassDetails = $ generator ->createClassNameDetails (
356+ 'ResetPasswordControllerTest ' ,
357+ 'Test \\' ,
358+ );
359+
360+ $ userRepositoryDetails = $ generator ->createClassNameDetails (
361+ sprintf ('%sRepository ' , $ userClassNameDetails ->getShortName ()),
362+ 'Repository \\'
363+ );
364+
365+ $ useStatements = new UseStatementGenerator ([
366+ $ userClassNameDetails ->getFullName (),
367+ $ userRepositoryDetails ->getFullName (),
368+ EntityManager::class,
369+ KernelBrowser::class,
370+ WebTestCase::class,
371+ UserPasswordHasherInterface::class,
372+ ]);
373+
374+ $ generator ->generateFile (
375+ targetPath: sprintf ('tests/%s.php ' , $ testClassDetails ->getShortName ()),
376+ templateName: 'resetPassword/Test.ResetPasswordController.tpl.php ' ,
377+ variables: [
378+ 'use_statements ' => $ useStatements ,
379+ 'user_short_name ' => $ userClassNameDetails ->getShortName (),
380+ 'user_repo_short_name ' => $ userRepositoryDetails ->getShortName (),
381+ 'success_route_path ' => null !== $ this ->controllerResetSuccessRoute ? $ this ->controllerResetSuccessRoute ->getPath () : '/ ' ,
382+ 'from_email ' => $ this ->fromEmailAddress ,
383+ ],
384+ );
385+
386+ if (!class_exists (WebTestCase::class)) {
387+ $ io ->caution ('You \'ll need to install the `symfony/test-pack` to execute the tests for your new controller. ' );
388+ }
389+ }
390+
337391 $ generator ->writeChanges ();
338392
339393 $ this ->writeSuccessMessage ($ io );
0 commit comments