diff --git a/src/CrudMake.php b/src/CrudMake.php index 8d0f704..88cc1b9 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}'; + protected $signature = 'make:crud {name} {--p|parent=} {--t|tests-only} {--f|formfield}'; /** * The console command description. diff --git a/src/CrudSimpleMake.php b/src/CrudSimpleMake.php index 3521dfc..fb7ab92 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}'; + protected $signature = 'make:crud-simple {name} {--p|parent=} {--t|tests-only} {--f|formfield}'; /** * The console command description. diff --git a/src/Generators/FormViewGenerator.php b/src/Generators/FormViewGenerator.php index 53919af..70fc928 100644 --- a/src/Generators/FormViewGenerator.php +++ b/src/Generators/FormViewGenerator.php @@ -29,6 +29,10 @@ class FormViewGenerator extends BaseGenerator */ public function getContent(string $stubName) { + if ($this->command->option('formfield')) { + $stubName .= '-formfield'; + } + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/src/Generators/IndexViewGenerator.php b/src/Generators/IndexViewGenerator.php index f27623f..58b4908 100644 --- a/src/Generators/IndexViewGenerator.php +++ b/src/Generators/IndexViewGenerator.php @@ -24,6 +24,10 @@ class IndexViewGenerator extends BaseGenerator */ public function getContent(string $stubName) { + if ($this->command->option('formfield')) { + $stubName .= '-formfield'; + } + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/src/Generators/ShowViewGenerator.php b/src/Generators/ShowViewGenerator.php index e2065e5..11f10b7 100644 --- a/src/Generators/ShowViewGenerator.php +++ b/src/Generators/ShowViewGenerator.php @@ -24,6 +24,10 @@ class ShowViewGenerator extends BaseGenerator */ public function getContent(string $stubName) { + if ($this->command->option('formfield')) { + $stubName .= '-formfield'; + } + return $this->replaceStubString($this->getStubFileContent($stubName)); } } diff --git a/src/stubs/resources/views/full/create-formfield.stub b/src/stubs/resources/views/full/create-formfield.stub index b4f2704..9cd22ca 100644 --- a/src/stubs/resources/views/full/create-formfield.stub +++ b/src/stubs/resources/views/full/create-formfield.stub @@ -7,16 +7,16 @@

{{ __('master.create') }}

- {!! Form::open(['route' => 'masters.store']) !!} + {{ Form::open(['route' => 'masters.store']) }}
{!! FormField::text('name', ['required' => true, 'label' => __('master.name')]) !!} {!! FormField::textarea('description', ['label' => __('master.description')]) !!}
- {!! Form::close() !!} + {{ Form::close() }}
diff --git a/src/stubs/resources/views/full/edit-formfield.stub b/src/stubs/resources/views/full/edit-formfield.stub index d23de5b..71d02ad 100644 --- a/src/stubs/resources/views/full/edit-formfield.stub +++ b/src/stubs/resources/views/full/edit-formfield.stub @@ -36,19 +36,19 @@ @else

{{ __('master.edit') }}

- {!! Form::model($singleMstr, ['route' => ['masters.update', $singleMstr],'method' => 'patch']) !!} + {{ Form::model($singleMstr, ['route' => ['masters.update', $singleMstr],'method' => 'patch']) }}
{!! FormField::text('name', ['required' => true, 'label' => __('master.name')]) !!} {!! FormField::textarea('description', ['label' => __('master.description')]) !!}
- {!! Form::close() !!} + {{ Form::close() }}
diff --git a/src/stubs/resources/views/full/index-formfield.stub b/src/stubs/resources/views/full/index-formfield.stub index a10b0a0..66c91fb 100644 --- a/src/stubs/resources/views/full/index-formfield.stub +++ b/src/stubs/resources/views/full/index-formfield.stub @@ -39,12 +39,12 @@ {{ $singleMstr->description }} @can('view', $singleMstr) - {!! link_to_route( + {{ link_to_route( 'masters.show', __('app.show'), [$singleMstr], ['class' => 'btn btn-default btn-xs', 'id' => 'show-master-' . $singleMstr->id] - ) !!} + ) }} @endcan diff --git a/src/stubs/resources/views/simple/index-formfield.stub b/src/stubs/resources/views/simple/index-formfield.stub index b602cbe..79d78ff 100644 --- a/src/stubs/resources/views/simple/index-formfield.stub +++ b/src/stubs/resources/views/simple/index-formfield.stub @@ -39,12 +39,12 @@ {{ $singleMstr->description }} @can('update', $singleMstr) - {!! link_to_route( + {{ link_to_route( 'masters.index', __('app.edit'), ['action' => 'edit', 'id' => $singleMstr->id] + Request::only('page', 'q'), ['id' => 'edit-master-'.$singleMstr->id] - ) !!} + ) }} @endcan diff --git a/tests/CommandOptions/FullCrudFormfieldOptionsTest.php b/tests/CommandOptions/FullCrudFormfieldOptionsTest.php new file mode 100644 index 0000000..a66c641 --- /dev/null +++ b/tests/CommandOptions/FullCrudFormfieldOptionsTest.php @@ -0,0 +1,245 @@ +artisan('make:crud', ['name' => $this->model_name, '--formfield' => 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}/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->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_formfield() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--formfield' => 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') +

+
+ @can('create', new {$this->full_model_name}) + {{ link_to_route('{$this->table_name}.create', __('{$this->lang_name}.create'), [], ['class' => 'btn btn-success']) }} + @endcan +
+ {{ __('{$this->lang_name}.list') }} + {{ __('app.total') }} : {{ \${$this->collection_model_var_name}->total() }} {{ __('{$this->lang_name}.{$this->lang_name}') }} +

+
+
+
+
+ {{ Form::open(['method' => 'get','class' => 'form-inline']) }} + {!! FormField::text('q', ['label' => __('{$this->lang_name}.search'), 'placeholder' => __('{$this->lang_name}.search_text'), 'class' => 'input-sm']) !!} + {{ Form::submit(__('{$this->lang_name}.search'), ['class' => 'btn btn-sm']) }} + {{ link_to_route('{$this->table_name}.index', __('app.reset')) }} + {{ Form::close() }} +
+ + + + + + + + + + + @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_link }}{{ \${$this->single_model_var_name}->description }} + @can('view', \${$this->single_model_var_name}) + {{ link_to_route( + '{$this->table_name}.show', + __('app.show'), + [\${$this->single_model_var_name}], + ['class' => 'btn btn-default btn-xs', 'id' => 'show-{$this->lang_name}-' . \${$this->single_model_var_name}->id] + ) }} + @endcan +
+
{{ \${$this->collection_model_var_name}->appends(Request::except('page'))->render() }}
+
+
+
+@endsection +"; + $this->assertEquals($indexViewContent, file_get_contents($indexViewPath)); + } + + /** @test */ + public function it_creates_correct_show_view_content_with_formfield() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--formfield' => true]); + + $showFormViewPath = resource_path("views/{$this->table_name}/show.blade.php"); + $this->assertFileExists($showFormViewPath); + + $showFormViewContent = "@extends('layouts.app') + +@section('title', __('{$this->lang_name}.detail')) + +@section('content') +
+
+
+

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

