From 5b3884162ab702417af6d60e78391944a87298a4 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 20 Apr 2021 13:40:24 +0800 Subject: [PATCH 1/2] Adopt laravel 8.x model factory directory structure --- src/Generators/ModelFactoryGenerator.php | 6 +++++- tests/CrudApiMakeCommandTest.php | 4 ++-- tests/CrudMakeCommandTest.php | 4 ++-- tests/CrudSimpleMakeCommandTest.php | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php index 046adf4..6be8b9a 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)) { 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 @@ class CrudApiMakeCommandTest extends TestCase $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 @@ class CrudApiMakeCommandTest extends TestCase $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 @@ class CrudMakeCommandTest extends TestCase $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 @@ class CrudMakeCommandTest extends TestCase $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 @@ class CrudSimpleMakeCommandTest extends TestCase $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 @@ class CrudSimpleMakeCommandTest extends TestCase $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")); From bb77d9214ed05e0faea37ea538df5a8da7dd4259 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 20 Apr 2021 14:00:41 +0800 Subject: [PATCH 2/2] Update factory model class namespace --- src/Generators/ModelFactoryGenerator.php | 6 ++++ tests/Generators/ModelFactoryGeneratorTest.php | 44 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php index 6be8b9a..0771116 100644 --- a/src/Generators/ModelFactoryGenerator.php +++ b/src/Generators/ModelFactoryGenerator.php @@ -51,6 +51,12 @@ class ModelFactoryGenerator extends BaseGenerator $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/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index 846d7ce..161efbf 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -41,6 +41,50 @@ class {$this->model_name}Factory extends Factory } /** @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() { app('files')->makeDirectory(base_path('stubs/simple-crud/database/factories'), 0777, true, true);