From 47bc4d2a75b5f273b620fcf86db624225e788fe1 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Fri, 7 Sep 2018 11:54:10 +0800 Subject: [PATCH] Generates bootstrap3 simple views for --bs3 option Add --bs3 option for make:crud and make:crud-simple --- src/CrudMake.php | 2 +- src/CrudSimpleMake.php | 2 +- src/Generators/FormViewGenerator.php | 4 + src/Generators/IndexViewGenerator.php | 4 + tests/CommandOptions/SimpleCrudBs3OptionsTest.php | 187 ++++++++++++++++++++++ 5 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 tests/CommandOptions/SimpleCrudBs3OptionsTest.php diff --git a/src/CrudMake.php b/src/CrudMake.php index 88cc1b9..46f9fa1 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -9,7 +9,7 @@ class CrudMake extends GeneratorCommand * * @var string */ - protected $signature = 'make:crud {name} {--p|parent=} {--t|tests-only} {--f|formfield}'; + protected $signature = 'make:crud {name} {--p|parent=} {--t|tests-only} {--f|formfield} {--bs3}'; /** * The console command description. diff --git a/src/CrudSimpleMake.php b/src/CrudSimpleMake.php index fb7ab92..05e59f8 100644 --- a/src/CrudSimpleMake.php +++ b/src/CrudSimpleMake.php @@ -9,7 +9,7 @@ class CrudSimpleMake extends GeneratorCommand * * @var string */ - protected $signature = 'make:crud-simple {name} {--p|parent=} {--t|tests-only} {--f|formfield}'; + protected $signature = 'make:crud-simple {name} {--p|parent=} {--t|tests-only} {--f|formfield} {--bs3}'; /** * The console command description. diff --git a/src/Generators/FormViewGenerator.php b/src/Generators/FormViewGenerator.php index 70fc928..a983c44 100644 --- a/src/Generators/FormViewGenerator.php +++ b/src/Generators/FormViewGenerator.php @@ -33,6 +33,10 @@ class FormViewGenerator extends BaseGenerator $stubName .= '-formfield'; } + if ($this->command->option('bs3')) { + $stubName .= '-bs3'; + } + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/src/Generators/IndexViewGenerator.php b/src/Generators/IndexViewGenerator.php index 58b4908..e7c6f19 100644 --- a/src/Generators/IndexViewGenerator.php +++ b/src/Generators/IndexViewGenerator.php @@ -28,6 +28,10 @@ class IndexViewGenerator extends BaseGenerator $stubName .= '-formfield'; } + if ($this->command->option('bs3')) { + $stubName .= '-bs3'; + } + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/tests/CommandOptions/SimpleCrudBs3OptionsTest.php b/tests/CommandOptions/SimpleCrudBs3OptionsTest.php new file mode 100644 index 0000000..5e20c70 --- /dev/null +++ b/tests/CommandOptions/SimpleCrudBs3OptionsTest.php @@ -0,0 +1,187 @@ +artisan('make:crud-simple', ['name' => $this->model_name, '--bs3' => true]); + + $this->assertNotContains("{$this->model_name} model already exists.", app(Kernel::class)->output()); + + $this->assertFileExists(app_path($this->model_name.'.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->assertFileExists($migrationFilePath); + + $this->assertFileExists(resource_path("views/{$this->table_name}/index.blade.php")); + $this->assertFileExists(resource_path("views/{$this->table_name}/forms.blade.php")); + + $localeConfig = config('app.locale'); + $this->assertFileExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.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/Feature/Manage{$this->model_name}Test.php")); + } + + /** @test */ + public function it_creates_correct_index_view_content_with_bootstrap3() + { + $this->artisan('make:crud-simple', ['name' => $this->model_name, '--bs3' => true]); + + $indexViewPath = resource_path("views/{$this->table_name}/index.blade.php"); + $this->assertFileExists($indexViewPath); + $indexViewContent = "@extends('layouts.app') + +@section('title', __('{$this->lang_name}.list')) + +@section('content') +

+ + {{ __('{$this->lang_name}.list') }} + {{ __('app.total') }} : {{ \${$this->collection_model_var_name}->total() }} {{ __('{$this->lang_name}.{$this->lang_name}') }} +

+
+
+
+
+
+
+ + lang_name}.search_text') }}\" name=\"q\" type=\"text\" id=\"q\" class=\"form-control input-sm\" value=\"{{ request('q') }}\"> +
+ lang_name}.search') }}\" class=\"btn btn-sm\"> + table_name}.index') }}\">{{ __('app.reset') }} +
+
+ + + + + + + + + + + @foreach(\${$this->collection_model_var_name} as \$key => \${$this->single_model_var_name}) + + + + + + + @endforeach + +
{{ __('app.table_no') }}{{ __('{$this->lang_name}.name') }}{{ __('{$this->lang_name}.description') }}{{ __('app.action') }}
{{ \${$this->collection_model_var_name}->firstItem() + \$key }}{{ \${$this->single_model_var_name}->name }}{{ \${$this->single_model_var_name}->description }} + @can('update', \${$this->single_model_var_name}) + table_name}.index', ['action' => 'edit', 'id' => \${$this->single_model_var_name}->id] + Request::only('page', 'q')) }}\" id=\"edit-{$this->lang_name}-{{ \${$this->single_model_var_name}->id }}\">{{ __('app.edit') }} + @endcan +
+
{{ \${$this->collection_model_var_name}->appends(Request::except('page'))->render() }}
+
+
+
+ @if(Request::has('action')) + @include('{$this->table_name}.forms') + @endif +
+
+@endsection +"; + $this->assertEquals($indexViewContent, file_get_contents($indexViewPath)); + } + + /** @test */ + public function it_creates_correct_forms_view_content_with_bootstrap3() + { + $this->artisan('make:crud-simple', ['name' => $this->model_name, '--bs3' => true]); + + $formViewPath = resource_path("views/{$this->table_name}/forms.blade.php"); + $this->assertFileExists($formViewPath); + $formViewContent = "@if (Request::get('action') == 'create') +@can('create', new {$this->full_model_name}) +
table_name}.store') }}\" accept-charset=\"UTF-8\"> + {{ csrf_field() }} +
has('name') ? ' has-error' : '' }}\"> + + + {!! \$errors->first('name', ':message') !!} +
+
has('description') ? ' has-error' : '' }}\"> + + + {!! \$errors->first('description', ':message') !!} +
+ lang_name}.create') }}\" class=\"btn btn-success\"> + table_name}.index') }}\" class=\"btn btn-default\">{{ __('app.cancel') }} +
+@endcan +@endif +@if (Request::get('action') == 'edit' && \$editable{$this->model_name}) +@can('update', \$editable{$this->model_name}) +
table_name}.update', \$editable{$this->model_name}) }}\" accept-charset=\"UTF-8\"> + {{ csrf_field() }} {{ method_field('patch') }} +
has('name') ? ' has-error' : '' }}\"> + + model_name}->name) }}\" required> + {!! \$errors->first('name', ':message') !!} +
+
has('description') ? ' has-error' : '' }}\"> + + + {!! \$errors->first('description', ':message') !!} +
+ + + lang_name}.update') }}\" class=\"btn btn-success\"> + table_name}.index', Request::only('q', 'page')) }}\" class=\"btn btn-default\">{{ __('app.cancel') }} + @can('delete', \$editable{$this->model_name}) + table_name}.index', ['action' => 'delete', 'id' => \$editable{$this->model_name}->id] + Request::only('page', 'q')) }}\" id=\"del-{$this->lang_name}-{{ \$editable{$this->model_name}->id }}\" class=\"btn btn-danger pull-right\">{{ __('app.delete') }} + @endcan +
+@endcan +@endif +@if (Request::get('action') == 'delete' && \$editable{$this->model_name}) +@can('delete', \$editable{$this->model_name}) +
+

{{ __('{$this->lang_name}.delete') }}

+
+ +

{{ \$editable{$this->model_name}->name }}

+ +

{{ \$editable{$this->model_name}->description }}

+ {!! \$errors->first('{$this->lang_name}_id', ':message') !!} +
+
+
{{ __('{$this->lang_name}.delete_confirm') }}
+
+
table_name}.destroy', \$editable{$this->model_name}) }}\" accept-charset=\"UTF-8\" onsubmit=\"return confirm("Are you sure to delete this?")\" class=\"del-form pull-right\" style=\"display: inline;\"> + {{ csrf_field() }} {{ method_field('delete') }} + lang_name}_id\" type=\"hidden\" value=\"{{ \$editable{$this->model_name}->id }}\"> + + + +
+ table_name}.index', Request::only('q', 'page')) }}\" class=\"btn btn-default\">{{ __('app.cancel') }} +
+
+@endcan +@endif +"; + $this->assertEquals($formViewContent, file_get_contents($formViewPath)); + } +}