+ + + + + +
{{ __('{$this->lang_name}.name') }}{{ \${$this->single_model_var_name}->name }}
{{ __('{$this->lang_name}.description') }}{{ \${$this->single_model_var_name}->description }}
+
+ @can('update', \${$this->single_model_var_name}) + {{ link_to_route('{$this->table_name}.edit', __('{$this->lang_name}.edit'), [\${$this->single_model_var_name}], ['class' => 'btn btn-warning', 'id' => 'edit-{$this->lang_name}-'.\${$this->single_model_var_name}->id]) }} + @endcan + {{ link_to_route('{$this->table_name}.index', __('{$this->lang_name}.back_to_index'), [], ['class' => 'btn btn-default']) }} +
+
+
+
+@endsection +"; + $this->assertEquals($showFormViewContent, file_get_contents($showFormViewPath)); + } + + /** @test */ + public function it_creates_correct_create_view_content_with_formfield() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--formfield' => true]); + + $createFormViewPath = resource_path("views/{$this->table_name}/create.blade.php"); + $this->assertFileExists($createFormViewPath); + $createFormViewContent = "@extends('layouts.app') + +@section('title', __('{$this->lang_name}.create')) + +@section('content') +
+
+
+

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

+ {{ Form::open(['route' => '{$this->table_name}.store']) }} +
+ {!! FormField::text('name', ['required' => true, 'label' => __('{$this->lang_name}.name')]) !!} + {!! FormField::textarea('description', ['label' => __('{$this->lang_name}.description')]) !!} +
+
+ {{ Form::submit(__('{$this->lang_name}.create'), ['class' => 'btn btn-success']) }} + {{ link_to_route('{$this->table_name}.index', __('app.cancel'), [], ['class' => 'btn btn-default']) }} +
+ {{ Form::close() }} +
+
+
+@endsection +"; + $this->assertEquals($createFormViewContent, file_get_contents($createFormViewPath)); + } + + /** @test */ + public function it_creates_correct_edit_view_content_with_formfield() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--formfield' => true]); + + $editFormViewPath = resource_path("views/{$this->table_name}/edit.blade.php"); + $this->assertFileExists($editFormViewPath); + $editFormViewContent = "@extends('layouts.app') + +@section('title', __('{$this->lang_name}.edit')) + +@section('content') +
+
+ @if (request('action') == 'delete' && \${$this->single_model_var_name}) + @can('delete', \${$this->single_model_var_name}) +
+

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

