Browse Source

Add make:crud-simple command with separated tests

Add crud type on BaseGenerator@generate method
Add controller.full.stub for full controller
Update ControllerGeneratorTest to adopt controller.full.stub
tags/1.0.0
Nafies Luthfi 8 years ago
parent
commit
149b211446
  1. 113
      src/CrudSimpleMake.php
  2. 11
      src/Generators/BaseGenerator.php
  3. 4
      src/Generators/ControllerGenerator.php
  4. 2
      src/Generators/FeatureTestGenerator.php
  5. 2
      src/Generators/FormViewGenerator.php
  6. 2
      src/Generators/IndexViewGenerator.php
  7. 2
      src/Generators/LangFileGenerator.php
  8. 2
      src/Generators/MigrationGenerator.php
  9. 2
      src/Generators/ModelFactoryGenerator.php
  10. 2
      src/Generators/ModelGenerator.php
  11. 2
      src/Generators/ModelPolicyGenerator.php
  12. 2
      src/Generators/ModelPolicyTestGenerator.php
  13. 2
      src/Generators/ModelTestGenerator.php
  14. 2
      src/Generators/WebRouteGenerator.php
  15. 5
      src/ServiceProvider.php
  16. 126
      src/stubs/controller.full.stub
  17. 156
      tests/CrudSimpleMakeCommandTest.php
  18. 96
      tests/Generators/ControllerGeneratorTest.php
  19. 321
      tests/Generators/Simple/ControllerGeneratorTest.php

113
src/CrudSimpleMake.php

@ -0,0 +1,113 @@
<?php
namespace Luthfi\CrudGenerator;
class CrudSimpleMake extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:crud-simple {name} {--p|parent=} {--t|tests-only}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create simple Laravel CRUD files of given model name.';
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->getModelName();
if ($this->modelExists()) {
$this->error("{$this->modelNames['model_name']} model already exists.");
return;
}
// Warn if it has no default layout view based on
// simple-crud.default_layout_view config
if ($this->defaultLayoutNotExists()) {
$this->warn(config('simple-crud.default_layout_view').' view does not exists.');
}
if ($this->option('tests-only')) {
$this->generateTestFiles();
$this->info('Test files generated successfully!');
return;
}
$this->generateRoutes();
$this->generateModel();
$this->generateController();
$this->generateResources();
$this->generateTestFiles();
$this->info('CRUD files generated successfully!');
}
/**
* Generate test files
*
* @return void
*/
public function generateTestFiles()
{
app('Luthfi\CrudGenerator\Generators\ModelTestGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\FeatureTestGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\ModelPolicyTestGenerator', ['command' => $this])->generate();
}
/**
* Generate Controller
*
* @return void
*/
public function generateController()
{
app('Luthfi\CrudGenerator\Generators\ControllerGenerator', ['command' => $this])->generate('simple');
}
/**
* Generate Model
*
* @return void
*/
public function generateModel()
{
app('Luthfi\CrudGenerator\Generators\ModelGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\MigrationGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\ModelPolicyGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\ModelFactoryGenerator', ['command' => $this])->generate();
}
/**
* Generate Route Route
*
* @return void
*/
public function generateRoutes()
{
app('Luthfi\CrudGenerator\Generators\WebRouteGenerator', ['command' => $this])->generate();
}
/**
* Generate Resources
*
* @return void
*/
public function generateResources()
{
app('Luthfi\CrudGenerator\Generators\LangFileGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\FormViewGenerator', ['command' => $this])->generate();
app('Luthfi\CrudGenerator\Generators\IndexViewGenerator', ['command' => $this])->generate();
}
}

11
src/Generators/BaseGenerator.php

