Skip to content

Commit da4673f

Browse files
committed
feature #810 Tweaks to the new make:test (weaverryan)
This PR was squashed before being merged into the 1.0-dev branch. Discussion ---------- Tweaks to the new make:test This follows #807 - I played with it locally and had some minor tweaks :). Cheers! Commits ------- 70f63b0 Tweaks to the new make:test
2 parents ed82536 + 70f63b0 commit da4673f

File tree

7 files changed

+102
-31
lines changed

7 files changed

+102
-31
lines changed

src/Maker/MakeTest.php

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Bundle\MakerBundle\Generator;
2020
use Symfony\Bundle\MakerBundle\InputAwareMakerInterface;
2121
use Symfony\Bundle\MakerBundle\InputConfiguration;
22+
use Symfony\Bundle\MakerBundle\Validator;
2223
use Symfony\Component\BrowserKit\History;
2324
use Symfony\Component\Console\Command\Command;
2425
use Symfony\Component\Console\Input\InputArgument;
@@ -77,43 +78,57 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
7778
}
7879

7980
$command
80-
->addArgument('name', InputArgument::OPTIONAL, 'The name of the test class (e.g. <fg=yellow>BlogPostTest</>)')
8181
->addArgument('type', InputArgument::OPTIONAL, 'The type of test: '.implode(', ', $typesDesc))
82+
->addArgument('name', InputArgument::OPTIONAL, 'The name of the test class (e.g. <fg=yellow>BlogPostTest</>)')
8283
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeTest.txt').implode("\n", $typesHelp));
8384

85+
$inputConf->setArgumentAsNonInteractive('name');
8486
$inputConf->setArgumentAsNonInteractive('type');
8587
}
8688

8789
public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
8890
{
8991
/* @deprecated remove the following block when removing make:unit-test and make:functional-test */
90-
$currentCommand = $input->getFirstArgument();
91-
switch ($currentCommand) {
92-
case 'make:unit-test':
93-
$input->setArgument('type', 'TestCase');
94-
$io->caution('The "make:unit-test" command is deprecated, use "make:test" instead.');
95-
96-
return;
97-
98-
case 'make:functional-test':
99-
$input->setArgument('type', trait_exists(PantherTestCaseTrait::class) ? 'WebTestCase' : 'PantherTestCase');
100-
$io->caution('The "make:functional-test" command is deprecated, use "make:test" instead.');
101-
102-
return;
103-
}
92+
$this->handleDeprecatedMakerCommands($input, $io);
10493

10594
if (null !== $type = $input->getArgument('type')) {
10695
if (!isset(self::DESCRIPTIONS[$type])) {
10796
throw new RuntimeCommandException(sprintf('The test type must be one of "%s", "%s" given.', implode('", "', array_keys(self::DESCRIPTIONS)), $type));
10897
}
98+
} else {
99+
$input->setArgument(
100+
'type',
101+
$io->choice('Which test type would you like?', self::DESCRIPTIONS)
102+
);
103+
}
109104

110-
return;
105+
if ('ApiTestCase' === $input->getArgument('type') && !class_exists(ApiTestCase::class)) {
106+
$io->warning([
107+
'API Platform is required for this test type. Install it with',
108+
'composer require api',
109+
]);
111110
}
112111

113-
$input->setArgument(
114-
'type',
115-
$io->choice('Which test type would you like?', self::DESCRIPTIONS)
116-
);
112+
if ('PantherTestCase' === $input->getArgument('type') && !trait_exists(PantherTestCaseTrait::class)) {
113+
$io->warning([
114+
'symfony/panther is required for this test type. Install it with',
115+
'composer require symfony/panther --dev',
116+
]);
117+
}
118+
119+
if (null === $input->getArgument('name')) {
120+
$io->writeln([
121+
'',
122+
'Choose a class name for your test, like:',
123+
' * <fg=yellow>UtilTest</> (to create tests/UtilTest.php)',
124+
' * <fg=yellow>Service\\UtilTest</> (to create tests/Service/UtilTest.php)',
125+
' * <fg=yellow>\\App\Tests\\Service\\UtilTest</> (to create tests/Service/UtilTest.php)',
126+
]);
127+
128+
$nameArgument = $command->getDefinition()->getArgument('name');
129+
$value = $io->ask($nameArgument->getDescription(), $nameArgument->getDefault(), [Validator::class, 'notBlank']);
130+
$input->setArgument($nameArgument->getName(), $value);
131+
}
117132
}
118133

