-
-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[generate:jstest] Add new command (#3465)
* Generate JavaScript test init * JavaScript test generator tests
- Loading branch information
1 parent
4ddcdad
commit 058dcc6
Showing
9 changed files
with
417 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,55 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Test\Command\GeneratorJsTestCommandTest. | ||
*/ | ||
|
||
namespace Drupal\Console\Test\Command; | ||
|
||
use Drupal\Console\Command\Generate\JsTestCommand; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Drupal\Console\Test\DataProvider\JsTestDataProviderTrait; | ||
|
||
class GeneratorJsTestCommandTest extends GenerateCommandTest | ||
{ | ||
use JsTestDataProviderTrait; | ||
|
||
/** | ||
* JavaScript test command test | ||
* | ||
* @param $module | ||
* @param $class_name | ||
* | ||
* @dataProvider commandData | ||
*/ | ||
public function testCommandJsTest( | ||
$module, | ||
$class_name | ||
) { | ||
$command = new JsTestCommand($this->getHelperSet()); | ||
$command->setHelperSet($this->getHelperSet()); | ||
$command->setGenerator($this->getGenerator()); | ||
|
||
$commandTester = new CommandTester($command); | ||
|
||
$code = $commandTester->execute( | ||
[ | ||
'--module' => $module, | ||
'--class' => $class_name, | ||
], | ||
['interactive' => false] | ||
); | ||
|
||
$this->assertEquals(0, $code); | ||
} | ||
|
||
private function getGenerator() | ||
{ | ||
return $this | ||
->getMockBuilder('Drupal\Console\Generator\JsTestGenerator') | ||
->disableOriginalConstructor() | ||
->setMethods(['generate']) | ||
->getMock(); | ||
} | ||
} |
This file contains 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,22 @@ | ||
<?php | ||
|
||
namespace Drupal\Console\Test\DataProvider; | ||
|
||
/** | ||
* Class JsTestDataProviderTrait | ||
* @package Drupal\Console\Test\DataProvider | ||
*/ | ||
trait JsTestDataProviderTrait | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public function commandData() | ||
{ | ||
$this->setUpTemporaryDirectory(); | ||
|
||
return [ | ||
['foo', 'JsFooTest'], | ||
]; | ||
} | ||
} |
This file contains 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,50 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Drupal\Console\Test\Generator\JsTestGeneratorTest. | ||
*/ | ||
|
||
namespace Drupal\Console\Test\Generator; | ||
|
||
use Drupal\Console\Generator\JsTestGenerator; | ||
use Drupal\Console\Test\DataProvider\JsTestDataProviderTrait; | ||
|
||
class JsTestGeneratorTest extends GeneratorTest | ||
{ | ||
use JsTestDataProviderTrait; | ||
|
||
/** | ||
* JavaScript test generator test | ||
* | ||
* @param $module | ||
* @param $class_name | ||
* | ||
* @dataProvider commandData | ||
*/ | ||
public function testGenerateJsTest( | ||
$module, | ||
$class_name | ||
) { | ||
$generator = new JsTestGenerator(); | ||
$this->getRenderHelper()->setSkeletonDirs($this->getSkeletonDirs()); | ||
$this->getRenderHelper()->setTranslator($this->getTranslatorHelper()); | ||
$generator->setHelperSet($this->getHelperSet()); | ||
|
||
$generator->generate( | ||
$module, | ||
$class_name | ||
); | ||
|
||
$files = [ | ||
$generator->getSite()->getJsTestsPath($module) . "/$class_name.php", | ||
]; | ||
|
||
foreach ($files as $file) { | ||
$this->assertTrue( | ||
file_exists($file), | ||
sprintf('%s does not exist', $file) | ||
); | ||
} | ||
} | ||
} |
This file contains 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 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 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,145 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Command\Generate\JsTestCommand. | ||
*/ | ||
|
||
namespace Drupal\Console\Command\Generate; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Drupal\Console\Generator\JsTestGenerator; | ||
use Drupal\Console\Command\Shared\ConfirmationTrait; | ||
use Drupal\Console\Command\Shared\ModuleTrait; | ||
use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait; | ||
use Symfony\Component\Console\Command\Command; | ||
use Drupal\Console\Core\Style\DrupalStyle; | ||
use Drupal\Console\Utils\Validator; | ||
use Drupal\Console\Extension\Manager; | ||
|
||
class JsTestCommand extends Command | ||
{ | ||
use ModuleTrait; | ||
use ConfirmationTrait; | ||
use ContainerAwareCommandTrait; | ||
|
||
/** | ||
* @var Manager | ||
*/ | ||
protected $extensionManager; | ||
|
||
/** | ||
* @var JsTestGenerator | ||
*/ | ||
protected $generator; | ||
|
||
/** | ||
* @var Validator | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* JsTestCommand constructor. | ||
* | ||
* @param Manager $extensionManager | ||
* @param JsTestGenerator $generator | ||
* @param Validator $validator | ||
*/ | ||
public function __construct( | ||
Manager $extensionManager, | ||
JsTestGenerator $generator, | ||
Validator $validator | ||
) { | ||
$this->extensionManager = $extensionManager; | ||
$this->generator = $generator; | ||
$this->validator = $validator; | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('generate:jstest') | ||
->setDescription($this->trans('commands.generate.jstest.description')) | ||
->setHelp($this->trans('commands.generate.jstest.help')) | ||
->addOption( | ||
'module', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
$this->trans('commands.common.options.module') | ||
) | ||
->addOption( | ||
'class', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
$this->trans('commands.generate.jstest.options.class') | ||
) | ||
->setAliases(['gjt']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new DrupalStyle($input, $output); | ||
$yes = $input->hasOption('yes') ? $input->getOption('yes') : false; | ||
|
||
// @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration | ||
if (!$this->confirmGeneration($io, $yes)) { | ||
return 1; | ||
} | ||
|
||
$module = $input->getOption('module'); | ||
$class = $input->getOption('class'); | ||
|
||
$this->generator->generate( | ||
$module, | ||
$class | ||
); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function interact(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new DrupalStyle($input, $output); | ||
|
||
// --module option | ||
$module = $input->getOption('module'); | ||
if (!$module) { | ||
// @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion | ||
$module = $this->moduleQuestion($io); | ||
$input->setOption('module', $module); | ||
} | ||
|
||
// --class option | ||
$class = $input->getOption('class'); | ||
if (!$class) { | ||
$class = $io->ask( | ||
$this->trans('commands.generate.jstest.questions.class'), | ||
'DefaultJsTest', | ||
function ($class) { | ||
return $this->validator->validateClassName($class); | ||
} | ||
); | ||
$input->setOption('class', $class); | ||
} | ||
} | ||
|
||
/** | ||
* @return \Drupal\Console\Generator\JsTestGenerator | ||
*/ | ||
protected function createGenerator() | ||
{ | ||
return new JsTestGenerator(); | ||
} | ||
} |
This file contains 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 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,52 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Generator\JsTestGenerator. | ||
*/ | ||
|
||
namespace Drupal\Console\Generator; | ||
|
||
use Drupal\Console\Core\Generator\Generator; | ||
use Drupal\Console\Extension\Manager; | ||
|
||
/** | ||
* Class JsTestGenerator | ||
* | ||
* @package Drupal\Console\Generator | ||
*/ | ||
class JsTestGenerator extends Generator | ||
{ | ||
/** | ||
* @var Manager | ||
*/ | ||
protected $extensionManager; | ||
|
||
/** | ||
* AuthenticationProviderGenerator constructor. | ||
* | ||
* @param Manager $extensionManager | ||
*/ | ||
public function __construct(Manager $extensionManager) | ||
{ | ||
$this->extensionManager = $extensionManager; | ||
} | ||
|
||
/** | ||
* @param $module | ||
* @param $class | ||
*/ | ||
public function generate($module, $class) | ||
{ | ||
$parameters = [ | ||
'module' => $module, | ||
'class' => $class, | ||
]; | ||
|
||
$this->renderFile( | ||
'module/src/Tests/js-test.php.twig', | ||
$this->extensionManager->getModule($module)->getJsTestsPath() . "/$class.php", | ||
$parameters | ||
); | ||
} | ||
} |
Oops, something went wrong.