Skip to content

Commit

Permalink
[generate:jstest] Add new command (#3465)
Browse files Browse the repository at this point in the history
* Generate JavaScript test init

* JavaScript test generator tests
  • Loading branch information
subhojit777 authored and jmolivas committed Sep 12, 2017
1 parent 4ddcdad commit 058dcc6
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Test/Command/GeneratorJsTestCommandTest.php
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();
}
}
22 changes: 22 additions & 0 deletions Test/DataProvider/JsTestDataProviderTrait.php
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'],
];
}
}
50 changes: 50 additions & 0 deletions Test/Generator/JsTestGeneratorTest.php
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)
);
}
}
}
5 changes: 5 additions & 0 deletions config/services/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,8 @@ services:
arguments: [ '@console.cache_context_generator', '@console.chain_queue', '@console.extension_manager', '@console.string_converter']
tags:
- { name: drupal.command }
console.generate_js_test:
class: Drupal\Console\Command\Generate\JsTestCommand
arguments: ['@console.extension_manager', '@console.js_test_generator', '@console.validator']
tags:
- { name: drupal.command }
5 changes: 5 additions & 0 deletions config/services/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,8 @@ services:
arguments: ['@console.extension_manager']
tags:
- { name: drupal.generator }
console.js_test_generator:
class: Drupal\Console\Generator\JsTestGenerator
arguments: ['@console.extension_manager']
tags:
- { name: drupal.generator }
145 changes: 145 additions & 0 deletions src/Command/Generate/JsTestCommand.php
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();
}
}
27 changes: 27 additions & 0 deletions src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,31 @@ public function getTemplatePath($fullPath = false)
{
return $this->getPath($fullPath) . '/templates';
}

/**
* @param bool $fullPath
* @return string
*/
public function getTestsPath($fullPath = false)
{
return $this->getPath($fullPath) . '/tests';
}

/**
* @param bool $fullPath
* @return string
*/
public function getTestsSourcePath($fullPath = false)
{
return $this->getTestsPath($fullPath) . '/src';
}

/**
* @param bool $fullPath
* @return string
*/
public function getJsTestsPath($fullPath = false)
{
return $this->getTestsSourcePath($fullPath) . '/FunctionalJavascript';
}
}
52 changes: 52 additions & 0 deletions src/Generator/JsTestGenerator.php
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
);
}
}
Loading

0 comments on commit 058dcc6

Please sign in to comment.