From a32a4274ef046c3298e4dce820a09bcf1195908d Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 17 Oct 2017 22:59:05 +0800 Subject: [PATCH] Added namespaced input to namespaced model Added namespaced input to namespaced model --- src/CrudMake.php | 27 +++++++++++- src/stubs/model.stub | 2 +- tests/CrudMakeCommandTest.php | 73 ++++++++++++++++++++++++++++++++- tests/Generators/ModelGeneratorTest.php | 21 ++++++++++ tests/TestCase.php | 1 + 5 files changed, 119 insertions(+), 5 deletions(-) diff --git a/src/CrudMake.php b/src/CrudMake.php index 5b55b3d..49f72d3 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -37,7 +37,9 @@ class CrudMake extends Command parent::__construct(); $this->files = $files; + $this->stubModelNames = [ + 'model_namespace' => 'mstrNmspc', 'full_model_name' => 'fullMstr', 'plural_model_name' => 'Masters', 'model_name' => 'Master', @@ -98,18 +100,36 @@ class CrudMake extends Command $modelName = is_null($modelName) ? $this->argument('name') : $modelName; $model_name = ucfirst(class_basename($modelName)); $plural_model_name = str_plural($model_name); + $modelPath = $this->getModelPath($modelName); + $modelNamespace = $this->getModelNamespace($modelPath); return $this->modelNames = [ - 'full_model_name' => 'App\\'.$model_name, + 'model_namespace' => $modelNamespace, + 'full_model_name' => $modelNamespace.'\\'.$model_name, 'plural_model_name' => $plural_model_name, 'model_name' => $model_name, 'table_name' => snake_case($plural_model_name), 'lang_name' => snake_case($model_name), 'collection_model_var_name' => camel_case($plural_model_name), 'single_model_var_name' => camel_case($model_name), + 'model_path' => $modelPath, ]; } + protected function getModelPath($modelName) + { + $inputName = explode('/', ucfirst($modelName)); + array_pop($inputName); + + return implode('/', $inputName); + } + + protected function getModelNamespace($modelPath) + { + $modelNamespace = str_replace('/', '\\', 'App/'.ucfirst($modelPath)); + return $modelNamespace == 'App\\' ? 'App' : $modelNamespace; + } + /** * Check for Model file existance * @@ -127,7 +147,10 @@ class CrudMake extends Command */ public function generateModel() { - $this->generateFile(app_path($this->modelNames['model_name'].'.php'), $this->getModelContent()); + $modelPath = $this->modelNames['model_path']; + $modelDirectory = $this->makeDirectory(app_path($modelPath)); + + $this->generateFile($modelDirectory.'/'.$this->modelNames['model_name'].'.php', $this->getModelContent()); $this->info($this->modelNames['model_name'].' model generated.'); } diff --git a/src/stubs/model.stub b/src/stubs/model.stub index 77c039e..774bfde 100644 --- a/src/stubs/model.stub +++ b/src/stubs/model.stub @@ -1,6 +1,6 @@ assertEquals([ + 'model_namespace' => 'mstrNmspc', 'full_model_name' => 'fullMstr', 'plural_model_name' => 'Masters', 'model_name' => 'Master', @@ -36,6 +37,20 @@ class CrudMakeCommandTest extends TestCase 'lang_name' => 'category', 'collection_model_var_name' => 'categories', 'single_model_var_name' => 'category', + 'model_path' => '', + 'model_namespace' => 'App', + ], $crudMaker->getModelName('Category')); + + $this->assertEquals([ + 'full_model_name' => 'App\Category', + 'plural_model_name' => 'Categories', + 'model_name' => 'Category', + 'table_name' => 'categories', + 'lang_name' => 'category', + 'collection_model_var_name' => 'categories', + 'single_model_var_name' => 'category', + 'model_path' => '', + 'model_namespace' => 'App', ], $crudMaker->getModelName('category')); } @@ -45,14 +60,40 @@ class CrudMakeCommandTest extends TestCase $crudMaker = app(CrudMake::class); $this->assertEquals([ - 'full_model_name' => 'App\Category', + 'full_model_name' => 'App\Entities\References\Category', + 'plural_model_name' => 'Categories', + 'model_name' => 'Category', + 'table_name' => 'categories', + 'lang_name' => 'category', + 'collection_model_var_name' => 'categories', + 'single_model_var_name' => 'category', + 'model_path' => 'Entities/References', + 'model_namespace' => 'App\Entities\References', + ], $crudMaker->getModelName('Entities/References/Category')); + + $this->assertEquals([ + 'full_model_name' => 'App\Models\Category', 'plural_model_name' => 'Categories', 'model_name' => 'Category', 'table_name' => 'categories', 'lang_name' => 'category', 'collection_model_var_name' => 'categories', 'single_model_var_name' => 'category', - ], $crudMaker->getModelName('References/Category')); + 'model_path' => 'Models', + 'model_namespace' => 'App\Models', + ], $crudMaker->getModelName('Models/Category')); + + $this->assertEquals([ + 'full_model_name' => 'App\Models\Category', + 'plural_model_name' => 'Categories', + 'model_name' => 'Category', + 'table_name' => 'categories', + 'lang_name' => 'category', + 'collection_model_var_name' => 'categories', + 'single_model_var_name' => 'category', + 'model_path' => 'Models', + 'model_namespace' => 'App\Models', + ], $crudMaker->getModelName('models/category')); } /** @test */ @@ -99,4 +140,32 @@ class CrudMakeCommandTest extends TestCase $this->assertFileNotExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); $this->assertFileNotExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); } + + /** @test */ + public function it_can_generate_crud_files_for_namespaced_model() + { + $inputName = 'Entities/References/Category'; + $modelName = 'Category'; + $pluralModelName = 'Categories'; + $tableName = 'categories'; + $langName = 'category'; + $modelPath = 'Entities/References'; + + $this->artisan('make:crud', ['name' => $inputName, '--no-interaction' => true]); + + $this->assertNotRegExp("/{$modelName} model already exists./", app(Kernel::class)->output()); + + $this->assertFileExists(app_path($modelPath.'/'.$modelName.'.php')); + $this->assertFileExists(app_path("Http/Controllers/{$pluralModelName}Controller.php")); + + $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$tableName.'_table.php'); + $this->assertFileExists($migrationFilePath); + + $this->assertFileExists(resource_path("views/{$tableName}/index.blade.php")); + $this->assertFileExists(resource_path("views/{$tableName}/forms.blade.php")); + $this->assertFileExists(resource_path("lang/en/{$langName}.php")); + $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); + $this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php")); + $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); + } } diff --git a/tests/Generators/ModelGeneratorTest.php b/tests/Generators/ModelGeneratorTest.php index 17559d7..bb6a354 100644 --- a/tests/Generators/ModelGeneratorTest.php +++ b/tests/Generators/ModelGeneratorTest.php @@ -26,4 +26,25 @@ class {$this->model_name} extends Model "; $this->assertEquals($modelClassContent, file_get_contents($modelPath)); } + + /** @test */ + public function it_creates_correct_namespaced_model_class_content() + { + $this->artisan('make:crud', ['name' => 'Entities/References/Category', '--no-interaction' => true]); + + $modelPath = app_path('Entities/References/Category.php'); + $this->assertFileExists($modelPath); + $modelClassContent = "assertEquals($modelClassContent, file_get_contents($modelPath)); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 35b7380..4879ae8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -37,6 +37,7 @@ abstract class TestCase extends BaseTestCase protected function cleanUpGeneratedFiles() { $this->removeFileOrDir(app_path($this->model_name.'.php')); + $this->removeFileOrDir(app_path('Entities')); $this->removeFileOrDir(app_path('Http')); $this->removeFileOrDir(database_path('migrations')); $this->removeFileOrDir(database_path('factories'));