From 0ca46d3975212b776daa1ce4394167552fbfaede Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 19 Oct 2017 13:44:28 +0800 Subject: [PATCH] Add feature test and model test generator --- src/CrudMake.php | 73 ++------------------------------- src/Generators/FeatureTestGenerator.php | 60 +++++++++++++++++++++++++++ src/Generators/ModelTestGenerator.php | 28 +++++++++++++ 3 files changed, 92 insertions(+), 69 deletions(-) create mode 100644 src/Generators/FeatureTestGenerator.php create mode 100644 src/Generators/ModelTestGenerator.php diff --git a/src/CrudMake.php b/src/CrudMake.php index 84100d8..c794a68 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -5,12 +5,14 @@ namespace Luthfi\CrudGenerator; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Luthfi\CrudGenerator\Generators\ControllerGenerator; +use Luthfi\CrudGenerator\Generators\FeatureTestGenerator; use Luthfi\CrudGenerator\Generators\FormViewGenerator; use Luthfi\CrudGenerator\Generators\IndexViewGenerator; use Luthfi\CrudGenerator\Generators\LangFileGenerator; use Luthfi\CrudGenerator\Generators\MigrationGenerator; use Luthfi\CrudGenerator\Generators\ModelFactoryGenerator; use Luthfi\CrudGenerator\Generators\ModelGenerator; +use Luthfi\CrudGenerator\Generators\ModelTestGenerator; use Luthfi\CrudGenerator\Generators\WebRouteGenerator; class CrudMake extends Command @@ -90,7 +92,8 @@ class CrudMake extends Command app(FormViewGenerator::class, ['command' => $this])->generate(); app(LangFileGenerator::class, ['command' => $this])->generate(); app(ModelFactoryGenerator::class, ['command' => $this])->generate(); - $this->generateTests(); + app(FeatureTestGenerator::class, ['command' => $this])->generate(); + app(ModelTestGenerator::class, ['command' => $this])->generate(); $this->info('CRUD files generated successfully!'); } else { @@ -149,74 +152,6 @@ class CrudMake extends Command } /** - * Generate Feature for CRUD Operation and and Unit Testing for Model behaviour - * @return void - */ - public function generateTests() - { - $this->createBrowserKitBaseTestClass(); - - $featureTestPath = $this->makeDirectory(base_path('tests/Feature')); - $this->generateFile("{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", $this->getFeatureTestContent()); - $this->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.'); - - $unitTestPath = $this->makeDirectory(base_path('tests/Unit/Models')); - $this->generateFile("{$unitTestPath}/{$this->modelNames['model_name']}Test.php", $this->getUnitTestContent()); - $this->info($this->modelNames['model_name'].'Test (model) generated.'); - } - - /** - * Generate BrowserKitTest class for BaseTestCase - * - * @return void - */ - public function createBrowserKitBaseTestClass() - { - $testsPath = base_path('tests'); - if (! $this->files->isDirectory($testsPath)) { - $this->files->makeDirectory($testsPath, 0777, true, true); - } - - if (! $this->files->exists($testsPath.'/BrowserKitTest.php')) { - $this->generateFile($testsPath.'/BrowserKitTest.php', $this->getBrowserKitBaseTestContent()); - - $this->info('BrowserKitTest generated.'); - } - } - - /** - * Get BrowserKitBaseTest class file content - * - * @return string - */ - public function getBrowserKitBaseTestContent() - { - return $this->files->get(__DIR__.'/stubs/test-browserkit-base-class.stub'); - } - - /** - * Get feature test file content from feature test stub - * - * @return string Replaced proper model names in feature test file content - */ - public function getFeatureTestContent() - { - $stub = $this->files->get(__DIR__.'/stubs/test-feature.stub'); - return $this->replaceStubString($stub); - } - - /** - * Get unit test file content from unit test stub - * - * @return string Replaced proper model names in unit test file content - */ - public function getUnitTestContent() - { - $stub = $this->files->get(__DIR__.'/stubs/test-unit.stub'); - return $this->replaceStubString($stub); - } - - /** * Make directory if the path is not exists * @param string $path Absolute path of targetted directory * @return string Absolute path diff --git a/src/Generators/FeatureTestGenerator.php b/src/Generators/FeatureTestGenerator.php new file mode 100644 index 0000000..bc49856 --- /dev/null +++ b/src/Generators/FeatureTestGenerator.php @@ -0,0 +1,60 @@ +createBrowserKitBaseTestClass(); + + $featureTestPath = $this->makeDirectory(base_path('tests/Feature')); + $this->generateFile("{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", $this->getContent()); + $this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.'); + + } + + /** + * {@inheritDoc} + */ + protected function getContent() + { + $stub = $this->files->get(__DIR__.'/../stubs/test-feature.stub'); + return $this->replaceStubString($stub); + } + + /** + * Generate BrowserKitTest class for BaseTestCase + * + * @return void + */ + private function createBrowserKitBaseTestClass() + { + $testsPath = base_path('tests'); + if (! $this->files->isDirectory($testsPath)) { + $this->files->makeDirectory($testsPath, 0777, true, true); + } + + if (! $this->files->exists($testsPath.'/BrowserKitTest.php')) { + $this->generateFile($testsPath.'/BrowserKitTest.php', $this->getBrowserKitBaseTestContent()); + + $this->command->info('BrowserKitTest generated.'); + } + } + + /** + * Get BrowserKitBaseTest class file content + * + * @return string + */ + public function getBrowserKitBaseTestContent() + { + return $this->files->get(__DIR__.'/../stubs/test-browserkit-base-class.stub'); + } +} \ No newline at end of file diff --git a/src/Generators/ModelTestGenerator.php b/src/Generators/ModelTestGenerator.php new file mode 100644 index 0000000..5226121 --- /dev/null +++ b/src/Generators/ModelTestGenerator.php @@ -0,0 +1,28 @@ +makeDirectory(base_path('tests/Unit/Models')); + $this->generateFile("{$unitTestPath}/{$this->modelNames['model_name']}Test.php", $this->getContent()); + $this->command->info($this->modelNames['model_name'].'Test (model) generated.'); + } + + /** + * {@inheritDoc} + */ + protected function getContent() + { + $stub = $this->files->get(__DIR__.'/../stubs/test-unit.stub'); + return $this->replaceStubString($stub); + } +} \ No newline at end of file