|
| 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 ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait; |
| 16 | +use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 17 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 18 | +use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
| 19 | +use Symfony\Bundle\MakerBundle\Generator; |
| 20 | +use Symfony\Bundle\MakerBundle\InputAwareMakerInterface; |
| 21 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 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 | + * @author Kévin Dunglas <[email protected]> |
| 31 | + * @author Javier Eguiluz <[email protected]> |
| 32 | + * @author Ryan Weaver <[email protected]> |
| 33 | + */ |
| 34 | +final class MakeTest extends AbstractMaker implements InputAwareMakerInterface |
| 35 | +{ |
| 36 | + private const DESCRIPTIONS = [ |
| 37 | + 'TestCase' => 'basic PHPUnit tests', |
| 38 | + 'KernelTestCase' => 'basic tests that have access to Symfony services', |
| 39 | + 'WebTestCase' => 'to run browser-like scenarios, but that don\'t execute JavaScript code', |
| 40 | + 'ApiTestCase' => 'to run API-oriented scenarios', |
| 41 | + 'PantherTestCase' => 'to run e2e scenarios, using a real-browser or HTTP client and a real web server', |
| 42 | + ]; |
| 43 | + private const DOCS = [ |
| 44 | + 'TestCase' => 'https://symfony.com/doc/current/testing.html#unit-tests', |
| 45 | + 'KernelTestCase' => 'https://symfony.com/doc/current/testing/database.html#functional-testing-of-a-doctrine-repository', |
| 46 | + 'WebTestCase' => 'https://symfony.com/doc/current/testing.html#functional-tests', |
| 47 | + 'ApiTestCase' => 'https://api-platform.com/docs/distribution/testing/', |
| 48 | + 'PantherTestCase' => 'https://github.com/symfony/panther#testing-usage', |
| 49 | + ]; |
| 50 | + |
| 51 | + public static function getCommandName(): string |
| 52 | + { |
| 53 | + return 'make:test'; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @deprecated remove this method when removing make:unit-test and make:functional-test |
| 58 | + */ |
| 59 | + public static function getCommandAliases(): iterable |
| 60 | + { |
| 61 | + yield 'make:unit-test'; |
| 62 | + yield 'make:functional-test'; |
| 63 | + } |
| 64 | + |
| 65 | + public static function getCommandDescription(): string |
| 66 | + { |
| 67 | + return 'Creates a new test class'; |
| 68 | + } |
| 69 | + |
| 70 | + public function configureCommand(Command $command, InputConfiguration $inputConf) |
| 71 | + { |
| 72 | + $typesDesc = []; |
| 73 | + $typesHelp = []; |
| 74 | + foreach (self::DESCRIPTIONS as $type => $desc) { |
| 75 | + $typesDesc[] = sprintf('<fg=yellow>%s</> (%s)', $type, $desc); |
| 76 | + $typesHelp[] = sprintf('* <info>%s</info>: %s', $type, $desc); |
| 77 | + } |
| 78 | + |
| 79 | + $command |
| 80 | + ->addArgument('name', InputArgument::OPTIONAL, 'The name of the test class (e.g. <fg=yellow>BlogPostTest</>)') |
| 81 | + ->addArgument('type', InputArgument::OPTIONAL, 'The type of test: '.implode(', ', $typesDesc)) |
| 82 | + ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeTest.txt').implode("\n", $typesHelp)); |
| 83 | + |
| 84 | + $inputConf->setArgumentAsNonInteractive('type'); |
| 85 | + } |
| 86 | + |
| 87 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command) |
| 88 | + { |
| 89 | + /* @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 | + } |
| 104 | + |
| 105 | + if (null !== $type = $input->getArgument('type')) { |
| 106 | + if (!isset(self::DESCRIPTIONS[$type])) { |
| 107 | + throw new RuntimeCommandException(sprintf('The test type must be one of "%s", "%s" given.', implode('", "', array_keys(self::DESCRIPTIONS)), $type)); |
| 108 | + } |
| 109 | + |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + $input->setArgument( |
| 114 | + 'type', |
| 115 | + $io->choice('Which test type would you like?', self::DESCRIPTIONS) |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) |
| 120 | + { |
| 121 | + $testClassNameDetails = $generator->createClassNameDetails( |
| 122 | + $input->getArgument('name'), |
| 123 | + 'Tests\\', |
| 124 | + 'Test' |
| 125 | + ); |
| 126 | + |
| 127 | + $type = $input->getArgument('type'); |
| 128 | + |
| 129 | + $generator->generateClass( |
| 130 | + $testClassNameDetails->getFullName(), |
| 131 | + "test/$type.tpl.php", |
| 132 | + ['web_assertions_are_available' => trait_exists(WebTestAssertionsTrait::class)] |
| 133 | + ); |
| 134 | + |
| 135 | + $generator->writeChanges(); |
| 136 | + |
| 137 | + $this->writeSuccessMessage($io); |
| 138 | + |
| 139 | + $io->text([ |
| 140 | + 'Next: Open your new test class and start customizing it.', |
| 141 | + sprintf('Find the documentation at <fg=yellow>%s</>', self::DOCS[$type]), |
| 142 | + ]); |
| 143 | + } |
| 144 | + |
| 145 | + public function configureDependencies(DependencyBuilder $dependencies, InputInterface $input = null): void |
| 146 | + { |
| 147 | + if (null === $input) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + switch ($input->getArgument('type')) { |
| 152 | + case 'WebTestCase': |
| 153 | + $dependencies->addClassDependency( |
| 154 | + History::class, |
| 155 | + 'browser-kit', |
| 156 | + true, |
| 157 | + true |
| 158 | + ); |
| 159 | + $dependencies->addClassDependency( |
| 160 | + CssSelectorConverter::class, |
| 161 | + 'css-selector', |
| 162 | + true, |
| 163 | + true |
| 164 | + ); |
| 165 | + |
| 166 | + return; |
| 167 | + |
| 168 | + case 'ApiTestCase': |
| 169 | + $dependencies->addClassDependency( |
| 170 | + ApiTestCase::class, |
| 171 | + 'api', |
| 172 | + true, |
| 173 | + false |
| 174 | + ); |
| 175 | + |
| 176 | + return; |
| 177 | + |
| 178 | + case 'PantherTestCase': |
| 179 | + $dependencies->addClassDependency( |
| 180 | + PantherTestCaseTrait::class, |
| 181 | + 'panther', |
| 182 | + true, |
| 183 | + true |
| 184 | + ); |
| 185 | + |
| 186 | + return; |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments