Generate a new test case

This commit is contained in:
Oliver Davies 2020-02-09 18:40:26 +00:00
parent b773e0f76f
commit 8a6b3c5e78
4 changed files with 60 additions and 16 deletions

View file

@ -1,6 +1,7 @@
<?php <?php
use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command; use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command;
use Opdavies\DrupalModuleGenerator\Service\TestNameConverter;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
@ -9,9 +10,10 @@ require_once __DIR__.'/vendor/autoload.php';
$app = new Application(); $app = new Application();
$finder = new Finder(); $finder = new Finder();
$testNameConverter = new TestNameConverter();
$app->addCommands([ $app->addCommands([
new GenerateDrupal7Command($finder), new GenerateDrupal7Command($finder, $testNameConverter),
]); ]);
$app->run(); $app->run();

View file

@ -2,4 +2,4 @@
namespace Drupal\{{ name }}\Tests\Functional; namespace Drupal\{{ name }}\Tests\Functional;
class {{ test_name }} extends \DrupalWebTestCase {} final class {{ test_name }} extends \DrupalWebTestCase {}

View file

@ -3,6 +3,7 @@
namespace Opdavies\DrupalModuleGenerator\Command; namespace Opdavies\DrupalModuleGenerator\Command;
use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException; use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException;
use Opdavies\DrupalModuleGenerator\Service\TestNameConverter;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -15,6 +16,9 @@ use Tightenco\Collect\Support\Collection;
class GenerateDrupal7Command extends Command class GenerateDrupal7Command extends Command
{ {
private $moduleName;
private $testName;
/** @var Filesystem */ /** @var Filesystem */
private $filesystem; private $filesystem;
@ -24,13 +28,18 @@ class GenerateDrupal7Command extends Command
/** @var SymfonyStyle $io */ /** @var SymfonyStyle $io */
private $io; private $io;
private $moduleName; /** @var TestNameConverter */
private $testNameConverter;
public function __construct(Finder $finder, string $name = null) public function __construct(
{ Finder $finder,
TestNameConverter $testNameConverter,
string $name = null
) {
parent::__construct($name); parent::__construct($name);
$this->finder = $finder; $this->finder = $finder;
$this->testNameConverter = $testNameConverter;
} }
/** /**
@ -56,6 +65,7 @@ class GenerateDrupal7Command extends Command
$this->io = new SymfonyStyle($input, $output); $this->io = new SymfonyStyle($input, $output);
$this->moduleName = $input->getArgument('module-name'); $this->moduleName = $input->getArgument('module-name');
$this->testName = $this->testNameConverter->__invoke($this->moduleName);
$this $this
->ensureDirectoryDoesNotExist() ->ensureDirectoryDoesNotExist()
@ -87,25 +97,33 @@ class GenerateDrupal7Command extends Command
private function createFiles() private function createFiles()
{ {
$createdFiles = new Collection(); $createdFiles = new Collection();
$testNameConverter = new TestNameConverter();
/** @var SplFileInfo $file */ /** @var SplFileInfo $file */
foreach ($this->finder->in('fixtures/drupal7_module')->name('/.[info,module]/') as $file) { foreach ($this->finder->in('fixtures/drupal7_module')->files() as $file) {
$filename = "{$this->moduleName}.{$file->getExtension()}";
if ($file->getRelativePath()) {
mkdir("{$this->moduleName}/{$file->getRelativePath()}", 0777, $recursive = true);
$filename = "{$this->testName}.php";
$filename = "{$file->getRelativePath()}/{$filename}";
}
$contents = $this->updateFileContents($file->getContents()); $contents = $this->updateFileContents($file->getContents());
file_put_contents( file_put_contents("{$this->moduleName}/{$filename}", $contents);
"{$this->moduleName}/{$this->moduleName}.{$file->getExtension()}",
$contents
);
$createdFiles->push("{$this->moduleName}.{$file->getExtension()}"); $createdFiles->push($filename);
} }
$this->io->listing($createdFiles->sort()->toArray()); $this->io->listing($createdFiles->filter()->sort()->toArray());
} }
private function updateFileContents($contents) private function updateFileContents($contents)
{ {
$contents = str_replace('{{ name }}', $this->moduleName, $contents); $contents = str_replace('{{ name }}', $this->moduleName, $contents);
$contents = str_replace('{{ test_name }}', $this->testName, $contents);
return $contents; return $contents;
} }

View file

@ -4,6 +4,7 @@ namespace Opdavies\Tests\DrupalModuleGenerator\Command;
use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command; use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command;
use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException; use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException;
use Opdavies\DrupalModuleGenerator\Service\TestNameConverter;
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
@ -23,7 +24,8 @@ class GenerateDrupal7ModuleCommandTest extends TestCase
$this->expectExceptionObject(CannotCreateModuleException::directoryAlreadyExists()); $this->expectExceptionObject(CannotCreateModuleException::directoryAlreadyExists());
$finder = new Finder(); $finder = new Finder();
$command = new GenerateDrupal7Command($finder); $testNameConverter = new TestNameConverter();
$command = new GenerateDrupal7Command($finder, $testNameConverter);
$commandTester = new CommandTester($command); $commandTester = new CommandTester($command);
$commandTester->execute([ $commandTester->execute([
@ -35,7 +37,8 @@ class GenerateDrupal7ModuleCommandTest extends TestCase
public function it_creates_a_new_module_directory() public function it_creates_a_new_module_directory()
{ {
$finder = new Finder(); $finder = new Finder();
$command = new GenerateDrupal7Command($finder); $testNameConverter = new TestNameConverter();
$command = new GenerateDrupal7Command($finder, $testNameConverter);
$commandTester = new CommandTester($command); $commandTester = new CommandTester($command);
$commandTester->execute([ $commandTester->execute([
@ -49,7 +52,8 @@ class GenerateDrupal7ModuleCommandTest extends TestCase
public function it_generates_an_info_file() public function it_generates_an_info_file()
{ {
$finder = new Finder(); $finder = new Finder();
$command = new GenerateDrupal7Command($finder); $testNameConverter = new TestNameConverter();
$command = new GenerateDrupal7Command($finder, $testNameConverter);
$commandTester = new CommandTester($command); $commandTester = new CommandTester($command);
$commandTester->execute([ $commandTester->execute([
@ -68,7 +72,8 @@ class GenerateDrupal7ModuleCommandTest extends TestCase
public function it_generates_a_module_file() public function it_generates_a_module_file()
{ {
$finder = new Finder(); $finder = new Finder();
$command = new GenerateDrupal7Command($finder); $testNameConverter = new TestNameConverter();
$command = new GenerateDrupal7Command($finder, $testNameConverter);
$commandTester = new CommandTester($command); $commandTester = new CommandTester($command);
$commandTester->execute([ $commandTester->execute([
@ -81,4 +86,23 @@ class GenerateDrupal7ModuleCommandTest extends TestCase
$this->assertStringContainsString('The main module file for test_module.', $contents); $this->assertStringContainsString('The main module file for test_module.', $contents);
} }
/** @test */
public function it_generates_a_test_case()
{
$finder = new Finder();
$testNameConverter = new TestNameConverter();
$command = new GenerateDrupal7Command($finder, $testNameConverter);
$commandTester = new CommandTester($command);
$commandTester->execute([
'module-name' => 'test_module',
]);
$this->assertTrue(is_file('test_module/src/Tests/Functional/TestModuleTest.php'));
$contents = file_get_contents('test_module/src/Tests/Functional/TestModuleTest.php');
$this->assertStringContainsString('final class TestModuleTest', $contents);
}
} }