Skip to content

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 1 commit into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
1.29
====

* [make:test] Added the maker
* [make:unit-test] Deprecated the maker
* [make:functional-test] Deprecated the maker

1.27
====

* [make:registration-form] Added a new question to generate code that will allow
users to click on the "verify email" link in their email without needing to be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds(self::MAKER_TAG) as $id => $tags) {
$def = $container->getDefinition($id);
if ($def->isDeprecated()) {
continue;
}

$class = $container->getParameterBag()->resolveValue($def->getClass());
if (!is_subclass_of($class, MakerInterface::class)) {
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, MakerInterface::class));
Expand All @@ -50,6 +54,15 @@ public function process(ContainerBuilder $container)

$commandDefinition->addTag('console.command', $tagAttributes);

/*
* @deprecated remove this block when removing make:unit-test and make:functional-test
*/
if (method_exists($class, 'getCommandAliases')) {
foreach ($class::getCommandAliases() as $alias) {
$commandDefinition->addTag('console.command', ['command' => $alias, 'description' => 'Deprecated alias of "make:test"']);
}
}

$container->setDefinition(sprintf('maker.auto_command.%s', Str::asTwigVariable($class::getCommandName())), $commandDefinition);
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/DependencyInjection/MakerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\MakerBundle\MakerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

Expand All @@ -25,6 +26,11 @@
*/
class MakerExtension extends Extension
{
/**
* @deprecated remove this block when removing make:unit-test and make:functional-test
*/
private const TEST_MAKER_DEPRECATION_MESSAGE = 'The "%service_id%" service is deprecated, use "maker.maker.make_test" instead.';

/**
* {@inheritdoc}
*/
Expand All @@ -34,6 +40,17 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.xml');
$loader->load('makers.xml');

/**
* @deprecated remove this block when removing make:unit-test and make:functional-test
*/
$deprecParams = method_exists(Definition::class, 'getDeprecation') ? ['symfony/maker-bundle', '1.29', self::TEST_MAKER_DEPRECATION_MESSAGE] : [true, self::TEST_MAKER_DEPRECATION_MESSAGE];
$container
->getDefinition('maker.maker.make_unit_test')
->setDeprecated(...$deprecParams);
$container
->getDefinition('maker.maker.make_functional_test')
->setDeprecated(...$deprecParams);

$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

Expand Down
4 changes: 4 additions & 0 deletions src/Maker/MakeFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
*/
Expand Down
189 changes: 189 additions & 0 deletions src/Maker/MakeTest.php
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'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we ever ask this interactively?

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
}
}
}
4 changes: 4 additions & 0 deletions src/Maker/MakeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
<tag name="maker.command" />
</service>

<service id="maker.maker.make_test" class="Symfony\Bundle\MakerBundle\Maker\MakeTest">
<tag name="maker.command" />
</service>

<service id="maker.maker.make_unit_test" class="Symfony\Bundle\MakerBundle\Maker\MakeUnitTest">
<tag name="maker.command" />
</service>
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/help/MakeTest.txt
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.
16 changes: 16 additions & 0 deletions src/Resources/skeleton/test/ApiTestCase.tpl.php
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' => '/']);
}
}
11 changes: 10 additions & 1 deletion src/Resources/skeleton/test/Functional.tpl.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php /* @deprecated remove this method when removing make:unit-test and make:functional-test */ ?>
<?= "<?php\n" ?>

namespace <?= $namespace; ?>;
Expand All @@ -10,16 +11,24 @@

class <?= $class_name ?> extends <?= $panther_is_available ? 'PantherTestCase' : 'WebTestCase' ?><?= "\n" ?>
{
public function testSomething()
public function testSomething(): void
{
<?php if ($panther_is_available): ?>
$client = static::createPantherClient();
<?php else: ?>
$client = static::createClient();
<?php endif ?>
$crawler = $client->request('GET', '/');

<?php if ($web_assertions_are_available): ?>
<?php if (!$panther_is_available): ?>
$this->assertResponseIsSuccessful();
<?php endif ?>
$this->assertSelectorTextContains('h1', 'Hello World');
<?php else: ?>
<?php if (!$panther_is_available): ?>
$this->assertSame(200, $client->getResponse()->getStatusCode());
<?php endif ?>
$this->assertStringContainsString('Hello World', $crawler->filter('h1')->text());
<?php endif ?>
}
Expand Down
15 changes: 15 additions & 0 deletions src/Resources/skeleton/test/KernelTestCase.tpl.php
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());
}
}
20 changes: 20 additions & 0 deletions src/Resources/skeleton/test/PantherTestCase.tpl.php
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 ?>
}
}
Loading