From 403e19a47f0efa8f118e18cd967b8f0f912081a5 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 14 Mar 2021 23:44:03 +0800 Subject: [PATCH 1/5] Prevent overriding the existing model class --- src/Generators/ModelGenerator.php | 10 ++++++---- tests/Generators/ModelGeneratorTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index c98b7ec..11a3442 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -14,11 +14,13 @@ 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)) { + return; + } + + $this->generateFile($modelClassPath, $this->getContent('models/model')); $this->command->info($this->modelNames['model_name'].' model generated.'); } diff --git a/tests/Generators/ModelGeneratorTest.php b/tests/Generators/ModelGeneratorTest.php index c6ab792..2d99b5f 100644 --- a/tests/Generators/ModelGeneratorTest.php +++ b/tests/Generators/ModelGeneratorTest.php @@ -97,4 +97,30 @@ 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; + config(['auth.providers.users.model' => 'App\Models\User']); + $this->artisan('make:model', ['name' => 'Models/'.$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('Models/'.$this->model_name.'.php'); + $this->assertFileExists($modelPath); + $modelClassContent = "model_name} extends Model +{ + use HasFactory; +} +"; + $this->assertEquals($modelClassContent, file_get_contents($modelPath)); + } } From bcec1e330c770ad8996ccbd11b4fb0f5398fd3b6 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 14 Mar 2021 23:58:58 +0800 Subject: [PATCH 2/5] Prevent overriding the existing model policy class --- src/Generators/ModelPolicyGenerator.php | 10 +++++--- tests/Generators/ModelPolicyGeneratorTest.php | 36 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Generators/ModelPolicyGenerator.php b/src/Generators/ModelPolicyGenerator.php index 8f6dbd6..5df21e0 100644 --- a/src/Generators/ModelPolicyGenerator.php +++ b/src/Generators/ModelPolicyGenerator.php @@ -17,11 +17,13 @@ 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)) { + return; + } + + $this->generateFile($modelPolicyClassPath, $this->getContent('models/model-policy')); $this->command->info($this->modelNames['model_name'].' model policy generated.'); diff --git a/tests/Generators/ModelPolicyGeneratorTest.php b/tests/Generators/ModelPolicyGeneratorTest.php index 63fd0ba..7cfc45f 100644 --- a/tests/Generators/ModelPolicyGeneratorTest.php +++ b/tests/Generators/ModelPolicyGeneratorTest.php @@ -229,4 +229,40 @@ 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'); + // dd(file_get_contents($modelPolicyPath)); + $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)); + } } From 56381a73526212f73280779b0e2c8557077471aa Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 15 Mar 2021 00:09:51 +0800 Subject: [PATCH 3/5] 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)); + } } From 62514f5206b70f9756f76a21d970424ad872aa30 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 15 Mar 2021 00:16:19 +0800 Subject: [PATCH 4/5] Remove unused lines --- tests/Generators/ModelFactoryGeneratorTest.php | 1 - tests/Generators/ModelPolicyGeneratorTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index ac00199..846d7ce 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -85,7 +85,6 @@ class {$this->model_name}Factory extends Factory /** @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]); diff --git a/tests/Generators/ModelPolicyGeneratorTest.php b/tests/Generators/ModelPolicyGeneratorTest.php index 7cfc45f..4629415 100644 --- a/tests/Generators/ModelPolicyGeneratorTest.php +++ b/tests/Generators/ModelPolicyGeneratorTest.php @@ -239,7 +239,6 @@ class AuthServiceProvider extends ServiceProvider $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); $modelPolicyPath = app_path('Policies/'.$this->model_name.'Policy.php'); - // dd(file_get_contents($modelPolicyPath)); $this->assertFileExists($modelPolicyPath); $modelPolicyContent = " Date: Mon, 15 Mar 2021 22:20:01 +0800 Subject: [PATCH 5/5] Add console warn for the extsting classes --- src/Generators/ModelFactoryGenerator.php | 1 + src/Generators/ModelGenerator.php | 1 + src/Generators/ModelPolicyGenerator.php | 1 + 3 files changed, 3 insertions(+) diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php index 3431d39..c1e47da 100644 --- a/src/Generators/ModelFactoryGenerator.php +++ b/src/Generators/ModelFactoryGenerator.php @@ -16,6 +16,7 @@ class ModelFactoryGenerator extends BaseGenerator $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; } diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 11a3442..79940a5 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -17,6 +17,7 @@ class ModelGenerator extends BaseGenerator $modelClassPath = $modelDirectory.'/'.$this->modelNames['model_name'].'.php'; if ($this->files->exists($modelClassPath)) { + $this->command->warn('Use the existing '.$this->modelNames['model_name'].' model.'); return; } diff --git a/src/Generators/ModelPolicyGenerator.php b/src/Generators/ModelPolicyGenerator.php index 5df21e0..38302ed 100644 --- a/src/Generators/ModelPolicyGenerator.php +++ b/src/Generators/ModelPolicyGenerator.php @@ -20,6 +20,7 @@ class ModelPolicyGenerator extends BaseGenerator $modelPolicyClassPath = $modelPolicyPath.'/'.$this->modelNames['model_name'].'Policy.php'; if ($this->files->exists($modelPolicyClassPath)) { + $this->command->warn('Use the existing '.$this->modelNames['model_name'].' model policy.'); return; }