diff --git a/readme.md b/readme.md index 5b269f1..cf938ae 100644 --- a/readme.md +++ b/readme.md @@ -51,6 +51,8 @@ Item model generated. Item table migration generated. ItemsController generated. Item view files generated. +item lang files generated. +item model factory generated. ManageItemsTest generated. ItemTest (model) generated. CRUD files generated successfully! diff --git a/src/CrudMake.php b/src/CrudMake.php index 1343816..41ed901 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -49,6 +49,7 @@ class CrudMake extends Command $this->generateController(); $this->generateViews(); $this->generateLangFile(); + $this->generateModelFactory(); $this->generateTests(); $this->info('CRUD files generated successfully!'); @@ -109,6 +110,15 @@ class CrudMake extends Command $this->info($this->singleModelName.' lang files generated.'); } + public function generateModelFactory() + { + $modelFactoryPath = $this->makeDirectory(database_path('factories')); + + $this->files->put($modelFactoryPath.'/'.$this->modelName.'Factory.php', $this->getModelFactoryContent()); + + $this->info($this->singleModelName.' model factory generated.'); + } + public function generateTests() { $featureTestPath = $this->makeDirectory(base_path('tests/Feature')); @@ -150,6 +160,12 @@ class CrudMake extends Command return $this->replaceViewDummyStrings($stub)->replaceClass($stub); } + public function getModelFactoryContent() + { + $stub = $this->files->get(__DIR__.'/stubs/model-factory.stub'); + return $this->replaceViewDummyStrings($stub)->replaceClass($stub); + } + public function getFeatureTestContent() { $stub = $this->files->get(__DIR__.'/stubs/test.stub'); diff --git a/src/stubs/model-factory.stub b/src/stubs/model-factory.stub new file mode 100644 index 0000000..4af10c6 --- /dev/null +++ b/src/stubs/model-factory.stub @@ -0,0 +1,12 @@ +define(Master::class, function (Faker $faker) { + + return [ + 'name' => $faker->word, + 'description' => $faker->sentence, + ]; +}); diff --git a/tests/CrudMakeCommandTest.php b/tests/CrudMakeCommandTest.php index a645c31..3c7f07f 100644 --- a/tests/CrudMakeCommandTest.php +++ b/tests/CrudMakeCommandTest.php @@ -18,6 +18,7 @@ class CrudMakeCommandTest extends TestCase $this->assertFileExists(resource_path("views/{$this->tableName}/index.blade.php")); $this->assertFileExists(resource_path("views/{$this->tableName}/forms.blade.php")); $this->assertFileExists(resource_path("lang/en/{$this->singleModelName}.php")); + $this->assertFileExists(database_path("factories/{$this->modelName}Factory.php")); $this->assertFileExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$this->modelName}Test.php")); } diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php new file mode 100644 index 0000000..7263bac --- /dev/null +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -0,0 +1,31 @@ +artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + + $modelFactoryPath = database_path('factories/'.$this->modelName.'Factory.php'); + $this->assertFileExists($modelFactoryPath); + $modelFactoryContent = "define(Item::class, function (Faker \$faker) { + + return [ + 'name' => \$faker->word, + 'description' => \$faker->sentence, + ]; +}); +"; + $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath)); + } +}