+
+ +

{{ \${$this->single_model_var_name}->name }}

+ +

{{ \${$this->single_model_var_name}->description }}

+ {!! \$errors->first('{$this->lang_name}_id', ':message') !!} +
+
+
{{ __('{$this->lang_name}.delete_confirm') }}
+
+ {!! FormField::delete( + ['route' => ['{$this->table_name}.destroy', \${$this->single_model_var_name}]], + __('app.delete_confirm_button'), + ['class'=>'btn btn-danger'], + [ + '{$this->lang_name}_id' => \${$this->single_model_var_name}->id, + 'page' => request('page'), + 'q' => request('q'), + ] + ) !!} + {{ link_to_route('{$this->table_name}.edit', __('app.cancel'), [\${$this->single_model_var_name}], ['class' => 'btn btn-default']) }} +
+
+ @endcan + @else +
+

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

+ {{ Form::model(\${$this->single_model_var_name}, ['route' => ['{$this->table_name}.update', \${$this->single_model_var_name}],'method' => 'patch']) }} +
+ {!! FormField::text('name', ['required' => true, 'label' => __('{$this->lang_name}.name')]) !!} + {!! FormField::textarea('description', ['label' => __('{$this->lang_name}.description')]) !!} +
+
+ {{ Form::submit(__('{$this->lang_name}.update'), ['class' => 'btn btn-success']) }} + {{ link_to_route('{$this->table_name}.show', __('app.cancel'), [\${$this->single_model_var_name}], ['class' => 'btn btn-default']) }} + @can('delete', \${$this->single_model_var_name}) + {{ link_to_route('{$this->table_name}.edit', __('app.delete'), [\${$this->single_model_var_name}, 'action' => 'delete'], ['class' => 'btn btn-danger pull-right', 'id' => 'del-{$this->lang_name}-'.\${$this->single_model_var_name}->id]) }} + @endcan +
+ {{ Form::close() }} +
+
+
+@endif +@endsection +"; + $this->assertEquals($editFormViewContent, file_get_contents($editFormViewPath)); + } + +} diff --git a/tests/CommandOptions/SimpleCrudFormfieldOptionsTest.php b/tests/CommandOptions/SimpleCrudFormfieldOptionsTest.php new file mode 100644 index 0000000..afec724 --- /dev/null +++ b/tests/CommandOptions/SimpleCrudFormfieldOptionsTest.php @@ -0,0 +1,183 @@ +artisan('make:crud-simple', ['name' => $this->model_name, '--formfield' => 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_formfield() + { + $this->artisan('make:crud-simple', ['name' => $this->model_name, '--formfield' => 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') +