@ -4,7 +4,7 @@ namespace Luthfi\CrudGenerator\Generators;
use Illuminate\Console\DetectsApplicationNamespace;
use Illuminate\Filesystem\Filesystem;
use Luthfi\CrudGenerator\CrudMake;
use Luthfi\CrudGenerator\GeneratorCommand;
/**
* Base Generator Class
@ -35,13 +35,13 @@ abstract class BaseGenerator
protected $stubModelNames;
/**
* The CrudMake class
* The Generator Command implementation class
*
* @var CrudMake
* @var GeneratorCommand
*/
protected $command;
public function __construct(Filesystem $files, CrudMake $command)
public function __construct(Filesystem $files, GeneratorCommand $command)
{
$this->files = $files;
@ -73,9 +73,10 @@ abstract class BaseGenerator
/**
* Generate class file content
*
* @param string $type Type of crud
* @return void
*/
abstract public function generate();
abstract public function generate(string $type = 'full');
/**
* Get class file content

4
src/Generators/ControllerGenerator.php

@ -10,7 +10,7 @@ class ControllerGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$parentControllerDirectory = '';
if (!is_null($this->command->option('parent'))) {
@ -19,7 +19,7 @@ class ControllerGenerator extends BaseGenerator
$controllerPath = $this->makeDirectory(app_path('Http/Controllers'.$parentControllerDirectory));
$controllerPath = $controllerPath.'/'.$this->modelNames['plural_model_name'].'Controller.php';
$this->generateFile($controllerPath, $this->getContent('controller.simple'));
$this->generateFile($controllerPath, $this->getContent('controller.'.$type));
$this->command->info($this->modelNames['plural_model_name'].'Controller generated.');
}

2
src/Generators/FeatureTestGenerator.php

@ -10,7 +10,7 @@ class FeatureTestGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$this->createBrowserKitBaseTestClass();

2
src/Generators/FormViewGenerator.php

@ -10,7 +10,7 @@ class FormViewGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$viewPath = $this->makeDirectory(resource_path('views/'.$this->modelNames['table_name']));

2
src/Generators/IndexViewGenerator.php

@ -10,7 +10,7 @@ class IndexViewGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$viewPath = $this->makeDirectory(resource_path('views/'.$this->modelNames['table_name']));

2
src/Generators/LangFileGenerator.php

@ -10,7 +10,7 @@ class LangFileGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$locale = config('app.locale');
$langPath = $this->makeDirectory(resource_path('lang/'.$locale));

2
src/Generators/MigrationGenerator.php

@ -10,7 +10,7 @@ class MigrationGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$prefix = date('Y_m_d_His');
$tableName = $this->modelNames['table_name'];

2
src/Generators/ModelFactoryGenerator.php

@ -10,7 +10,7 @@ class ModelFactoryGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$modelFactoryPath = $this->makeDirectory(database_path('factories'));

2
src/Generators/ModelGenerator.php

@ -10,7 +10,7 @@ class ModelGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$modelPath = $this->modelNames['model_path'];
$modelDirectory = $this->makeDirectory(app_path($modelPath));

2
src/Generators/ModelPolicyGenerator.php

@ -10,7 +10,7 @@ class ModelPolicyGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$parentDirectory = '';
if (!is_null($this->command->option('parent'))) {

2
src/Generators/ModelPolicyTestGenerator.php

@ -10,7 +10,7 @@ class ModelPolicyTestGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$modelPolicyTestPath = $this->makeDirectory(base_path('tests/Unit/Policies'));

2
src/Generators/ModelTestGenerator.php

@ -10,7 +10,7 @@ class ModelTestGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$unitTestPath = $this->makeDirectory(base_path('tests/Unit/Models'));

2
src/Generators/WebRouteGenerator.php

@ -10,7 +10,7 @@ class WebRouteGenerator extends BaseGenerator
/**
* {@inheritDoc}
*/
public function generate()
public function generate(string $type = 'full')
{
$webRoutePath = $this->makeRouteFile(base_path('routes'), 'web.php');

5
src/ServiceProvider.php

@ -14,16 +14,17 @@ class ServiceProvider extends BaseServiceProvider
if ($this->app->runningInConsole()) {
$this->commands([
CrudMake::class,
CrudSimpleMake::class,
]);
}
$this->mergeConfigFrom(__DIR__ . '/config.php', 'simple-crud');
$this->mergeConfigFrom(__DIR__.'/config.php', 'simple-crud');
}
public function boot()
{
$this->publishes([
__DIR__ . '/config.php' => config_path('simple-crud.php'),
__DIR__.'/config.php' => config_path('simple-crud.php'),
], 'config');
}
}