119134
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
@@ -186,4 +201,23 @@ public function configureDependencies(DependencyBuilder $dependencies, InputInte
186201
return;
187202
}
188203
}
204+
205+
/**
206+
* @deprecated
207+
*/
208+
private function handleDeprecatedMakerCommands(InputInterface $input, ConsoleStyle $io): void
209+
{
210+
$currentCommand = $input->getFirstArgument();
211+
switch ($currentCommand) {
212+
case 'make:unit-test':
213+
$input->setArgument('type', 'TestCase');
214+
$io->warning('The "make:unit-test" command is deprecated, use "make:test" instead.');
215+
break;
216+
217+
case 'make:functional-test':
218+
$input->setArgument('type', trait_exists(PantherTestCaseTrait::class) ? 'WebTestCase' : 'PantherTestCase');
219+
$io->warning('The "make:functional-test" command is deprecated, use "make:test" instead.');
220+
break;
221+
}
222+
}
189223
}

src/Resources/help/MakeTest.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The <info>%command.name%</info> command generates a new test class.
22

3-
<info>php %command.full_name% BlogPostTest TestCase</info>
3+
<info>php %command.full_name% TestCase BlogPostTest</info>
44

5-
If the first argument is missing, the command will ask for the class name interactively.
5+
If the first argument is missing, the command will ask for the test type interactively.
66

7-
If the second argument is missing, the command will ask for the test type interactively.
7+
If the second argument is missing, the command will ask for the class name interactively.

src/Resources/skeleton/test/KernelTestCase.tpl.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public function testSomething(): void
1111
$kernel = self::bootKernel();
1212

1313
$this->assertSame('test', $kernel->getEnvironment());
14+
//$routerService = self::$container->get('router');
15+
//$myCustomService = self::$container->get(CustomService::class);
1416
}
1517
}

src/Test/MakerTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ protected function executeMakerCommand(MakerTestDetails $testDetails)
4141
throw new \LogicException('The MakerTestCase cannot be run as the Process component is not installed. Try running "compose require --dev symfony/process".');
4242
}
4343

44+
if ($testDetails->shouldSkip()) {
45+
$this->markTestSkipped($testDetails->getSkipMessage());
46+
}
47+
4448
if (!$testDetails->isSupportedByCurrentPhpVersion()) {
4549
$this->markTestSkipped();
4650
}

src/Test/MakerTestDetails.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ final class MakerTestDetails
4848

4949
private $guardAuthenticators = [];
5050

51+
private $shouldSkip = false;
52+
private $skipMessage;
53+
5154
/**
5255
* @return static
5356
*/
@@ -330,4 +333,22 @@ public function getRequiredPackageVersions(): array
330333
{
331334
return $this->requiredPackageVersions;
332335
}
336+
337+
public function skip(string $message)
338+
{
339+
$this->shouldSkip = true;
340+
$this->skipMessage = $message;
341+
342+
return $this;
343+
}
344+
345+
public function shouldSkip(): bool
346+
{
347+
return $this->shouldSkip;
348+
}
349+
350+
public function getSkipMessage(): ?string
351+
{
352+
return $this->skipMessage;
353+
}
333354
}

tests/Maker/MakeFunctionalTestTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ class MakeFunctionalTestTest extends MakerTestCase
2222
{
2323
public function getTestDetails()
2424
{
25+
yield 'functional_maker' => [MakerTestDetails::createTest(
26+
$this->getMakerInstance(MakeFunctionalTest::class),
27+
[
28+
// functional test class name
29+
'FooBar',
30+
])
31+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFunctional')
32+
->skip('See https://github.com/symfony/maker-bundle/pull/807/files#r571308250'),
33+
];
34+
2535
yield 'functional_with_panther' => [MakerTestDetails::createTest(
2636
$this->getMakerInstance(MakeFunctionalTest::class),
2737
[

tests/Maker/MakeTestTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,42 @@ public function getTestDetails()
2222
yield 'TestCase' => [MakerTestDetails::createTest(
2323
$this->getMakerInstance(MakeTest::class),
2424
[
25-
// class name
26-
'FooBar',
2725
// type
2826
'TestCase',
27+
// class name
28+
'FooBar',
2929
]),
3030
];
3131

3232
yield 'KernelTestCase' => [MakerTestDetails::createTest(
3333
$this->getMakerInstance(MakeTest::class),
3434
[
35-
// functional test class name
36-
'FooBar',
3735
// type
3836
'KernelTestCase',
37+
// functional test class name
38+
'FooBar',
3939
])
4040
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFunctional'),
4141
];
4242

4343
yield 'WebTestCase' => [MakerTestDetails::createTest(
4444
$this->getMakerInstance(MakeTest::class),
4545
[
46-
// functional test class name
47-
'FooBar',
4846
// type
4947
'WebTestCase',
48+
// functional test class name
49+
'FooBar',
5050
])
5151
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFunctional'),
5252
];
5353

5454
yield 'PantherTestCase' => [MakerTestDetails::createTest(
5555
$this->getMakerInstance(MakeTest::class),
5656
[
57-
// functional test class name
58-
'FooBar',
5957
// type
6058
'PantherTestCase',
59+
// functional test class name
60+
'FooBar',
6161
])
6262
->addExtraDependencies('panther')
6363
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFunctional'),

0 commit comments

Comments
 (0)