-
-
Notifications
You must be signed in to change notification settings - Fork 438
Add a new make:test command #807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,11 @@ | |
| use Symfony\Component\CssSelector\CssSelectorConverter; | ||
| use Symfony\Component\Panther\PantherTestCaseTrait; | ||
|
|
||
| trigger_deprecation('symfony/maker-bundle', '1.29', 'The "%s" class is deprecated, use "%s" instead.', MakeFunctionalTest::class, MakeTest::class); | ||
|
|
||
| /** | ||
| * @deprecated since MakerBundle 1.29, use Symfony\Bundle\MakerBundle\Maker\MakeTest instead. | ||
| * | ||
| * @author Javier Eguiluz <[email protected]> | ||
| * @author Ryan Weaver <[email protected]> | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| <?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\Core\Bridge\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\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 | ||
| */ | ||
| public static function getCommandAliases(): iterable | ||
| { | ||
| yield 'make:unit-test'; | ||
| yield 'make:functional-test'; | ||
| } | ||
|
|
||
| public static function getCommandDescription(): string | ||
| { | ||
| return 'Creates a new test class'; | ||
| } | ||
|
|
||
| public function configureCommand(Command $command, InputConfiguration $inputConf) | ||
| { | ||
| $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('name', InputArgument::OPTIONAL, 'The name of the test class (e.g. <fg=yellow>BlogPostTest</>)') | ||
| ->addArgument('type', InputArgument::OPTIONAL, 'The type of test: '.implode(', ', $typesDesc)) | ||
| ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeTest.txt').implode("\n", $typesHelp)); | ||
|
|
||
| $inputConf->setArgumentAsNonInteractive('type'); | ||
| } | ||
|
|
||
| public function interact(InputInterface $input, ConsoleStyle $io, Command $command) | ||
| { | ||
| /* @deprecated remove the following block when removing make:unit-test and make:functional-test */ | ||
| $currentCommand = $input->getFirstArgument(); | ||
| switch ($currentCommand) { | ||
| case 'make:unit-test': | ||
| $input->setArgument('type', 'TestCase'); | ||
| $io->caution('The "make:unit-test" command is deprecated, use "make:test" instead.'); | ||
|
|
||
| return; | ||
|
|
||
| case 'make:functional-test': | ||
| $input->setArgument('type', trait_exists(PantherTestCaseTrait::class) ? 'WebTestCase' : 'PantherTestCase'); | ||
| $io->caution('The "make:functional-test" command is deprecated, use "make:test" instead.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| 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)); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $input->setArgument( | ||
| 'type', | ||
| $io->choice('Which test type would you like?', self::DESCRIPTIONS) | ||
| ); | ||
| } | ||
|
|
||
| public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) | ||
| { | ||
| $testClassNameDetails = $generator->createClassNameDetails( | ||
| $input->getArgument('name'), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we ever ask this interactively?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's delegated to the method in the abstract class. |
||
| 'Tests\\', | ||
| 'Test' | ||
| ); | ||
|
|
||
| $type = $input->getArgument('type'); | ||
|
|
||
| $generator->generateClass( | ||
| $testClassNameDetails->getFullName(), | ||
| "test/$type.tpl.php", | ||
| ['web_assertions_are_available' => trait_exists(WebTestAssertionsTrait::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( | ||
| ApiTestCase::class, | ||
| 'api', | ||
| true, | ||
| false | ||
| ); | ||
|
|
||
| return; | ||
|
|
||
| case 'PantherTestCase': | ||
| $dependencies->addClassDependency( | ||
| PantherTestCaseTrait::class, | ||
| 'panther', | ||
| true, | ||
| true | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,11 @@ | |
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
|
|
||
| trigger_deprecation('symfony/maker-bundle', '1.29', 'The "%s" class is deprecated, use "%s" instead.', MakeUnitTest::class, MakeTest::class); | ||
|
|
||
| /** | ||
| * @deprecated since MakerBundle 1.29, use Symfony\Bundle\MakerBundle\Maker\MakeTest instead. | ||
| * | ||
| * @author Javier Eguiluz <[email protected]> | ||
| * @author Ryan Weaver <[email protected]> | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| The <info>%command.name%</info> command generates a new test class. | ||
|
|
||
| <info>php %command.full_name% BlogPostTest TestCase</info> | ||
|
|
||
| If the first argument is missing, the command will ask for the class name interactively. | ||
|
|
||
| If the second argument is missing, the command will ask for the test type interactively. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?= "<?php\n" ?> | ||
|
|
||
| namespace <?= $namespace; ?>; | ||
|
|
||
| use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase; | ||
|
|
||
| class <?= $class_name ?> extends ApiTestCase | ||
| { | ||
| public function testSomething(): void | ||
| { | ||
| $response = static::createClient()->request('GET', '/'); | ||
|
|
||
| $this->assertResponseIsSuccessful(); | ||
| $this->assertJsonContains(['@id' => '/']); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?= "<?php\n" ?> | ||
|
|
||
| namespace <?= $namespace; ?>; | ||
|
|
||
| use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
|
||
| class <?= $class_name ?> extends KernelTestCase | ||
| { | ||
| public function testSomething(): void | ||
| { | ||
| $kernel = self::bootKernel(); | ||
|
|
||
| $this->assertSame('test', $kernel->getEnvironment()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?= "<?php\n" ?> | ||
|
|
||
| namespace <?= $namespace; ?>; | ||
|
|
||
| use Symfony\Component\Panther\PantherTestCase; | ||
|
|
||
| class <?= $class_name ?> extends PantherTestCase | ||
| { | ||
| public function testSomething(): void | ||
| { | ||
| $client = static::createPantherClient(); | ||
| $crawler = $client->request('GET', '/'); | ||
|
|
||
| <?php if ($web_assertions_are_available): ?> | ||
| $this->assertSelectorTextContains('h1', 'Hello World'); | ||
| <?php else: ?> | ||
| $this->assertStringContainsString('Hello World', $crawler->filter('h1')->text()); | ||
| <?php endif ?> | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.