diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php index 046adf4..0771116 100644 --- a/src/Generators/ModelFactoryGenerator.php +++ b/src/Generators/ModelFactoryGenerator.php @@ -12,7 +12,11 @@ class ModelFactoryGenerator extends BaseGenerator */ public function generate(string $type = 'full') { - $modelFactoryPath = $this->makeDirectory(database_path('factories')); + $modelFactoryPath = database_path('factories'); + if ($this->modelNames['model_path'] != 'Models') { + $modelFactoryPath .= '/'.$this->modelNames['model_path']; + } + $modelFactoryPath = $this->makeDirectory($modelFactoryPath); $modelFactoryClassPath = $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php'; if ($this->files->exists($modelFactoryClassPath)) { @@ -47,6 +51,12 @@ public function getContent(string $stubName) $modelFactoryFileContent = str_replace($string, $replacement, $modelFactoryFileContent); } + if ($this->modelNames['model_path'] != 'Models') { + $string = 'Database\Factories'; + $replacement = $string.'\\'.str_replace('/', '\\', $this->modelNames['model_path']); + $modelFactoryFileContent = str_replace($string, $replacement, $modelFactoryFileContent); + } + return $this->replaceStubString($modelFactoryFileContent); } } diff --git a/tests/CrudApiMakeCommandTest.php b/tests/CrudApiMakeCommandTest.php index 60a5360..dde115e 100644 --- a/tests/CrudApiMakeCommandTest.php +++ b/tests/CrudApiMakeCommandTest.php @@ -115,7 +115,7 @@ public function it_can_generate_crud_files_for_namespaced_model() $this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php")); $this->assertFileExists(app_path("Policies/{$modelName}Policy.php")); - $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); + $this->assertFileExists(database_path("factories/{$inputName}Factory.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php")); $this->assertFileExists(base_path("tests/Feature/Api/Manage{$modelName}Test.php")); @@ -148,7 +148,7 @@ public function it_can_generate_crud_files_with_parent_option() $localeConfig = config('app.locale'); $this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php")); - $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); + $this->assertFileExists(database_path("factories/{$inputName}Factory.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php")); $this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php")); diff --git a/tests/CrudMakeCommandTest.php b/tests/CrudMakeCommandTest.php index d34c46f..7f4441e 100644 --- a/tests/CrudMakeCommandTest.php +++ b/tests/CrudMakeCommandTest.php @@ -128,7 +128,7 @@ public function it_can_generate_crud_files_for_namespaced_model() $this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php")); $this->assertFileExists(app_path("Policies/{$modelName}Policy.php")); - $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); + $this->assertFileExists(database_path("factories/{$inputName}Factory.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php")); $this->assertFileExists(base_path("tests/Feature/Manage{$modelName}Test.php")); @@ -163,7 +163,7 @@ public function it_can_generate_crud_files_with_parent_option() $localeConfig = config('app.locale'); $this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php")); - $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); + $this->assertFileExists(database_path("factories/{$inputName}Factory.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php")); $this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php")); diff --git a/tests/CrudSimpleMakeCommandTest.php b/tests/CrudSimpleMakeCommandTest.php index de97f88..996c181 100644 --- a/tests/CrudSimpleMakeCommandTest.php +++ b/tests/CrudSimpleMakeCommandTest.php @@ -121,7 +121,7 @@ public function it_can_generate_crud_files_for_namespaced_model() $this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php")); $this->assertFileExists(app_path("Policies/{$modelName}Policy.php")); - $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); + $this->assertFileExists(database_path("factories/{$inputName}Factory.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php")); $this->assertFileExists(base_path("tests/Feature/Manage{$modelName}Test.php")); @@ -154,7 +154,7 @@ public function it_can_generate_crud_files_with_parent_option() $localeConfig = config('app.locale'); $this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php")); - $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); + $this->assertFileExists(database_path("factories/{$inputName}Factory.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Policies/{$modelName}PolicyTest.php")); $this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php")); diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index 846d7ce..161efbf 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -40,6 +40,50 @@ public function definition() $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath)); } + /** @test */ + public function it_creates_correct_model_factory_content_with_namespaced_model() + { + $inputName = 'Entities/References/Category'; + $modelName = 'Category'; + $fullModelName = 'App\Entities\References\Category'; + $pluralModelName = 'Categories'; + $tableName = 'categories'; + $langName = 'category'; + $modelPath = 'Entities/References'; + $factoryNamespace = 'Entities\References'; + + $this->artisan('make:crud-api', ['name' => $inputName, '--no-interaction' => true]); + + $modelFactoryPath = database_path('factories/'.$inputName.'Factory.php'); + $this->assertFileExists($modelFactoryPath); + $modelFactoryContent = " \$this->faker->word, + 'description' => \$this->faker->sentence, + 'creator_id' => function () { + return User::factory()->create()->id; + }, + ]; + } +} +"; + $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath)); + } + + /** @test */ public function it_creates_model_factory_file_content_from_published_stub() {