+
+ @can('create', new {$this->full_model_name}) + {{ link_to_route('{$this->table_name}.index', __('{$this->lang_name}.create'), ['action' => 'create'], ['class' => 'btn btn-success']) }} + @endcan +
+ {{ __('{$this->lang_name}.list') }} + {{ __('app.total') }} : {{ \${$this->collection_model_var_name}->total() }} {{ __('{$this->lang_name}.{$this->lang_name}') }} +

+
+
+
+
+ {{ Form::open(['method' => 'get', 'class' => 'form-inline']) }} + {!! FormField::text('q', ['label' => __('{$this->lang_name}.search'), 'placeholder' => __('{$this->lang_name}.search_text'), 'class' => 'input-sm']) !!} + {{ Form::submit(__('{$this->lang_name}.search'), ['class' => 'btn btn-sm']) }} + {{ link_to_route('{$this->table_name}.index', __('app.reset')) }} + {{ Form::close() }} +
+ + + + + + + + + + + @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}) + {{ link_to_route( + '{$this->table_name}.index', + __('app.edit'), + ['action' => 'edit', 'id' => \${$this->single_model_var_name}->id] + Request::only('page', 'q'), + ['id' => 'edit-{$this->lang_name}-'.\${$this->single_model_var_name}->id] + ) }} + @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_formfield() + { + $this->artisan('make:crud-simple', ['name' => $this->model_name, '--formfield' => 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}) + {{ Form::open(['route' => '{$this->table_name}.store']) }} + {!! FormField::text('name', ['required' => true, 'label' => __('{$this->lang_name}.name')]) !!} + {!! FormField::textarea('description', ['label' => __('{$this->lang_name}.description')]) !!} + {{ Form::submit(__('{$this->lang_name}.create'), ['class' => 'btn btn-success']) }} + {{ link_to_route('{$this->table_name}.index', __('app.cancel'), [], ['class' => 'btn btn-default']) }} + {{ Form::close() }} +@endcan +@endif +@if (Request::get('action') == 'edit' && \$editable{$this->model_name}) +@can('update', \$editable{$this->model_name}) + {{ Form::model(\$editable{$this->model_name}, ['route' => ['{$this->table_name}.update', \$editable{$this->model_name}], 'method' => 'patch']) }} + {!! FormField::text('name', ['required' => true, 'label' => __('{$this->lang_name}.name')]) !!} + {!! FormField::textarea('description', ['label' => __('{$this->lang_name}.description')]) !!} + @if (request('q')) + {{ Form::hidden('q', request('q')) }} + @endif + @if (request('page')) + {{ Form::hidden('page', request('page')) }} + @endif + {{ Form::submit(__('{$this->lang_name}.update'), ['class' => 'btn btn-success']) }} + {{ link_to_route('{$this->table_name}.index', __('app.cancel'), Request::only('page', 'q'), ['class' => 'btn btn-default']) }} + @can('delete', \$editable{$this->model_name}) + {{ link_to_route( + '{$this->table_name}.index', + __('app.delete'), + ['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'] + ) }} + @endcan + {{ Form::close() }} +@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') }}
+
+ {!! FormField::delete( + ['route' => ['{$this->table_name}.destroy', \$editable{$this->model_name}]], + __('app.delete_confirm_button'), + ['class'=>'btn btn-danger'], + [ + '{$this->lang_name}_id' => \$editable{$this->model_name}->id, + 'page' => request('page'), + 'q' => request('q'), + ] + ) !!} + {{ link_to_route('{$this->table_name}.index', __('app.cancel'), Request::only('page', 'q'), ['class' => 'btn btn-default']) }} +
+
+@endcan +@endif +"; + $this->assertEquals($formViewContent, file_get_contents($formViewPath)); + } +}