2020use Symfony \Bundle \MakerBundle \Maker \Common \CanGenerateTestsTrait ;
2121use Symfony \Bundle \MakerBundle \Str ;
2222use Symfony \Bundle \MakerBundle \Util \ClassSource \Model \ClassData ;
23+ use Symfony \Bundle \MakerBundle \Util \NamespacesHelper ;
2324use Symfony \Bundle \MakerBundle \Util \PhpCompatUtil ;
2425use Symfony \Bundle \TwigBundle \TwigBundle ;
2526use Symfony \Component \Console \Command \Command ;
@@ -43,8 +44,10 @@ final class MakeController extends AbstractMaker
4344 private bool $ usesTwigTemplate ;
4445 private string $ twigTemplatePath ;
4546
46- public function __construct (private ?PhpCompatUtil $ phpCompatUtil = null )
47- {
47+ public function __construct (
48+ private NamespacesHelper $ namespacesHelper ,
49+ private ?PhpCompatUtil $ phpCompatUtil = null ,
50+ ) {
4851 if (null !== $ phpCompatUtil ) {
4952 @trigger_deprecation (
5053 'symfony/maker-bundle ' ,
@@ -82,7 +85,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
8285 $ this ->isInvokable = (bool ) $ input ->getOption ('invokable ' );
8386
8487 $ controllerClass = $ input ->getArgument ('controller-class ' );
85- $ controllerClassName = \sprintf ('Controller \%s ' , $ controllerClass );
88+ $ controllerClassName = \sprintf ('%s \%s ' , $ this -> namespacesHelper -> getControllerNamespace () , $ controllerClass );
8689
8790 // If the class name provided is absolute, we do not assume it will live in src/Controller
8891 // e.g. src/Custom/Location/For/MyController instead of src/Controller/MyController
@@ -92,6 +95,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
9295
9396 $ this ->controllerClassData = ClassData::create (
9497 class: $ controllerClassName ,
98+ rootNamespace: $ this ->namespacesHelper ->getRootNamespace (),
9599 suffix: 'Controller ' ,
96100 extendsClass: AbstractController::class,
97101 useStatements: [
@@ -138,8 +142,15 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
138142 }
139143
140144 if ($ this ->shouldGenerateTests ()) {
145+ $ testClassName =\sprintf (
146+ '%s\%s\%s ' ,
147+ $ this ->namespacesHelper ->getTestNamespace (),
148+ $ this ->namespacesHelper ->getControllerNamespace (),
149+ $ this ->controllerClassData ->getClassName (relative: true , withoutSuffix: true )
150+ );
141151 $ testClassData = ClassData::create (
142- class: \sprintf ('Tests\Controller\%s ' , $ this ->controllerClassData ->getClassName (relative: true , withoutSuffix: true )),
152+ class: $ testClassName ,
153+ rootNamespace: $ this ->namespacesHelper ->getRootNamespace (),
143154 suffix: 'ControllerTest ' ,
144155 extendsClass: WebTestCase::class,
145156 );
0 commit comments