Skip to content

Commit de15602

Browse files
authored
feature #1603 [make:controller] add ability to create controller with tests
1 parent 3280a5d commit de15602

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/Maker/MakeController.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
namespace Symfony\Bundle\MakerBundle\Maker;
1313

1414
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1516
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1617
use Symfony\Bundle\MakerBundle\DependencyBuilder;
1718
use Symfony\Bundle\MakerBundle\Generator;
1819
use Symfony\Bundle\MakerBundle\InputConfiguration;
20+
use Symfony\Bundle\MakerBundle\Maker\Common\CanGenerateTestsTrait;
1921
use Symfony\Bundle\MakerBundle\Str;
2022
use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData;
2123
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
@@ -34,6 +36,8 @@
3436
*/
3537
final class MakeController extends AbstractMaker
3638
{
39+
use CanGenerateTestsTrait;
40+
3741
public function __construct(private ?PhpCompatUtil $phpCompatUtil = null)
3842
{
3943
if (null !== $phpCompatUtil) {
@@ -63,6 +67,13 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
6367
->addOption('invokable', 'i', InputOption::VALUE_NONE, 'Use this option to create an invokable controller')
6468
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeController.txt'))
6569
;
70+
71+
$this->configureCommandWithTestsOption($command);
72+
}
73+
74+
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
75+
{
76+
$this->interactSetGenerateTests($input, $io);
6677
}
6778

6879
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
@@ -121,6 +132,24 @@ class: $controllerClassName,
121132
);
122133
}
123134

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+
124153
$generator->writeChanges();
125154

126155
$this->writeSuccessMessage($io);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?= "<?php\n" ?>
2+
3+
namespace <?= $class_data->getNamespace(); ?>;
4+
5+
<?= $class_data->getUseStatements(); ?>
6+
7+
<?= $class_data->getClassDeclaration(); ?>
8+
{
9+
public function testIndex(): void
10+
{
11+
$client = static::createClient();
12+
$client->request('GET', '<?= $route_path; ?>');
13+
14+
self::assertResponseIsSuccessful();
15+
}
16+
}

tests/Maker/MakeControllerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ public function getTestDetails(): \Generator
4747
}),
4848
];
4949

50+
yield 'it_generates_a_controller-with-tests' => [$this->createMakerTest()
51+
->addExtraDependencies('symfony/test-pack')
52+
->run(function (MakerTestRunner $runner) {
53+
$output = $runner->runMaker([
54+
'FooBar', // controller class name
55+
'y', // create tests
56+
]);
57+
58+
$this->assertStringContainsString('src/Controller/FooBarController.php', $output);
59+
$this->assertStringContainsString('tests/Controller/FooBarControllerTest.php', $output);
60+
61+
$this->assertFileExists($runner->getPath('src/Controller/FooBarController.php'));
62+
$this->assertFileExists($runner->getPath('tests/Controller/FooBarControllerTest.php'));
63+
64+
$this->runControllerTest($runner, 'it_generates_a_controller.php');
65+
}),
66+
];
67+
5068
yield 'it_generates_a_controller__no_input' => [$this->createMakerTest()
5169
->run(function (MakerTestRunner $runner) {
5270
$output = $runner->runMaker([], 'FooBar');

0 commit comments

Comments
 (0)