-
-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathMakeTest.php
229 lines (198 loc) · 8.41 KB
/
MakeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MakerBundle\Maker;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputAwareMakerInterface;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Symfony\Component\Panther\PantherTestCaseTrait;
/**
* @author Kévin Dunglas <[email protected]>
* @author Javier Eguiluz <[email protected]>
* @author Ryan Weaver <[email protected]>
*/
final class MakeTest extends AbstractMaker implements InputAwareMakerInterface
{
private const DESCRIPTIONS = [
'TestCase' => 'basic PHPUnit tests',
'KernelTestCase' => 'basic tests that have access to Symfony services',
'WebTestCase' => 'to run browser-like scenarios, but that don\'t execute JavaScript code',
'ApiTestCase' => 'to run API-oriented scenarios',
'PantherTestCase' => 'to run e2e scenarios, using a real-browser or HTTP client and a real web server',
];
private const DOCS = [
'TestCase' => 'https://symfony.com/doc/current/testing.html#unit-tests',
'KernelTestCase' => 'https://symfony.com/doc/current/testing/database.html#functional-testing-of-a-doctrine-repository',
'WebTestCase' => 'https://symfony.com/doc/current/testing.html#functional-tests',
'ApiTestCase' => 'https://api-platform.com/docs/distribution/testing/',
'PantherTestCase' => 'https://github.com/symfony/panther#testing-usage',
];
public static function getCommandName(): string
{
return 'make:test';
}
/**
* @deprecated remove this method when removing make:unit-test and make:functional-test
*
* @return string[]
*/
public static function getCommandAliases(): iterable
{
yield 'make:unit-test';
yield 'make:functional-test';
}
public static function getCommandDescription(): string
{
return 'Create a new test class';
}
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$typesDesc = [];
$typesHelp = [];
foreach (self::DESCRIPTIONS as $type => $desc) {
$typesDesc[] = \sprintf('<fg=yellow>%s</> (%s)', $type, $desc);
$typesHelp[] = \sprintf('* <info>%s</info>: %s', $type, $desc);
}
$command
->addArgument('type', InputArgument::OPTIONAL, 'The type of test: '.implode(', ', $typesDesc))
->addArgument('name', InputArgument::OPTIONAL, 'The name of the test class (e.g. <fg=yellow>BlogPostTest</>)')
->setHelp($this->getHelpFileContents('MakeTest.txt').implode("\n", $typesHelp))
;
$inputConfig->setArgumentAsNonInteractive('name');
$inputConfig->setArgumentAsNonInteractive('type');
}
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
/* @deprecated remove the following block when removing make:unit-test and make:functional-test */
$this->handleDeprecatedMakerCommands($input, $io);
if (null !== $type = $input->getArgument('type')) {
if (!isset(self::DESCRIPTIONS[$type])) {
throw new RuntimeCommandException(\sprintf('The test type must be one of "%s", "%s" given.', implode('", "', array_keys(self::DESCRIPTIONS)), $type));
}
} else {
$input->setArgument(
'type',
$io->choice('Which test type would you like?', self::DESCRIPTIONS)
);
}
if ('ApiTestCase' === $input->getArgument('type') && !class_exists(ApiTestCase::class)) {
$io->warning([
'API Platform is required for this test type. Install it with',
'composer require api',
]);
}
if ('PantherTestCase' === $input->getArgument('type') && !trait_exists(PantherTestCaseTrait::class)) {
$io->warning([
'symfony/panther is required for this test type. Install it with',
'composer require symfony/panther --dev',
]);
}
if (null === $input->getArgument('name')) {
$io->writeln([
'',
'Choose a class name for your test, like:',
' * <fg=yellow>UtilTest</> (to create tests/UtilTest.php)',
' * <fg=yellow>Service\\UtilTest</> (to create tests/Service/UtilTest.php)',
' * <fg=yellow>\\App\Tests\\Service\\UtilTest</> (to create tests/Service/UtilTest.php)',
]);
$nameArgument = $command->getDefinition()->getArgument('name');
$value = $io->ask($nameArgument->getDescription(), $nameArgument->getDefault(), Validator::notBlank(...));
$input->setArgument($nameArgument->getName(), $value);
}
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$testClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'Tests\\',
'Test'
);
$type = $input->getArgument('type');
$generator->generateClass(
$testClassNameDetails->getFullName(),
"test/$type.tpl.php",
[
'web_assertions_are_available' => trait_exists(WebTestAssertionsTrait::class),
'api_test_case_fqcn' => !class_exists(ApiTestCase::class) ? LegacyApiTestCase::class : ApiTestCase::class,
]
);
$generator->writeChanges();
$this->writeSuccessMessage($io);
$io->text([
'Next: Open your new test class and start customizing it.',
\sprintf('Find the documentation at <fg=yellow>%s</>', self::DOCS[$type]),
]);
}
public function configureDependencies(DependencyBuilder $dependencies, ?InputInterface $input = null): void
{
if (null === $input) {
return;
}
switch ($input->getArgument('type')) {
case 'WebTestCase':
$dependencies->addClassDependency(
History::class,
'browser-kit',
true,
true
);
$dependencies->addClassDependency(
CssSelectorConverter::class,
'css-selector',
true,
true
);
return;
case 'ApiTestCase':
$dependencies->addClassDependency(
!class_exists(ApiTestCase::class) ? LegacyApiTestCase::class : ApiTestCase::class,
'api',
true,
false
);
return;
case 'PantherTestCase':
$dependencies->addClassDependency(
PantherTestCaseTrait::class,
'panther',
true,
true
);
return;
}
}
/**
* @deprecated
*/
private function handleDeprecatedMakerCommands(InputInterface $input, ConsoleStyle $io): void
{
$currentCommand = $input->getFirstArgument();
switch ($currentCommand) {
case 'make:unit-test':
$input->setArgument('type', 'TestCase');
$io->warning('The "make:unit-test" command is deprecated, use "make:test" instead.');
break;
case 'make:functional-test':
$input->setArgument('type', trait_exists(PantherTestCaseTrait::class) ? 'WebTestCase' : 'PantherTestCase');
$io->warning('The "make:functional-test" command is deprecated, use "make:test" instead.');
break;
}
}
}