Browse Source

Add create and update form request stubs

tags/1.2.9
Nafies Luthfi 7 years ago
parent
commit
725e371589
  1. 12
      src/CrudMake.php
  2. 47
      src/Generators/FormRequestGenerator.php
  3. 45
      src/stubs/requests/create-request.stub
  4. 31
      src/stubs/requests/update-request.stub
  5. 131
      tests/CommandOptions/FullCrudFormRequestOptionsTest.php

12
src/CrudMake.php

@ -56,6 +56,10 @@ class CrudMake extends GeneratorCommand
$this->generateResources();
$this->generateTestFiles();
if ($this->option('form-requests')) {
$this->generateRequestClasses();
}
$this->info('CRUD files generated successfully!');
}
@ -116,4 +120,12 @@ class CrudMake extends GeneratorCommand
app('Luthfi\CrudGenerator\Generators\IndexViewGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\ShowViewGenerator', ['command' => $this])->generate();
}
/**
* Generate Form Requests
*/
public function generateRequestClasses()
{
app('Luthfi\CrudGenerator\Generators\FormRequestGenerator', ['command' => $this])->generate();
}
}

47
src/Generators/FormRequestGenerator.php

@ -0,0 +1,47 @@
<?php
namespace Luthfi\CrudGenerator\Generators;
/**
* Form Request Generator Class
*/
class FormRequestGenerator extends BaseGenerator
{
/**
* {@inheritDoc}
*/
public function generate(string $type = 'full')
{
$modelName = $this->modelNames['model_name'];
$pluralModelName = $this->modelNames['plural_model_name'];
$requestPath = $this->makeDirectory(app_path('Http/Requests/'.$pluralModelName));
$createRequestPath = $requestPath.'/CreateRequest.php';
$this->generateFile($createRequestPath, $this->getContent('requests/create-request'));
$updateRequestPath = $requestPath.'/UpdateRequest.php';
$this->generateFile($updateRequestPath, $this->getContent('requests/update-request'));
$this->command->info($modelName.' Form Requests generated.');
}
/**
* {@inheritDoc}
*/
public function getContent(string $stubName)
{
$stub = $this->getStubFileContent($stubName);
$controllerFileContent = $this->replaceStubString($stub);
$appNamespace = $this->getAppNamespace();
$controllerFileContent = str_replace(
"App\Http\Controllers",
"{$appNamespace}Http\Controllers",
$controllerFileContent
);
return $controllerFileContent;
}
}

45
src/stubs/requests/create-request.stub

@ -0,0 +1,45 @@
<?php
namespace App\Http\Requests\Masters;
use fullMstr;
use Illuminate\Foundation\Http\FormRequest;
class CreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return $this->user()->can('create', new Master);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
];
}
/**
* Save proposal to database.
*
* @return \fullMstr
*/
public function save()
{
$newMaster = $this->validated();
$newMaster['creator_id'] = auth()->id();
return Master::create($newMaster);
}
}

31
src/stubs/requests/update-request.stub

@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests\Masters;
use Illuminate\Foundation\Http\FormRequest;
class UpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return $this->user()->can('update', $this->route('master'));
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
];
}
}

131
tests/CommandOptions/FullCrudFormRequestOptionsTest.php

@ -3,10 +3,41 @@
namespace Tests\CommandOptions;
use Tests\TestCase;
use Illuminate\Contracts\Console\Kernel;
class FullCrudFormRequestOptionsTest extends TestCase
{
/** @test */
public function it_can_generate_form_request_classes()
{
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => 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"));
$this->assertFileExists(app_path("Http/Requests/{$this->plural_model_name}/CreateRequest.php"));
$this->assertFileExists(app_path("Http/Requests/{$this->plural_model_name}/UpdateRequest.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_can_generate_controller_file_with_form_requests_class()
{
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => true]);
@ -124,4 +155,104 @@ class {$this->model_name}Controller extends Controller
";
$this->assertEquals($ctrlClassContent, file_get_contents(app_path("Http/Controllers/{$this->model_name}Controller.php")));
}
/** @test */
public function it_generates_correct_create_form_request_file_content()
{
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => true]);
$classFilePath = app_path("Http/Requests/{$this->plural_model_name}/CreateRequest.php");
$this->assertFileExists($classFilePath);
$formRequestClassContent = "<?php
namespace App\Http\Requests\\{$this->plural_model_name};
use {$this->full_model_name};
use Illuminate\Foundation\Http\FormRequest;
class CreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return \$this->user()->can('create', new {$this->model_name});
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
];
}
/**
* Save proposal to database.
*
* @return \\{$this->full_model_name}
*/
public function save()
{
\$new{$this->model_name} = \$this->validated();
\$new{$this->model_name}['creator_id'] = auth()->id();
return {$this->model_name}::create(\$new{$this->model_name});
}
}
";
$this->assertEquals($formRequestClassContent, file_get_contents($classFilePath));
}
/** @test */
public function it_generates_correct_update_form_request_file_content()
{
$this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => true]);
$classFilePath = app_path("Http/Requests/{$this->plural_model_name}/UpdateRequest.php");
$this->assertFileExists($classFilePath);
$formRequestClassContent = "<?php
namespace App\Http\Requests\\{$this->plural_model_name};
use Illuminate\Foundation\Http\FormRequest;
class UpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return \$this->user()->can('update', \$this->route('{$this->lang_name}'));
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
];
}
}
";
$this->assertEquals($formRequestClassContent, file_get_contents($classFilePath));
}
}
Loading…
Cancel
Save