From 56381a73526212f73280779b0e2c8557077471aa Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 15 Mar 2021 00:09:51 +0800 Subject: [PATCH] Prevent overriding the existing model factory class --- src/Generators/ModelFactoryGenerator.php | 7 ++++- tests/Generators/ModelFactoryGeneratorTest.php | 41 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php index 4fe46bb..3431d39 100644 --- a/src/Generators/ModelFactoryGenerator.php +++ b/src/Generators/ModelFactoryGenerator.php @@ -13,9 +13,14 @@ class ModelFactoryGenerator extends BaseGenerator public function generate(string $type = 'full') { $modelFactoryPath = $this->makeDirectory(database_path('factories')); + $modelFactoryClassPath = $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php'; + + if ($this->files->exists($modelFactoryClassPath)) { + return; + } $this->generateFile( - $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php', + $modelFactoryClassPath, $this->getContent('database/factories/model-factory') ); diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index 0d2fa30..ac00199 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -81,4 +81,45 @@ class {$this->model_name}Factory extends Factory $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath)); $this->removeFileOrDir(base_path('stubs')); } + + /** @test */ + public function it_doesnt_override_the_existing_model_factory_content() + { + // $this->artisan('make:model', ['name' => 'Models/'.$this->model_name, '--no-interaction' => true]); + $this->artisan('make:factory', ['name' => $this->model_name.'Factory', '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + + $modelFactoryPath = database_path('factories/'.$this->model_name.'Factory.php'); + $this->assertFileExists($modelFactoryPath); + $modelFactoryContent = "model_name}Factory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected \$model = Model::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + // + ]; + } +} +"; + $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath)); + } }