Browse Source

Add --parent option for structuring controller path

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
8eba854b9f
  1. 37
      src/CrudMake.php
  2. 29
      tests/CrudMakeCommandTest.php
  3. 98
      tests/Generators/ControllerGeneratorTest.php
  4. 14
      tests/Generators/RouteWebGeneratorTest.php

37
src/CrudMake.php

@ -55,7 +55,7 @@ class CrudMake extends Command
*
* @var string
*/
protected $signature = 'make:crud {name}';
protected $signature = 'make:crud {name} {--parent=}';
/**
* The console command description.
@ -162,7 +162,11 @@ class CrudMake extends Command
*/
public function generateController()
{
$controllerPath = $this->makeDirectory(app_path('Http/Controllers'));
$parentControllerDirectory = '';
if (! is_null($this->option('parent'))) {
$parentControllerDirectory = '/'.$this->option('parent');
}
$controllerPath = $this->makeDirectory(app_path('Http/Controllers'.$parentControllerDirectory));
$controllerPath = $controllerPath.'/'.$this->modelNames['plural_model_name'].'Controller.php';
$this->generateFile($controllerPath, $this->getControllerContent());
@ -316,7 +320,18 @@ class CrudMake extends Command
public function getControllerContent()
{
$stub = $this->files->get(__DIR__.'/stubs/controller.model.stub');
return $this->replaceStubString($stub);
$controllerFileContent = $this->replaceStubString($stub);
if (! is_null($parentName = $this->option('parent'))) {
$controllerFileContent = str_replace(
'App\Http\Controllers',
'App\Http\Controllers\\'.$parentName,
$controllerFileContent
);
}
return $controllerFileContent;
}
/**
@ -434,7 +449,21 @@ class CrudMake extends Command
public function getWebRouteContent()
{
$stub = $this->files->get(__DIR__.'/stubs/route-web.stub');
return $this->replaceStubString($stub);
$webRouteFileContent = $this->replaceStubString($stub);
if (! is_null($parentName = $this->option('parent'))) {
$pluralModelName = $this->modelNames['plural_model_name'];
$webRouteFileContent = str_replace(
$pluralModelName.'Controller',
$parentName.'/'.$pluralModelName.'Controller',
$webRouteFileContent
);
}
return $webRouteFileContent;
}
/**

29
tests/CrudMakeCommandTest.php

@ -78,4 +78,33 @@ class CrudMakeCommandTest extends TestCase
$this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}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->assertNotRegExp("/{$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"));
$this->assertFileExists(resource_path("lang/en/{$langName}.php"));
$this->assertFileExists(database_path("factories/{$modelName}Factory.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php"));
$this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php"));
}
}

98
tests/Generators/ControllerGeneratorTest.php

@ -201,4 +201,102 @@ class CategoriesController extends Controller
";
$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', ['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 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->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->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->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")));
}
}

14
tests/Generators/RouteWebGeneratorTest.php

@ -19,4 +19,18 @@ Route::apiResource('{$this->table_name}', '{$this->plural_model_name}Controller'
";
$this->assertEquals($routeWebFileContent, file_get_contents($routeWebPath));
}
/** @test */
public function it_creates_correct_web_route_content_with_parent_command_option()
{
$this->artisan('make:crud', ['name' => $this->model_name, '--parent' => 'Projects', '--no-interaction' => true]);
$routeWebPath = base_path('routes/web.php');
$this->assertFileExists($routeWebPath);
$routeWebFileContent = "<?php
Route::apiResource('{$this->table_name}', 'Projects/{$this->plural_model_name}Controller');
";
$this->assertEquals($routeWebFileContent, file_get_contents($routeWebPath));
}
}
Loading…
Cancel
Save