diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php index 4fe46bb..c1e47da 100644 --- a/src/Generators/ModelFactoryGenerator.php +++ b/src/Generators/ModelFactoryGenerator.php @@ -13,9 +13,15 @@ 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)) { + $this->command->warn('Use the existing '.$this->modelNames['model_name'].' model factory.'); + return; + } $this->generateFile( - $modelFactoryPath.'/'.$this->modelNames['model_name'].'Factory.php', + $modelFactoryClassPath, $this->getContent('database/factories/model-factory') ); diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index c98b7ec..79940a5 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -14,11 +14,14 @@ class ModelGenerator extends BaseGenerator { $modelPath = $this->modelNames['model_path']; $modelDirectory = $this->makeDirectory(app_path($modelPath)); + $modelClassPath = $modelDirectory.'/'.$this->modelNames['model_name'].'.php'; - $this->generateFile( - $modelDirectory.'/'.$this->modelNames['model_name'].'.php', - $this->getContent('models/model') - ); + if ($this->files->exists($modelClassPath)) { + $this->command->warn('Use the existing '.$this->modelNames['model_name'].' model.'); + return; + } + + $this->generateFile($modelClassPath, $this->getContent('models/model')); $this->command->info($this->modelNames['model_name'].' model generated.'); } diff --git a/src/Generators/ModelPolicyGenerator.php b/src/Generators/ModelPolicyGenerator.php index 8f6dbd6..38302ed 100644 --- a/src/Generators/ModelPolicyGenerator.php +++ b/src/Generators/ModelPolicyGenerator.php @@ -17,11 +17,14 @@ class ModelPolicyGenerator extends BaseGenerator $parentDirectory = '/'.$this->command->option('parent'); } $modelPolicyPath = $this->makeDirectory(app_path('Policies'.$parentDirectory)); + $modelPolicyClassPath = $modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php'; - $this->generateFile( - $modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php', - $this->getContent('models/model-policy') - ); + if ($this->files->exists($modelPolicyClassPath)) { + $this->command->warn('Use the existing '.$this->modelNames['model_name'].' model policy.'); + return; + } + + $this->generateFile($modelPolicyClassPath, $this->getContent('models/model-policy')); $this->command->info($this->modelNames['model_name'].' model policy generated.'); diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index 0633396..d052bf5 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -74,4 +74,28 @@ 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: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 = "define(Model::class, function (Faker \$faker) { + return [ + // + ]; +}); +"; + $this->assertEquals($modelFactoryContent, file_get_contents($modelFactoryPath)); + } } diff --git a/tests/Generators/ModelGeneratorTest.php b/tests/Generators/ModelGeneratorTest.php index 5293bf6..fabcb1f 100644 --- a/tests/Generators/ModelGeneratorTest.php +++ b/tests/Generators/ModelGeneratorTest.php @@ -89,4 +89,28 @@ class Category extends Model $this->removeFileOrDir(resource_path('views/categories')); $this->removeFileOrDir(resource_path("lang/en/category.php")); } + + /** @test */ + public function it_doesnt_override_the_existing_model() + { + $this->mockConsoleOutput = true; + $this->artisan('make:model', ['name' => $this->model_name, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]) + ->expectsQuestion('Model file exists, are you sure to generate CRUD files?', true); + + $modelPath = app_path($this->model_name.'.php'); + $this->assertFileExists($modelPath); + $modelClassContent = "model_name} extends Model +{ + // +} +"; + $this->assertEquals($modelClassContent, file_get_contents($modelPath)); + } } diff --git a/tests/Generators/ModelPolicyGeneratorTest.php b/tests/Generators/ModelPolicyGeneratorTest.php index 63fd0ba..a140378 100644 --- a/tests/Generators/ModelPolicyGeneratorTest.php +++ b/tests/Generators/ModelPolicyGeneratorTest.php @@ -229,4 +229,39 @@ class AuthServiceProvider extends ServiceProvider "; $this->assertEquals($authSPContent, file_get_contents($authSPPath)); } + + /** @test */ + public function it_doesnt_override_the_existing_model_policy_content() + { + $userModel = config('auth.providers.users.model'); + + $this->artisan('make:policy', ['name' => $this->model_name.'Policy', '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + + $modelPolicyPath = app_path('Policies/'.$this->model_name.'Policy.php'); + $this->assertFileExists($modelPolicyPath); + $modelPolicyContent = "model_name}Policy +{ + use HandlesAuthorization; + + /** + * Create a new policy instance. + * + * @return void + */ + public function __construct() + { + // + } +} +"; + $this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath)); + } }