|
12 | 12 | namespace Symfony\Bundle\MakerBundle\Maker;
|
13 | 13 |
|
14 | 14 | use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
|
| 15 | +use Doctrine\ORM\EntityManager; |
15 | 16 | use Doctrine\ORM\EntityManagerInterface;
|
16 | 17 | use Doctrine\ORM\Mapping\Column;
|
17 | 18 | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
18 | 19 | use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
19 | 20 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
| 21 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
20 | 22 | use Symfony\Bundle\MakerBundle\ConsoleStyle;
|
21 | 23 | use Symfony\Bundle\MakerBundle\DependencyBuilder;
|
22 | 24 | use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
|
23 | 25 | use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
|
24 | 26 | use Symfony\Bundle\MakerBundle\FileManager;
|
25 | 27 | use Symfony\Bundle\MakerBundle\Generator;
|
26 | 28 | use Symfony\Bundle\MakerBundle\InputConfiguration;
|
| 29 | +use Symfony\Bundle\MakerBundle\Maker\Common\CanGenerateTestsTrait; |
27 | 30 | use Symfony\Bundle\MakerBundle\Renderer\FormTypeRenderer;
|
28 | 31 | use Symfony\Bundle\MakerBundle\Security\InteractiveSecurityHelper;
|
29 | 32 | use Symfony\Bundle\MakerBundle\Security\Model\Authenticator;
|
|
68 | 71 | */
|
69 | 72 | final class MakeRegistrationForm extends AbstractMaker
|
70 | 73 | {
|
| 74 | + use CanGenerateTestsTrait; |
| 75 | + |
71 | 76 | private string $userClass;
|
72 | 77 | private string $usernameField;
|
73 | 78 | private string $passwordField;
|
@@ -104,6 +109,8 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
|
104 | 109 | $command
|
105 | 110 | ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeRegistrationForm.txt'))
|
106 | 111 | ;
|
| 112 | + |
| 113 | + $this->addWithTestsOption($command); |
107 | 114 | }
|
108 | 115 |
|
109 | 116 | public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
|
@@ -180,6 +187,8 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
|
180 | 187 | $routeNames = array_keys($this->router->getRouteCollection()->all());
|
181 | 188 | $this->redirectRouteName = $io->choice('What route should the user be redirected to after registration?', $routeNames);
|
182 | 189 | }
|
| 190 | + |
| 191 | + $this->interactSetGenerateTests($input, $io); |
183 | 192 | }
|
184 | 193 |
|
185 | 194 | /** @param array<string, mixed> $securityData */
|
@@ -400,6 +409,33 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
|
400 | 409 | $this->fileManager->dumpFile($classDetails->getPath(), $userManipulator->getSourceCode());
|
401 | 410 | }
|
402 | 411 |
|
| 412 | + // Generate PHPUnit Tests |
| 413 | + if ($this->shouldGenerateTests()) { |
| 414 | + $testClassDetails = $generator->createClassNameDetails( |
| 415 | + 'RegistrationControllerTest', |
| 416 | + 'Test\\' |
| 417 | + ); |
| 418 | + |
| 419 | + $useStatements = new UseStatementGenerator([ |
| 420 | + EntityManager::class, |
| 421 | + TemplatedEmail::class, |
| 422 | + WebTestCase::class, |
| 423 | + $userRepoVars['repository_full_class_name'], |
| 424 | + ]); |
| 425 | + |
| 426 | + $generator->generateFile( |
| 427 | + targetPath: sprintf('tests/%s.php', $testClassDetails->getShortName()), |
| 428 | + templateName: 'registration/Test.WithVerify.tpl.php', |
| 429 | + variables: array_merge([ |
| 430 | + 'use_statements' => $useStatements, |
| 431 | + ], $userRepoVars) |
| 432 | + ); |
| 433 | + |
| 434 | + if (!class_exists(WebTestCase::class)) { |
| 435 | + $io->caution('You\'ll need to install the `symfony/test-pack` to execute the tests for your new controller.'); |
| 436 | + } |
| 437 | + } |
| 438 | + |
403 | 439 | $generator->writeChanges();
|
404 | 440 |
|
405 | 441 | $this->writeSuccessMessage($io);
|
|
0 commit comments