diff --git a/src/Console/Command/GenerateCommand.php b/src/Console/Command/GenerateCommand.php index afee097..13ddc8d 100644 --- a/src/Console/Command/GenerateCommand.php +++ b/src/Console/Command/GenerateCommand.php @@ -63,6 +63,7 @@ class GenerateCommand extends Command 'repository' => 'Generates a Repository for a Model', 'scaffold' => 'Generates a MVC using the database', 'service' => 'Generates a Service Object class', + 'task' => 'Generates a Task for Scheduling' ]; public function initialize(): void @@ -412,6 +413,23 @@ protected function repository(array $data) ); } + protected function task(array $data) + { + $data['custom'] = Inflector::human($data['underscored']); + + $this->generate( + $this->getTemplateFilename('task'), + $this->getBaseFolder($data['name'], self::SRC). DS .'Task'. DS ."{$data['class']}Task.php", + $data + ); + + $this->generate( + $this->getTemplateFilename('task_test'), + $this->getBaseFolder($data['name'], self::TEST). DS .'Task'. DS ."{$data['class']}TaskTest.php", + $data + ); + } + protected function record(array $data) { $this->generate( diff --git a/templates/generator/task.tpl b/templates/generator/task.tpl new file mode 100644 index 0000000..1ac00c0 --- /dev/null +++ b/templates/generator/task.tpl @@ -0,0 +1,26 @@ +command('ls -la') + ->everyMinute(); + } + + protected function shutdown(): void + { + } +} diff --git a/templates/generator/task_test.tpl b/templates/generator/task_test.tpl new file mode 100644 index 0000000..fafb77c --- /dev/null +++ b/templates/generator/task_test.tpl @@ -0,0 +1,17 @@ +markTestIncomplete( + 'This test has not been implemented yet.' + ); + (new %class%Task())->dispatch(); + } +} \ No newline at end of file diff --git a/tests/TestCase/Console/Command/GenerateCommandTest.php b/tests/TestCase/Console/Command/GenerateCommandTest.php index 1f8705c..7cd15be 100644 --- a/tests/TestCase/Console/Command/GenerateCommandTest.php +++ b/tests/TestCase/Console/Command/GenerateCommandTest.php @@ -589,6 +589,26 @@ public function testPlugin() $this->recursiveDelete(PLUGINS . DS . 'dummy'); } + public function testGenerateTask() + { + $this->exec('generate --force task Dummy'); + + $this->assertExitSuccess(); + $filename = APP . DS . 'Task' . DS . 'DummyTask.php'; + $this->assertOutputContains('src/Task/DummyTask.php'); + $this->assertFileExists($filename); + + $this->assertFileHash('d7c26db6ab2efa5134e642cc71455812', $filename); + unlink($filename); + + $filename = TESTS . DS . 'TestCase' . DS . 'Task' . DS . 'DummyTaskTest.php'; + + $this->assertOutputContains('TestCase/Task/DummyTaskTest.php'); + $this->assertFileExists($filename); + $this->assertFileHash('721d6a783b06f1aae788035566e00f03', $filename); + unlink($filename); + } + /* 'plugin' => 'Generates a plugin skeleton',