|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony MakerBundle package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MakerBundle\Maker; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait; |
| 15 | +use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 16 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 17 | +use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
| 18 | +use Symfony\Bundle\MakerBundle\Generator; |
| 19 | +use Symfony\Bundle\MakerBundle\InputAwareMakerInterface; |
| 20 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 21 | +use Symfony\Bundle\MakerBundle\Validator; |
| 22 | +use Symfony\Component\BrowserKit\History; |
| 23 | +use Symfony\Component\Console\Command\Command; |
| 24 | +use Symfony\Component\Console\Input\InputArgument; |
| 25 | +use Symfony\Component\Console\Input\InputInterface; |
| 26 | +use Symfony\Component\CssSelector\CssSelectorConverter; |
| 27 | +use Symfony\Component\Panther\PantherTestCaseTrait; |
| 28 | + |
| 29 | +/** |
| 30 | + * MakeTest |
| 31 | + * |
| 32 | + * @author Kévin Dunglas <[email protected]> |
| 33 | + * @author Javier Eguiluz <[email protected]> |
| 34 | + * @author Ryan Weaver <[email protected]> |
| 35 | + */ |
| 36 | +final class MakeTest extends AbstractMaker implements InputAwareMakerInterface |
| 37 | +{ |
| 38 | + public static function getCommandName(): string |
| 39 | + { |
| 40 | + return 'make:test'; |
| 41 | + } |
| 42 | + |
| 43 | + // TODO: remove this method when removing make:unit-test and make:functional-test |
| 44 | + public static function getCommandAliases(): iterable |
| 45 | + { |
| 46 | + yield 'make:unit-test'; |
| 47 | + yield 'make:functional-test'; |
| 48 | + } |
| 49 | + |
| 50 | + public static function getCommandDescription(): string |
| 51 | + { |
| 52 | + return 'Creates a new test class'; |
| 53 | + } |
| 54 | + |
| 55 | + public function configureCommand(Command $command, InputConfiguration $inputConf) |
| 56 | + { |
| 57 | + $command |
| 58 | + ->addArgument('name', InputArgument::OPTIONAL, 'The name of the test class (e.g. <fg=yellow>BlogPostTest</>)') |
| 59 | + ->addArgument('type', InputArgument::OPTIONAL, 'The type of test: <fg=yellow>unit</> (PHPUnit), <fg=yellow>integration</> (WebTestCase) or <fg=yellow>e2e</> (PantherTestCase)') |
| 60 | + ->setHelp(file_get_contents(__DIR__ . '/../Resources/help/MakeTest.txt')); |
| 61 | + |
| 62 | + $inputConf->setArgumentAsNonInteractive('type'); |
| 63 | + } |
| 64 | + |
| 65 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command) |
| 66 | + { |
| 67 | + // TODO: remove the following block when removing make:unit-test and make:functional-test |
| 68 | + $currentCommand = $input->getFirstArgument(); |
| 69 | + switch ($currentCommand) { |
| 70 | + case 'make:unit-test': |
| 71 | + $input->setArgument('type', 'unit'); |
| 72 | + $io->caution('The "make:unit-test" command is deprecated, use "make:test" instead.'); |
| 73 | + |
| 74 | + return; |
| 75 | + |
| 76 | + case 'make:functional-test': |
| 77 | + $input->setArgument('type', trait_exists(PantherTestCaseTrait::class) ? 'e2e' : 'integration'); |
| 78 | + $io->caution('The "make:functional-test" command is deprecated, use "make:test" instead.'); |
| 79 | + |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (null !== $input->getArgument('type')) { |
| 84 | + $this->normalizeType($input); |
| 85 | + |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + $input->setArgument( |
| 90 | + 'type', |
| 91 | + $io->choice($command->getDefinition()->getArgument('type')->getDescription(), ['unit', 'integration', 'e2e']) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) |
| 96 | + { |
| 97 | + $testClassNameDetails = $generator->createClassNameDetails( |
| 98 | + $input->getArgument('name'), |
| 99 | + 'Tests\\', |
| 100 | + 'Test' |
| 101 | + ); |
| 102 | + |
| 103 | + switch ($type = $input->getArgument('type')) { |
| 104 | + case 'unit': |
| 105 | + $generator->generateClass( |
| 106 | + $testClassNameDetails->getFullName(), |
| 107 | + 'test/Unit.tpl.php', |
| 108 | + [] |
| 109 | + ); |
| 110 | + $url = 'https://symfony.com/doc/current/testing.html#unit-tests'; |
| 111 | + |
| 112 | + break; |
| 113 | + |
| 114 | + case 'integration': |
| 115 | + $generator->generateClass( |
| 116 | + $testClassNameDetails->getFullName(), |
| 117 | + 'test/Functional.tpl.php', |
| 118 | + [ |
| 119 | + 'web_assertions_are_available' => trait_exists(WebTestAssertionsTrait::class), |
| 120 | + 'panther_is_available' => false, |
| 121 | + ] |
| 122 | + ); |
| 123 | + $url = 'https://symfony.com/doc/current/testing.html#functional-tests'; |
| 124 | + |
| 125 | + break; |
| 126 | + |
| 127 | + default: |
| 128 | + $generator->generateClass( |
| 129 | + $testClassNameDetails->getFullName(), |
| 130 | + 'test/Functional.tpl.php', |
| 131 | + [ |
| 132 | + 'web_assertions_are_available' => trait_exists(WebTestAssertionsTrait::class), |
| 133 | + 'panther_is_available' => true, |
| 134 | + ] |
| 135 | + ); |
| 136 | + $url = 'https://github.com/symfony/panther'; |
| 137 | + |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + $generator->writeChanges(); |
| 142 | + |
| 143 | + $this->writeSuccessMessage($io); |
| 144 | + |
| 145 | + $io->text([ |
| 146 | + 'Next: Open your new test class and start customizing it.', |
| 147 | + sprintf('Find the documentation at <fg=yellow>%s</>', $url), |
| 148 | + ]); |
| 149 | + } |
| 150 | + |
| 151 | + public function configureDependencies(DependencyBuilder $dependencies, InputInterface $input = null): void |
| 152 | + { |
| 153 | + if ($input === null) { |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + switch ($input->getArgument('type')) { |
| 158 | + case 'integration': |
| 159 | + $dependencies->addClassDependency( |
| 160 | + History::class, |
| 161 | + 'browser-kit', |
| 162 | + true, |
| 163 | + true |
| 164 | + ); |
| 165 | + $dependencies->addClassDependency( |
| 166 | + CssSelectorConverter::class, |
| 167 | + 'css-selector', |
| 168 | + true, |
| 169 | + true |
| 170 | + ); |
| 171 | + return; |
| 172 | + |
| 173 | + case 'e2e': |
| 174 | + $dependencies->addClassDependency( |
| 175 | + PantherTestCaseTrait::class, |
| 176 | + 'panther', |
| 177 | + true, |
| 178 | + true |
| 179 | + ); |
| 180 | + return; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private function normalizeType(InputInterface $input): void |
| 185 | + { |
| 186 | + switch ($type = $input->getArgument('type')) { |
| 187 | + case 'unit': |
| 188 | + return; |
| 189 | + |
| 190 | + case 'integration': |
| 191 | + case 'web': |
| 192 | + case 'functional': |
| 193 | + $input->setArgument(type, 'integration'); |
| 194 | + return; |
| 195 | + |
| 196 | + case 'e2e': |
| 197 | + case 'ui': |
| 198 | + case 'panther': |
| 199 | + $input->setArgument(type, 'e2'); |
| 200 | + return; |
| 201 | + |
| 202 | + default: |
| 203 | + throw new RuntimeCommandException(sprintf('The test type must be "unit", "integration" or "e2e", "%s" given.', $type)); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments