|
12 | 12 | namespace Symfony\Bundle\MakerBundle\Maker;
|
13 | 13 |
|
14 | 14 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
| 15 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
15 | 16 | use Symfony\Bundle\MakerBundle\ConsoleStyle;
|
16 | 17 | use Symfony\Bundle\MakerBundle\DependencyBuilder;
|
17 | 18 | use Symfony\Bundle\MakerBundle\Generator;
|
18 | 19 | use Symfony\Bundle\MakerBundle\InputConfiguration;
|
| 20 | +use Symfony\Bundle\MakerBundle\Maker\Common\CanGenerateTestsTrait; |
19 | 21 | use Symfony\Bundle\MakerBundle\Str;
|
20 | 22 | use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;
|
21 | 23 | use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
|
|
34 | 36 | */
|
35 | 37 | final class MakeController extends AbstractMaker
|
36 | 38 | {
|
| 39 | + use CanGenerateTestsTrait; |
| 40 | + |
37 | 41 | public function __construct(private ?PhpCompatUtil $phpCompatUtil = null)
|
38 | 42 | {
|
39 | 43 | if (null !== $phpCompatUtil) {
|
@@ -63,6 +67,13 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
|
63 | 67 | ->addOption('invokable', 'i', InputOption::VALUE_NONE, 'Use this option to create an invokable controller')
|
64 | 68 | ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
|
65 | 69 | ;
|
| 70 | + |
| 71 | + $this->configureCommandWithTestsOption($command); |
| 72 | + } |
| 73 | + |
| 74 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
| 75 | + { |
| 76 | + $this->interactSetGenerateTests($input, $io); |
66 | 77 | }
|
67 | 78 |
|
68 | 79 | public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
|
@@ -121,6 +132,24 @@ class: $controllerClassName,
|
121 | 132 | );
|
122 | 133 | }
|
123 | 134 |
|
| 135 | + if ($this->shouldGenerateTests()) { |
| 136 | + $testClassData = ClassData::create( |
| 137 | + class: \sprintf('Tests\Controller\%s', $controllerClassData->getClassName(relative: true, withoutSuffix: true)), |
| 138 | + suffix: 'ControllerTest', |
| 139 | + extendsClass: WebTestCase::class, |
| 140 | + useStatements: [ |
| 141 | + ] |
| 142 | + ); |
| 143 | + |
| 144 | + $generator->generateClassFromClassData($testClassData, 'controller/test/Test.tpl.php', [ |
| 145 | + 'route_path' => Str::asRoutePath($controllerClassData->getClassName(relative: true, withoutSuffix: true)), |
| 146 | + ]); |
| 147 | + |
| 148 | + if (!class_exists(WebTestCase::class)) { |
| 149 | + $io->caution('You\'ll need to install the `symfony/test-pack` to execute the tests for your new controller.'); |
| 150 | + } |
| 151 | + } |
| 152 | + |
124 | 153 | $generator->writeChanges();
|
125 | 154 |
|
126 | 155 | $this->writeSuccessMessage($io);
|
|
0 commit comments