diff --git a/app.php b/app.php index 957e7a7..1b37bab 100644 --- a/app.php +++ b/app.php @@ -1,6 +1,7 @@ addCommands([ - new GenerateDrupal7Command($finder), + new GenerateDrupal7Command($finder, $testNameConverter), ]); $app->run(); diff --git a/fixtures/drupal7_module/src/Tests/Functional/ExampleTest.php b/fixtures/drupal7_module/src/Tests/Functional/ExampleTest.php index f0625a0..6bd1635 100644 --- a/fixtures/drupal7_module/src/Tests/Functional/ExampleTest.php +++ b/fixtures/drupal7_module/src/Tests/Functional/ExampleTest.php @@ -2,4 +2,4 @@ namespace Drupal\{{ name }}\Tests\Functional; -class {{ test_name }} extends \DrupalWebTestCase {} +final class {{ test_name }} extends \DrupalWebTestCase {} diff --git a/src/Command/GenerateDrupal7Command.php b/src/Command/GenerateDrupal7Command.php index a827533..bd6fab3 100644 --- a/src/Command/GenerateDrupal7Command.php +++ b/src/Command/GenerateDrupal7Command.php @@ -3,6 +3,7 @@ namespace Opdavies\DrupalModuleGenerator\Command; use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException; +use Opdavies\DrupalModuleGenerator\Service\TestNameConverter; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -15,6 +16,9 @@ use Tightenco\Collect\Support\Collection; class GenerateDrupal7Command extends Command { + private $moduleName; + private $testName; + /** @var Filesystem */ private $filesystem; @@ -24,13 +28,18 @@ class GenerateDrupal7Command extends Command /** @var SymfonyStyle $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); $this->finder = $finder; + $this->testNameConverter = $testNameConverter; } /** @@ -56,6 +65,7 @@ class GenerateDrupal7Command extends Command $this->io = new SymfonyStyle($input, $output); $this->moduleName = $input->getArgument('module-name'); + $this->testName = $this->testNameConverter->__invoke($this->moduleName); $this ->ensureDirectoryDoesNotExist() @@ -87,25 +97,33 @@ class GenerateDrupal7Command extends Command private function createFiles() { $createdFiles = new Collection(); + $testNameConverter = new TestNameConverter(); /** @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()); - file_put_contents( - "{$this->moduleName}/{$this->moduleName}.{$file->getExtension()}", - $contents - ); + file_put_contents("{$this->moduleName}/{$filename}", $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) { $contents = str_replace('{{ name }}', $this->moduleName, $contents); + $contents = str_replace('{{ test_name }}', $this->testName, $contents); return $contents; } diff --git a/tests/Command/GenerateDrupal7ModuleCommandTest.php b/tests/Command/GenerateDrupal7ModuleCommandTest.php index 4156a61..fef6d67 100644 --- a/tests/Command/GenerateDrupal7ModuleCommandTest.php +++ b/tests/Command/GenerateDrupal7ModuleCommandTest.php @@ -4,6 +4,7 @@ namespace Opdavies\Tests\DrupalModuleGenerator\Command; use Opdavies\DrupalModuleGenerator\Command\GenerateDrupal7Command; use Opdavies\DrupalModuleGenerator\Exception\CannotCreateModuleException; +use Opdavies\DrupalModuleGenerator\Service\TestNameConverter; use Symfony\Component\Console\Tester\CommandTester; use PHPUnit\Framework\TestCase; use Symfony\Component\Filesystem\Filesystem; @@ -23,7 +24,8 @@ class GenerateDrupal7ModuleCommandTest extends TestCase $this->expectExceptionObject(CannotCreateModuleException::directoryAlreadyExists()); $finder = new Finder(); - $command = new GenerateDrupal7Command($finder); + $testNameConverter = new TestNameConverter(); + $command = new GenerateDrupal7Command($finder, $testNameConverter); $commandTester = new CommandTester($command); $commandTester->execute([ @@ -35,7 +37,8 @@ class GenerateDrupal7ModuleCommandTest extends TestCase public function it_creates_a_new_module_directory() { $finder = new Finder(); - $command = new GenerateDrupal7Command($finder); + $testNameConverter = new TestNameConverter(); + $command = new GenerateDrupal7Command($finder, $testNameConverter); $commandTester = new CommandTester($command); $commandTester->execute([ @@ -49,7 +52,8 @@ class GenerateDrupal7ModuleCommandTest extends TestCase public function it_generates_an_info_file() { $finder = new Finder(); - $command = new GenerateDrupal7Command($finder); + $testNameConverter = new TestNameConverter(); + $command = new GenerateDrupal7Command($finder, $testNameConverter); $commandTester = new CommandTester($command); $commandTester->execute([ @@ -68,7 +72,8 @@ class GenerateDrupal7ModuleCommandTest extends TestCase public function it_generates_a_module_file() { $finder = new Finder(); - $command = new GenerateDrupal7Command($finder); + $testNameConverter = new TestNameConverter(); + $command = new GenerateDrupal7Command($finder, $testNameConverter); $commandTester = new CommandTester($command); $commandTester->execute([ @@ -81,4 +86,23 @@ class GenerateDrupal7ModuleCommandTest extends TestCase $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); + } }