126
src/stubs/controller.full.stub

@ -0,0 +1,126 @@
<?php
namespace App\Http\Controllers;
use fullMstr;
use Illuminate\Http\Request;
class MastersController extends Controller
{
/**
* Display a listing of the singleMstr.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$editableMaster = null;
$mstrCollections = Master::where(function ($query) {
$query->where('name', 'like', '%'.request('q').'%');
})->paginate(25);
if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) {
$editableMaster = Master::find(request('id'));
}
return view('masters.index', compact('mstrCollections', 'editableMaster'));
}
/**
* Show the form for creating a new singleMstr.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('masters.create');
}
/**
* Store a newly created singleMstr in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->authorize('create', new Master);
$this->validate($request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
Master::create($request->only('name', 'description'));
return redirect()->route('masters.index');
}
/**
* Display the specified singleMstr.
*
* @param \App\Master $singleMstr
* @return \Illuminate\Http\Response
*/
public function show(Master $singleMstr)
{
return view('masters.show', compact('singleMstr'));
}
/**
* Show the form for editing the specified singleMstr.
*
* @param \App\Master $singleMstr
* @return \Illuminate\Http\Response
*/
public function edit(Master $singleMstr)
{
return view('masters.edit', compact('singleMstr'));
}
/**
* Update the specified singleMstr in storage.
*
* @param \Illuminate\Http\Request $request
* @param \fullMstr $singleMstr
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Master $singleMstr)
{
$this->authorize('update', $singleMstr);
$this->validate($request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
$routeParam = request()->only('page', 'q');
$singleMstr = $singleMstr->update($request->only('name', 'description'));
return redirect()->route('masters.index', $routeParam);
}
/**
* Remove the specified singleMstr from storage.
*
* @param \fullMstr $singleMstr
* @return \Illuminate\Http\Response
*/
public function destroy(Master $singleMstr)
{
$this->authorize('delete', $singleMstr);
$this->validate(request(), [
'master_id' => 'required',
]);
$routeParam = request()->only('page', 'q');
if (request('master_id') == $singleMstr->id && $singleMstr->delete()) {
return redirect()->route('masters.index', $routeParam);
}
return back();
}
}

156
tests/CrudSimpleMakeCommandTest.php

