From b1da3471d066043190e4fddcd4530f7d26ebe308 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Wed, 11 Nov 2020 20:06:57 +0800 Subject: [PATCH] Generate CRUD files for the existing model with confirmation --- .gitignore | 3 ++- src/CrudMake.php | 7 +++++-- tests/CrudMakeCommandTest.php | 39 +++++++++++++++++++------------------ tests/CrudSimpleMakeCommandTest.php | 7 ++++--- tests/TestCase.php | 2 +- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 49ce3c1..f721ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/vendor \ No newline at end of file +/vendor +/.phpunit.result.cache diff --git a/src/CrudMake.php b/src/CrudMake.php index 757de13..05ac521 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -33,8 +33,11 @@ class CrudMake extends GeneratorCommand $this->getModelName(); if ($this->modelExists()) { - $this->error("{$this->modelNames['model_name']} model already exists."); - return; + $confirm = $this->confirm('Model file exists, are you sure to generate CRUD files?', 'no'); + if (in_array($confirm, ['yes', 'y']) == false) { + $this->error("{$this->modelNames['model_name']} model already exists."); + return; + } } // Warn if it has no default layout view based on diff --git a/tests/CrudMakeCommandTest.php b/tests/CrudMakeCommandTest.php index 5029b18..ca53b52 100644 --- a/tests/CrudMakeCommandTest.php +++ b/tests/CrudMakeCommandTest.php @@ -36,42 +36,43 @@ class CrudMakeCommandTest extends TestCase } /** @test */ - public function it_cannot_generate_crud_files_if_model_exists() + public function it_generates_crud_files_for_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]); - - $this->assertContains("{$this->model_name} model already exists.", app(Kernel::class)->output()); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]) + ->expectsQuestion('Model file exists, are you sure to generate CRUD files?', 'yes'); $this->assertFileExists(app_path($this->model_name.'.php')); - $this->assertFileNotExists(app_path("Http/Controllers/{$this->model_name}Controller.php")); + $this->assertFileExists(app_path("Http/Controllers/{$this->model_name}Controller.php")); $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->table_name.'_table.php'); - $this->assertFileNotExists($migrationFilePath); + $this->assertFileExists($migrationFilePath); - $this->assertFileNotExists(resource_path("views/{$this->table_name}/index.blade.php")); - $this->assertFileNotExists(resource_path("views/{$this->table_name}/create.blade.php")); - $this->assertFileNotExists(resource_path("views/{$this->table_name}/edit.blade.php")); + $this->assertFileExists(resource_path("views/{$this->table_name}/index.blade.php")); + $this->assertFileExists(resource_path("views/{$this->table_name}/create.blade.php")); + $this->assertFileExists(resource_path("views/{$this->table_name}/edit.blade.php")); $this->assertFileNotExists(resource_path("views/{$this->table_name}/forms.blade.php")); $localeConfig = config('app.locale'); - $this->assertFileNotExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php")); + $this->assertFileExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php")); - $this->assertFileNotExists(base_path("routes/web.php")); - $this->assertFileNotExists(app_path("Policies/{$this->model_name}Policy.php")); - $this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php")); - $this->assertFileNotExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); - $this->assertFileNotExists(base_path("tests/Unit/Policies/{$this->model_name}PolicyTest.php")); - $this->assertFileNotExists(base_path("tests/Feature/Manage{$this->model_name}Test.php")); + $this->assertFileExists(base_path("routes/web.php")); + $this->assertFileExists(app_path("Policies/{$this->model_name}Policy.php")); + $this->assertFileExists(database_path("factories/{$this->model_name}Factory.php")); + $this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); + $this->assertFileExists(base_path("tests/Unit/Policies/{$this->model_name}PolicyTest.php")); + $this->assertFileExists(base_path("tests/Feature/Manage{$this->model_name}Test.php")); } /** @test */ public function it_cannot_generate_crud_files_if_namespaced_model_exists() { + $this->mockConsoleOutput = true; $this->artisan('make:model', ['name' => 'Entities/Projects/Problem', '--no-interaction' => true]); - $this->artisan('make:crud', ['name' => 'Entities/Projects/Problem', '--no-interaction' => true]); - - $this->assertContains("Problem model already exists.", app(Kernel::class)->output()); + $this->artisan('make:crud', ['name' => 'Entities/Projects/Problem', '--no-interaction' => true]) + ->expectsQuestion('Model file exists, are you sure to generate CRUD files?', 'no') + ->expectsOutput('Problem model already exists.'); $this->assertFileExists(app_path('Entities/Projects/Problem.php')); $this->assertFileNotExists(app_path("Http/Controllers/ProblemsController.php")); diff --git a/tests/CrudSimpleMakeCommandTest.php b/tests/CrudSimpleMakeCommandTest.php index 155358c..11c155e 100644 --- a/tests/CrudSimpleMakeCommandTest.php +++ b/tests/CrudSimpleMakeCommandTest.php @@ -36,10 +36,11 @@ class CrudSimpleCommandTest extends TestCase /** @test */ public function it_cannot_generate_crud_files_if_model_exists() { + $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]); - - $this->assertContains("{$this->model_name} model already exists.", app(Kernel::class)->output()); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]) + ->expectsQuestion('Model file exists, are you sure to generate CRUD files?', 'no') + ->expectsOutput("{$this->model_name} model already exists."); $this->assertFileExists(app_path($this->model_name.'.php')); $this->assertFileNotExists(app_path("Http/Controllers/{$this->model_name}Controller.php")); diff --git a/tests/TestCase.php b/tests/TestCase.php index 2f7c234..7723a4d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -21,7 +21,7 @@ abstract class TestCase extends BaseTestCase $this->model_name = 'MemberType'; $this->full_model_name = 'App\\'.$this->model_name; - $this->plural_model_name = str_plural($this->model_name); + $this->plural_model_name = Str::plural($this->model_name); $this->table_name = Str::snake($this->plural_model_name); $this->lang_name = Str::snake($this->model_name); $this->collection_model_var_name = Str::camel($this->plural_model_name);