From 149b211446511df7aff009ef9d416dcae10a0e85 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 11 Feb 2018 11:11:33 +0800 Subject: [PATCH] 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 --- src/CrudSimpleMake.php | 113 ++++++++ src/Generators/BaseGenerator.php | 11 +- src/Generators/ControllerGenerator.php | 4 +- src/Generators/FeatureTestGenerator.php | 2 +- src/Generators/FormViewGenerator.php | 2 +- src/Generators/IndexViewGenerator.php | 2 +- src/Generators/LangFileGenerator.php | 2 +- src/Generators/MigrationGenerator.php | 2 +- src/Generators/ModelFactoryGenerator.php | 2 +- src/Generators/ModelGenerator.php | 2 +- src/Generators/ModelPolicyGenerator.php | 2 +- src/Generators/ModelPolicyTestGenerator.php | 2 +- src/Generators/ModelTestGenerator.php | 2 +- src/Generators/WebRouteGenerator.php | 2 +- src/ServiceProvider.php | 5 +- src/stubs/controller.full.stub | 126 ++++++++ tests/CrudSimpleMakeCommandTest.php | 156 ++++++++++ tests/Generators/ControllerGeneratorTest.php | 96 ++++++ .../Generators/Simple/ControllerGeneratorTest.php | 321 +++++++++++++++++++++ 19 files changed, 834 insertions(+), 20 deletions(-) create mode 100644 src/CrudSimpleMake.php create mode 100644 src/stubs/controller.full.stub create mode 100644 tests/CrudSimpleMakeCommandTest.php create mode 100644 tests/Generators/Simple/ControllerGeneratorTest.php diff --git a/src/CrudSimpleMake.php b/src/CrudSimpleMake.php new file mode 100644 index 0000000..f86fbf5 --- /dev/null +++ b/src/CrudSimpleMake.php @@ -0,0 +1,113 @@ +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(); + } +} diff --git a/src/Generators/BaseGenerator.php b/src/Generators/BaseGenerator.php index ca9cd77..b456709 100644 --- a/src/Generators/BaseGenerator.php +++ b/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 diff --git a/src/Generators/ControllerGenerator.php b/src/Generators/ControllerGenerator.php index c411546..7940311 100644 --- a/src/Generators/ControllerGenerator.php +++ b/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.'); } diff --git a/src/Generators/FeatureTestGenerator.php b/src/Generators/FeatureTestGenerator.php index 14a41a9..3f8e105 100644 --- a/src/Generators/FeatureTestGenerator.php +++ b/src/Generators/FeatureTestGenerator.php @@ -10,7 +10,7 @@ class FeatureTestGenerator extends BaseGenerator /** * {@inheritDoc} */ - public function generate() + public function generate(string $type = 'full') { $this->createBrowserKitBaseTestClass(); diff --git a/src/Generators/FormViewGenerator.php b/src/Generators/FormViewGenerator.php index 4b9bd4a..776e2d7 100644 --- a/src/Generators/FormViewGenerator.php +++ b/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'])); diff --git a/src/Generators/IndexViewGenerator.php b/src/Generators/IndexViewGenerator.php index 0fafa2f..c5e0c25 100644 --- a/src/Generators/IndexViewGenerator.php +++ b/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'])); diff --git a/src/Generators/LangFileGenerator.php b/src/Generators/LangFileGenerator.php index aba5254..0443b88 100644 --- a/src/Generators/LangFileGenerator.php +++ b/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)); diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 56b3d0c..0a7d705 100644 --- a/src/Generators/MigrationGenerator.php +++ b/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']; diff --git a/src/Generators/ModelFactoryGenerator.php b/src/Generators/ModelFactoryGenerator.php index 8962616..1d5871f 100644 --- a/src/Generators/ModelFactoryGenerator.php +++ b/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')); diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 7292b5b..aa538f1 100644 --- a/src/Generators/ModelGenerator.php +++ b/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)); diff --git a/src/Generators/ModelPolicyGenerator.php b/src/Generators/ModelPolicyGenerator.php index 3d09a57..10ced33 100644 --- a/src/Generators/ModelPolicyGenerator.php +++ b/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'))) { diff --git a/src/Generators/ModelPolicyTestGenerator.php b/src/Generators/ModelPolicyTestGenerator.php index aa22dd7..ca316c3 100644 --- a/src/Generators/ModelPolicyTestGenerator.php +++ b/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')); diff --git a/src/Generators/ModelTestGenerator.php b/src/Generators/ModelTestGenerator.php index 70a7199..4391329 100644 --- a/src/Generators/ModelTestGenerator.php +++ b/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')); diff --git a/src/Generators/WebRouteGenerator.php b/src/Generators/WebRouteGenerator.php index 57c69fc..eaf0396 100644 --- a/src/Generators/WebRouteGenerator.php +++ b/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'); diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 3478546..9b4689e 100644 --- a/src/ServiceProvider.php +++ b/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'); } } diff --git a/src/stubs/controller.full.stub b/src/stubs/controller.full.stub new file mode 100644 index 0000000..03d32b1 --- /dev/null +++ b/src/stubs/controller.full.stub @@ -0,0 +1,126 @@ +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(); + } +} diff --git a/tests/CrudSimpleMakeCommandTest.php b/tests/CrudSimpleMakeCommandTest.php new file mode 100644 index 0000000..1c69903 --- /dev/null +++ b/tests/CrudSimpleMakeCommandTest.php @@ -0,0 +1,156 @@ +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")); + } +} diff --git a/tests/Generators/ControllerGeneratorTest.php b/tests/Generators/ControllerGeneratorTest.php index 31c9004..eaefe94 100644 --- a/tests/Generators/ControllerGeneratorTest.php +++ b/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 diff --git a/tests/Generators/Simple/ControllerGeneratorTest.php b/tests/Generators/Simple/ControllerGeneratorTest.php new file mode 100644 index 0000000..71b74d9 --- /dev/null +++ b/tests/Generators/Simple/ControllerGeneratorTest.php @@ -0,0 +1,321 @@ +artisan('make:crud-simple', ['name' => $this->model_name, '--no-interaction' => true]); + + $this->assertFileExists(app_path("Http/Controllers/{$this->plural_model_name}Controller.php")); + $ctrlClassContent = "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 = "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 = "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"))); + } +}