@ -0,0 +1,156 @@
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
class CrudSimpleCommandTest extends TestCase
{
/** @test */
public function it_can_generate_simple_crud_files()
{
$this->artisan('make:crud-simple', ['name' => $this->model_name, '--no-interaction' => 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->plural_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->plural_model_name}Test.php"));
}
/** @test */
public function it_cannot_generate_crud_files_if_model_exists()
{
$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->assertFileExists(app_path($this->model_name.'.php'));
$this->assertFileNotExists(app_path("Http/Controllers/{$this->plural_model_name}Controller.php"));
$migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->table_name.'_table.php');
$this->assertFileNotExists($migrationFilePath);
$this->assertFileNotExists(resource_path("views/{$this->table_name}/index.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->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/Feature/Manage{$this->plural_model_name}Test.php"));
}
/** @test */
public function it_cannot_generate_crud_files_if_namespaced_model_exists()
{
$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->assertFileExists(app_path('Entities/Projects/Problem.php'));
$this->assertFileNotExists(app_path("Http/Controllers/ProblemsController.php"));
$migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_problems_table.php');
$this->assertFileNotExists($migrationFilePath);
$this->assertFileNotExists(resource_path("views/problems/index.blade.php"));
$this->assertFileNotExists(resource_path("views/problems/forms.blade.php"));
$localeConfig = config('app.locale');
$this->assertFileNotExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php"));
$this->assertFileNotExists(app_path("Policies/ProblemPolicy.php"));
$this->assertFileNotExists(database_path("factories/ProblemFactory.php"));
$this->assertFileNotExists(base_path("tests/Unit/Models/ProblemTest.php"));
$this->assertFileNotExists(base_path("tests/Feature/ManageProblemsTest.php"));
$this->removeFileOrDir(app_path('Entities/Projects'));
$this->removeFileOrDir(resource_path('views/problems'));
$this->removeFileOrDir(resource_path("lang/en/problem.php"));
}
/** @test */
public function it_can_generate_crud_files_for_namespaced_model()
{
$inputName = 'Entities/References/Category';
$modelName = 'Category';
$pluralModelName = 'Categories';
$tableName = 'categories';
$langName = 'category';
$modelPath = 'Entities/References';
$this->artisan('make:crud', ['name' => $inputName, '--no-interaction' => true]);
$this->assertNotContains("{$modelName} model already exists.", app(Kernel::class)->output());
$this->assertFileExists(app_path($modelPath.'/'.$modelName.'.php'));
$this->assertFileExists(app_path("Http/Controllers/{$pluralModelName}Controller.php"));
$migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$tableName.'_table.php');
$this->assertFileExists($migrationFilePath);
$this->assertFileExists(resource_path("views/{$tableName}/index.blade.php"));
$this->assertFileExists(resource_path("views/{$tableName}/forms.blade.php"));
$localeConfig = config('app.locale');
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));
$this->assertFileExists(app_path("Policies/{$modelName}Policy.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php"));
}
/** @test */
public function it_can_generate_crud_files_with_parent_option()
{
$inputName = 'Entities/References/Category';
$modelName = 'Category';
$parentName = 'Projects';
$pluralModelName = 'Categories';
$tableName = 'categories';
$langName = 'category';
$modelPath = 'Entities/References';
$this->artisan('make:crud', ['name' => $inputName, '--parent' => $parentName, '--no-interaction' => true]);
$this->assertNotContains("{$modelName} model already exists.", app(Kernel::class)->output());
$this->assertFileExists(app_path($modelPath.'/'.$modelName.'.php'));
$this->assertFileExists(app_path("Http/Controllers/{$parentName}/{$pluralModelName}Controller.php"));
$migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$tableName.'_table.php');
$this->assertFileExists($migrationFilePath);
$this->assertFileExists(resource_path("views/{$tableName}/index.blade.php"));
$this->assertFileExists(resource_path("views/{$tableName}/forms.blade.php"));
$localeConfig = config('app.locale');
$this->assertFileExists(resource_path("lang/{$localeConfig}/{$langName}.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
$this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php"));
}
}

96
tests/Generators/ControllerGeneratorTest.php

@ -41,6 +41,16 @@ class {$this->plural_model_name}Controller extends Controller
}
/**
* Show the form for creating a new {$this->single_model_var_name}.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('{$this->table_name}.create');
}
/**
* Store a newly created {$this->single_model_var_name} in storage.
*
* @param \Illuminate\Http\Request \$request
@ -61,6 +71,28 @@ class {$this->plural_model_name}Controller extends Controller
}
/**
* Display the specified {$this->single_model_var_name}.
*
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return \Illuminate\Http\Response
*/
public function show({$this->model_name} \${$this->single_model_var_name})
{
return view('{$this->table_name}.show', compact('{$this->single_model_var_name}'));
}
/**
* Show the form for editing the specified {$this->single_model_var_name}.
*
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return \Illuminate\Http\Response
*/
public function edit({$this->model_name} \${$this->single_model_var_name})
{
return view('{$this->table_name}.edit', compact('{$this->single_model_var_name}'));
}
/**
* Update the specified {$this->single_model_var_name} in storage.
*
* @param \Illuminate\Http\Request \$request
@ -145,6 +177,16 @@ class CategoriesController extends Controller
}
/**
* Show the form for creating a new category.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('categories.create');
}
/**
* Store a newly created category in storage.
*
* @param \Illuminate\Http\Request \$request
@ -165,6 +207,28 @@ class CategoriesController extends Controller
}
/**
* Display the specified category.
*
* @param \App\Category \$category
* @return \Illuminate\Http\Response
*/
public function show(Category \$category)
{
return view('categories.show', compact('category'));
}
/**
* Show the form for editing the specified category.
*
* @param \App\Category \$category
* @return \Illuminate\Http\Response
*/
public function edit(Category \$category)
{
return view('categories.edit', compact('category'));
}
/**
* Update the specified category in storage.
*
* @param \Illuminate\Http\Request \$request
@ -250,6 +314,16 @@ class CategoriesController extends Controller
}
/**
* Show the form for creating a new category.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('categories.create');
}
/**
* Store a newly created category in storage.
*
* @param \Illuminate\Http\Request \$request
@ -270,6 +344,28 @@ class CategoriesController extends Controller
}
/**
* Display the specified category.
*
* @param \App\Category \$category
* @return \Illuminate\Http\Response
*/
public function show(Category \$category)
{
return view('categories.show', compact('category'));
}
/**
* Show the form for editing the specified category.
*
* @param \App\Category \$category
* @return \Illuminate\Http\Response
*/
public function edit(Category \$category)
{
return view('categories.edit', compact('category'));
}
/**
* Update the specified category in storage.
*
* @param \Illuminate\Http\Request \$request

321
tests/Generators/Simple/ControllerGeneratorTest.php

@ -0,0 +1,321 @@
<?php
namespace Tests\Generators\Simple;
use Tests\TestCase;
class ControllerGeneratorTest extends TestCase
{
/** @test */
public function it_creates_correct_controller_class_content()
{
$this->artisan('make:crud-simple', ['name' => $this->model_name, '--no-interaction' => true]);
$this->assertFileExists(app_path("Http/Controllers/{$this->plural_model_name}Controller.php"));
$ctrlClassContent = "<?php
namespace App\Http\Controllers;
use {$this->full_model_name};
use Illuminate\Http\Request;
class {$this->plural_model_name}Controller extends Controller
{
/**
* Display a listing of the {$this->single_model_var_name}.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
\$editable{$this->model_name} = null;
\${$this->collection_model_var_name} = {$this->model_name}::where(function (\$query) {
\$query->where('name', 'like', '%'.request('q').'%');
})->paginate(25);
if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) {
\$editable{$this->model_name} = {$this->model_name}::find(request('id'));
}
return view('{$this->table_name}.index', compact('{$this->collection_model_var_name}', 'editable{$this->model_name}'));
}
/**
* Store a newly created {$this->single_model_var_name} in storage.
*
* @param \Illuminate\Http\Request \$request
* @return \Illuminate\Http\Response
*/
public function store(Request \$request)
{
\$this->authorize('create', new {$this->model_name});
\$this->validate(\$request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
{$this->model_name}::create(\$request->only('name', 'description'));
return redirect()->route('{$this->table_name}.index');
}
/**
* Update the specified {$this->single_model_var_name} in storage.
*
* @param \Illuminate\Http\Request \$request
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return \Illuminate\Http\Response
*/
public function update(Request \$request, {$this->model_name} \${$this->single_model_var_name})
{
\$this->authorize('update', \${$this->single_model_var_name});
\$this->validate(\$request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
\$routeParam = request()->only('page', 'q');
\${$this->single_model_var_name} = \${$this->single_model_var_name}->update(\$request->only('name', 'description'));
return redirect()->route('{$this->table_name}.index', \$routeParam);
}
/**
* Remove the specified {$this->single_model_var_name} from storage.
*
* @param \\{$this->full_model_name} \${$this->single_model_var_name}
* @return \Illuminate\Http\Response
*/
public function destroy({$this->model_name} \${$this->single_model_var_name})
{
\$this->authorize('delete', \${$this->single_model_var_name});
\$this->validate(request(), [
'{$this->lang_name}_id' => 'required',
]);
\$routeParam = request()->only('page', 'q');
if (request('{$this->lang_name}_id') == \${$this->single_model_var_name}->id && \${$this->single_model_var_name}->delete()) {
return redirect()->route('{$this->table_name}.index', \$routeParam);
}
return back();
}
}
";
$this->assertEquals($ctrlClassContent, file_get_contents(app_path("Http/Controllers/{$this->plural_model_name}Controller.php")));
}
/** @test */
public function it_creates_correct_controller_class_content_for_namespaced_model()
{
$this->artisan('make:crud-simple', ['name' => 'Entities/References/Category', '--no-interaction' => true]);
$this->assertFileExists(app_path("Http/Controllers/CategoriesController.php"));
$ctrlClassContent = "<?php
namespace App\Http\Controllers;
use App\Entities\References\Category;
use Illuminate\Http\Request;
class CategoriesController extends Controller
{
/**
* Display a listing of the category.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
\$editableCategory = null;
\$categories = Category::where(function (\$query) {
\$query->where('name', 'like', '%'.request('q').'%');
})->paginate(25);
if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) {
\$editableCategory = Category::find(request('id'));
}
return view('categories.index', compact('categories', 'editableCategory'));
}
/**
* Store a newly created category in storage.
*
* @param \Illuminate\Http\Request \$request
* @return \Illuminate\Http\Response
*/
public function store(Request \$request)
{
\$this->authorize('create', new Category);
\$this->validate(\$request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
Category::create(\$request->only('name', 'description'));
return redirect()->route('categories.index');
}
/**
* Update the specified category in storage.
*
* @param \Illuminate\Http\Request \$request
* @param \App\Entities\References\Category \$category
* @return \Illuminate\Http\Response
*/
public function update(Request \$request, Category \$category)
{
\$this->authorize('update', \$category);
\$this->validate(\$request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
\$routeParam = request()->only('page', 'q');
\$category = \$category->update(\$request->only('name', 'description'));
return redirect()->route('categories.index', \$routeParam);
}
/**
* Remove the specified category from storage.
*
* @param \App\Entities\References\Category \$category
* @return \Illuminate\Http\Response
*/
public function destroy(Category \$category)
{
\$this->authorize('delete', \$category);
\$this->validate(request(), [
'category_id' => 'required',
]);
\$routeParam = request()->only('page', 'q');
if (request('category_id') == \$category->id && \$category->delete()) {
return redirect()->route('categories.index', \$routeParam);
}
return back();
}
}
";
$this->assertEquals($ctrlClassContent, file_get_contents(app_path("Http/Controllers/CategoriesController.php")));
}
/** @test */
public function it_creates_correct_controller_with_parent()
{
$this->artisan('make:crud-simple', ['name' => 'Entities/References/Category', '--parent' => 'Projects', '--no-interaction' => true]);
$this->assertFileExists(app_path("Http/Controllers/Projects/CategoriesController.php"));
$ctrlClassContent = "<?php
namespace App\Http\Controllers\Projects;
use App\Entities\References\Category;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class CategoriesController extends Controller
{
/**
* Display a listing of the category.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
\$editableCategory = null;
\$categories = Category::where(function (\$query) {
\$query->where('name', 'like', '%'.request('q').'%');
})->paginate(25);
if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) {
\$editableCategory = Category::find(request('id'));
}
return view('categories.index', compact('categories', 'editableCategory'));
}
/**
* Store a newly created category in storage.
*
* @param \Illuminate\Http\Request \$request
* @return \Illuminate\Http\Response
*/
public function store(Request \$request)
{
\$this->authorize('create', new Category);
\$this->validate(\$request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
Category::create(\$request->only('name', 'description'));
return redirect()->route('categories.index');
}
/**
* Update the specified category in storage.
*
* @param \Illuminate\Http\Request \$request
* @param \App\Entities\References\Category \$category
* @return \Illuminate\Http\Response
*/
public function update(Request \$request, Category \$category)
{
\$this->authorize('update', \$category);
\$this->validate(\$request, [
'name' => 'required|max:60',
'description' => 'nullable|max:255',
]);
\$routeParam = request()->only('page', 'q');
\$category = \$category->update(\$request->only('name', 'description'));
return redirect()->route('categories.index', \$routeParam);
}
/**
* Remove the specified category from storage.
*
* @param \App\Entities\References\Category \$category
* @return \Illuminate\Http\Response
*/
public function destroy(Category \$category)
{
\$this->authorize('delete', \$category);
\$this->validate(request(), [
'category_id' => 'required',
]);
\$routeParam = request()->only('page', 'q');
if (request('category_id') == \$category->id && \$category->delete()) {
return redirect()->route('categories.index', \$routeParam);
}
return back();
}
}
";
$this->assertEquals($ctrlClassContent, file_get_contents(app_path("Http/Controllers/Projects/CategoriesController.php")));
}
}
Loading…
Cancel
Save