From 5469b4f164c6153cffe22dc94f8534b4322d0496 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 15 Oct 2017 11:02:21 +0800 Subject: [PATCH] Update model names --- src/CrudMake.php | 8 +-- tests/CrudMakeCommandTest.php | 68 +++++++++++++------- tests/Generators/ControllerGeneratorTest.php | 48 +++++++------- tests/Generators/FeatureTestGeneratorTest.php | 88 +++++++++++++------------- tests/Generators/LangGeneratorTest.php | 40 ++++++------ tests/Generators/MigrationGeneratorTest.php | 10 +-- tests/Generators/ModelFactoryGeneratorTest.php | 8 +-- tests/Generators/ModelGeneratorTest.php | 6 +- tests/Generators/ModelTestGeneratorTest.php | 14 ++-- tests/Generators/RouteWebGeneratorTest.php | 4 +- tests/Generators/ViewsGeneratorTest.php | 80 +++++++++++------------ tests/TestCase.php | 26 ++++---- 12 files changed, 214 insertions(+), 186 deletions(-) diff --git a/src/CrudMake.php b/src/CrudMake.php index 76b2baf..e480a11 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -27,7 +27,7 @@ class CrudMake extends Command * * @var array */ - private $stubModelNames = ['Masters', 'Master', 'masters', 'master']; + public $stubModelNames = ['Masters', 'Master', 'masters', 'master']; /** * Construct CrudMake class @@ -85,11 +85,11 @@ class CrudMake extends Command * * @return void */ - public function getModelName() + public function getModelName($modelName = null) { - $modelName = $this->argument('name'); + $modelName = is_null($modelName) ? $this->argument('name') : $modelName; - $this->modelNames = [ + return $this->modelNames = [ 'plural_model_name' => str_plural($modelName), 'model_name' => $modelName, 'lowercase_plural_model_name' => strtolower(str_plural($modelName)), diff --git a/tests/CrudMakeCommandTest.php b/tests/CrudMakeCommandTest.php index 81ffc1e..bcdc2f4 100644 --- a/tests/CrudMakeCommandTest.php +++ b/tests/CrudMakeCommandTest.php @@ -3,49 +3,73 @@ namespace Tests; use Illuminate\Contracts\Console\Kernel; +use Luthfi\CrudGenerator\CrudMake; class CrudMakeCommandTest extends TestCase { /** @test */ + public function it_has_stub_model_names_property() + { + $crudMaker = app(CrudMake::class); + + $this->assertEquals([ + 'Masters', 'Master', 'masters', 'master', + ], $crudMaker->stubModelNames); + } + + /** @test */ + public function it_has_model_names_property() + { + $crudMaker = app(CrudMake::class); + + $this->assertEquals([ + 'plural_model_name' => 'Categories', + 'model_name' => 'Category', + 'lowercase_plural_model_name' => 'categories', + 'lowercase_single_model_name' => 'category', + ], $crudMaker->getModelName('Category')); + } + + /** @test */ public function it_can_generate_simple_crud_files() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $this->assertFileExists(app_path($this->modelName.'.php')); - $this->assertFileExists(app_path("Http/Controllers/{$this->pluralModelName}Controller.php")); + $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->tableName.'_table.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->tableName}/index.blade.php")); - $this->assertFileExists(resource_path("views/{$this->tableName}/forms.blade.php")); - $this->assertFileExists(resource_path("lang/en/{$this->singleModelName}.php")); - $this->assertFileExists(database_path("factories/{$this->modelName}Factory.php")); + $this->assertFileExists(resource_path("views/{$this->table_name}/index.blade.php")); + $this->assertFileExists(resource_path("views/{$this->table_name}/forms.blade.php")); + $this->assertFileExists(resource_path("lang/en/{$this->single_model_var_name}.php")); + $this->assertFileExists(database_path("factories/{$this->model_name}Factory.php")); $this->assertFileExists(base_path("routes/web.php")); - $this->assertFileExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php")); - $this->assertFileExists(base_path("tests/Unit/Models/{$this->modelName}Test.php")); + $this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); + $this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); } /** @test */ public function it_cannot_generate_crud_files_if_model_exists() { - $this->artisan('make:model', ['name' => $this->modelName, '--no-interaction' => true]); - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:model', ['name' => $this->model_name, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $this->assertRegExp("/{$this->modelName} model already exists./", app(Kernel::class)->output()); + $this->assertRegExp("/{$this->model_name} model already exists./", app(Kernel::class)->output()); - $this->assertFileExists(app_path($this->modelName.'.php')); - $this->assertFileNotExists(app_path("Http/Controllers/{$this->pluralModelName}Controller.php")); + $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->tableName.'_table.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->tableName}/index.blade.php")); - $this->assertFileNotExists(resource_path("views/{$this->tableName}/forms.blade.php")); - $this->assertFileNotExists(resource_path("lang/en/{$this->singleModelName}.php")); - $this->assertFileNotExists(database_path("factories/{$this->modelName}Factory.php")); + $this->assertFileNotExists(resource_path("views/{$this->table_name}/index.blade.php")); + $this->assertFileNotExists(resource_path("views/{$this->table_name}/forms.blade.php")); + $this->assertFileNotExists(resource_path("lang/en/{$this->single_model_var_name}.php")); + $this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php")); $this->assertFileNotExists(base_path("routes/web.php")); - $this->assertFileNotExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php")); - $this->assertFileNotExists(base_path("tests/Unit/Models/{$this->modelName}Test.php")); + $this->assertFileNotExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); + $this->assertFileNotExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); } } diff --git a/tests/Generators/ControllerGeneratorTest.php b/tests/Generators/ControllerGeneratorTest.php index 9350a90..3cbc953 100644 --- a/tests/Generators/ControllerGeneratorTest.php +++ b/tests/Generators/ControllerGeneratorTest.php @@ -9,39 +9,39 @@ class ControllerGeneratorTest extends TestCase /** @test */ public function it_creates_correct_controller_class_content() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $this->assertFileExists(app_path("Http/Controllers/{$this->pluralModelName}Controller.php")); + $this->assertFileExists(app_path("Http/Controllers/{$this->plural_model_name}Controller.php")); $ctrlClassContent = "modelName}; +use App\\{$this->model_name}; use Illuminate\Http\Request; -class {$this->pluralModelName}Controller extends Controller +class {$this->plural_model_name}Controller extends Controller { /** - * Display a listing of the {$this->singleModelName}. + * Display a listing of the {$this->single_model_var_name}. * * @return \Illuminate\Http\Response */ public function index() { - \$editable{$this->modelName} = null; - \${$this->tableName} = {$this->modelName}::where(function (\$query) { + \$editable{$this->model_name} = null; + \${$this->table_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->modelName} = {$this->modelName}::find(request('id')); + \$editable{$this->model_name} = {$this->model_name}::find(request('id')); } - return view('{$this->tableName}.index', compact('{$this->tableName}', 'editable{$this->modelName}')); + return view('{$this->table_name}.index', compact('{$this->table_name}', 'editable{$this->model_name}')); } /** - * Store a newly created {$this->singleModelName} in storage. + * Store a newly created {$this->single_model_var_name} in storage. * * @param \Illuminate\Http\Request \$request * @return \Illuminate\Http\Response @@ -53,19 +53,19 @@ class {$this->pluralModelName}Controller extends Controller 'description' => 'nullable|max:255', ]); - {$this->modelName}::create(\$request->only('name', 'description')); + {$this->model_name}::create(\$request->only('name', 'description')); - return redirect()->route('{$this->tableName}.index'); + return redirect()->route('{$this->table_name}.index'); } /** - * Update the specified {$this->singleModelName} in storage. + * Update the specified {$this->single_model_var_name} in storage. * * @param \Illuminate\Http\Request \$request - * @param \App\\{$this->modelName} \${$this->singleModelName} + * @param \App\\{$this->model_name} \${$this->single_model_var_name} * @return \Illuminate\Http\Response */ - public function update(Request \$request, {$this->modelName} \${$this->singleModelName}) + public function update(Request \$request, {$this->model_name} \${$this->single_model_var_name}) { \$this->validate(\$request, [ 'name' => 'required|max:60', @@ -74,33 +74,33 @@ class {$this->pluralModelName}Controller extends Controller \$routeParam = request()->only('page', 'q'); - \${$this->singleModelName} = \${$this->singleModelName}->update(\$request->only('name', 'description')); + \${$this->single_model_var_name} = \${$this->single_model_var_name}->update(\$request->only('name', 'description')); - return redirect()->route('{$this->tableName}.index', \$routeParam); + return redirect()->route('{$this->table_name}.index', \$routeParam); } /** - * Remove the specified {$this->singleModelName} from storage. + * Remove the specified {$this->single_model_var_name} from storage. * - * @param \App\\{$this->modelName} \${$this->singleModelName} + * @param \App\\{$this->model_name} \${$this->single_model_var_name} * @return \Illuminate\Http\Response */ - public function destroy({$this->modelName} \${$this->singleModelName}) + public function destroy({$this->model_name} \${$this->single_model_var_name}) { \$this->validate(request(), [ - '{$this->singleModelName}_id' => 'required', + '{$this->single_model_var_name}_id' => 'required', ]); \$routeParam = request()->only('page', 'q'); - if (request('{$this->singleModelName}_id') == \${$this->singleModelName}->id && \${$this->singleModelName}->delete()) { - return redirect()->route('{$this->tableName}.index', \$routeParam); + if (request('{$this->single_model_var_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->pluralModelName}Controller.php"))); + $this->assertEquals($ctrlClassContent, file_get_contents(app_path("Http/Controllers/{$this->plural_model_name}Controller.php"))); } } diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index c05b7e2..bbb6b26 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -9,7 +9,7 @@ class FeatureTestGeneratorTest extends TestCase /** @test */ public function it_creates_browser_kit_base_test_class() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); $this->assertFileExists(base_path("tests/BrowserKitTest.php")); $browserKitTestClassContent = "artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $this->assertFileExists(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php")); + $this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); $modelClassContent = "modelName}; +use App\\{$this->model_name}; use Tests\BrowserKitTest as TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; -class Manage{$this->pluralModelName}Test extends TestCase +class Manage{$this->plural_model_name}Test extends TestCase { use DatabaseMigrations; /** @test */ - public function user_can_see_{$this->singleModelName}_list_in_{$this->singleModelName}_index_page() + public function user_can_see_{$this->single_model_var_name}_list_in_{$this->single_model_var_name}_index_page() { - \${$this->singleModelName}1 = factory({$this->modelName}::class)->create(['name' => 'Testing name', 'description' => 'Testing 123']); - \${$this->singleModelName}2 = factory({$this->modelName}::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']); + \${$this->single_model_var_name}1 = factory({$this->model_name}::class)->create(['name' => 'Testing name', 'description' => 'Testing 123']); + \${$this->single_model_var_name}2 = factory({$this->model_name}::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']); \$this->loginAsUser(); - \$this->visit(route('{$this->tableName}.index')); - \$this->see(\${$this->singleModelName}1->name); - \$this->see(\${$this->singleModelName}2->name); + \$this->visit(route('{$this->table_name}.index')); + \$this->see(\${$this->single_model_var_name}1->name); + \$this->see(\${$this->single_model_var_name}2->name); } /** @test */ - public function user_can_create_a_{$this->singleModelName}() + public function user_can_create_a_{$this->single_model_var_name}() { \$this->loginAsUser(); - \$this->visit(route('{$this->tableName}.index')); + \$this->visit(route('{$this->table_name}.index')); - \$this->click(trans('{$this->singleModelName}.create')); - \$this->seePageIs(route('{$this->tableName}.index', ['action' => 'create'])); + \$this->click(trans('{$this->single_model_var_name}.create')); + \$this->seePageIs(route('{$this->table_name}.index', ['action' => 'create'])); - \$this->type('{$this->modelName} 1 name', 'name'); - \$this->type('{$this->modelName} 1 description', 'description'); - \$this->press(trans('{$this->singleModelName}.create')); + \$this->type('{$this->model_name} 1 name', 'name'); + \$this->type('{$this->model_name} 1 description', 'description'); + \$this->press(trans('{$this->single_model_var_name}.create')); - \$this->seePageIs(route('{$this->tableName}.index')); + \$this->seePageIs(route('{$this->table_name}.index')); - \$this->seeInDatabase('{$this->tableName}', [ - 'name' => '{$this->modelName} 1 name', - 'description' => '{$this->modelName} 1 description', + \$this->seeInDatabase('{$this->table_name}', [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', ]); } /** @test */ - public function user_can_edit_a_{$this->singleModelName}_within_search_query() + public function user_can_edit_a_{$this->single_model_var_name}_within_search_query() { \$this->loginAsUser(); - \${$this->singleModelName} = factory({$this->modelName}::class)->create(['name' => 'Testing 123']); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); - \$this->visit(route('{$this->tableName}.index', ['q' => '123'])); - \$this->click('edit-{$this->singleModelName}-'.\${$this->singleModelName}->id); - \$this->seePageIs(route('{$this->tableName}.index', ['action' => 'edit', 'id' => \${$this->singleModelName}->id, 'q' => '123'])); + \$this->visit(route('{$this->table_name}.index', ['q' => '123'])); + \$this->click('edit-{$this->single_model_var_name}-'.\${$this->single_model_var_name}->id); + \$this->seePageIs(route('{$this->table_name}.index', ['action' => 'edit', 'id' => \${$this->single_model_var_name}->id, 'q' => '123'])); - \$this->type('{$this->modelName} 1 name', 'name'); - \$this->type('{$this->modelName} 1 description', 'description'); - \$this->press(trans('{$this->singleModelName}.update')); + \$this->type('{$this->model_name} 1 name', 'name'); + \$this->type('{$this->model_name} 1 description', 'description'); + \$this->press(trans('{$this->single_model_var_name}.update')); - \$this->seePageIs(route('{$this->tableName}.index', ['q' => '123'])); + \$this->seePageIs(route('{$this->table_name}.index', ['q' => '123'])); - \$this->seeInDatabase('{$this->tableName}', [ - 'name' => '{$this->modelName} 1 name', - 'description' => '{$this->modelName} 1 description', + \$this->seeInDatabase('{$this->table_name}', [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', ]); } /** @test */ - public function user_can_delete_a_{$this->singleModelName}() + public function user_can_delete_a_{$this->single_model_var_name}() { \$this->loginAsUser(); - \${$this->singleModelName} = factory({$this->modelName}::class)->create(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); - \$this->visit(route('{$this->tableName}.index', [\${$this->singleModelName}->id])); - \$this->click('del-{$this->singleModelName}-'.\${$this->singleModelName}->id); - \$this->seePageIs(route('{$this->tableName}.index', ['action' => 'delete', 'id' => \${$this->singleModelName}->id])); + \$this->visit(route('{$this->table_name}.index', [\${$this->single_model_var_name}->id])); + \$this->click('del-{$this->single_model_var_name}-'.\${$this->single_model_var_name}->id); + \$this->seePageIs(route('{$this->table_name}.index', ['action' => 'delete', 'id' => \${$this->single_model_var_name}->id])); - \$this->seeInDatabase('{$this->tableName}', [ - 'id' => \${$this->singleModelName}->id, + \$this->seeInDatabase('{$this->table_name}', [ + 'id' => \${$this->single_model_var_name}->id, ]); \$this->press(trans('app.delete_confirm_button')); - \$this->dontSeeInDatabase('{$this->tableName}', [ - 'id' => \${$this->singleModelName}->id, + \$this->dontSeeInDatabase('{$this->table_name}', [ + 'id' => \${$this->single_model_var_name}->id, ]); } } "; - $this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Feature/Manage{$this->pluralModelName}Test.php"))); + $this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php"))); } } diff --git a/tests/Generators/LangGeneratorTest.php b/tests/Generators/LangGeneratorTest.php index 31b11dc..d1771d6 100644 --- a/tests/Generators/LangGeneratorTest.php +++ b/tests/Generators/LangGeneratorTest.php @@ -9,36 +9,36 @@ class LangGeneratorTest extends TestCase /** @test */ public function it_creates_correct_model_lang_content() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $langPath = resource_path('lang/en/'.$this->singleModelName.'.php'); + $langPath = resource_path('lang/en/'.$this->single_model_var_name.'.php'); $this->assertFileExists($langPath); $langFileContent = "singleModelName}' => '{$this->modelName}', - 'list' => '{$this->modelName} List', - 'search' => 'Search {$this->modelName}', - 'not_found' => '{$this->modelName} not found.', - 'empty' => '{$this->modelName} is empty.', - 'back_to_show' => 'Back to {$this->modelName} Detail', - 'back_to_index' => 'Back to {$this->modelName} List', + '{$this->single_model_var_name}' => '{$this->model_name}', + 'list' => '{$this->model_name} List', + 'search' => 'Search {$this->model_name}', + 'not_found' => '{$this->model_name} not found.', + 'empty' => '{$this->model_name} is empty.', + 'back_to_show' => 'Back to {$this->model_name} Detail', + 'back_to_index' => 'Back to {$this->model_name} List', // Actions - 'create' => 'Create new {$this->modelName}', - 'created' => 'Create new {$this->modelName} succeded.', - 'edit' => 'Edit {$this->modelName}', - 'update' => 'Update {$this->modelName}', - 'updated' => 'Update {$this->modelName} succeded.', - 'delete' => 'Delete {$this->modelName}', - 'delete_confirm' => 'Are you sure to delete this {$this->modelName}?', - 'deleted' => 'Delete {$this->modelName} succeded.', - 'undeleted' => '{$this->modelName} not deleted.', + 'create' => 'Create new {$this->model_name}', + 'created' => 'Create new {$this->model_name} succeded.', + 'edit' => 'Edit {$this->model_name}', + 'update' => 'Update {$this->model_name}', + 'updated' => 'Update {$this->model_name} succeded.', + 'delete' => 'Delete {$this->model_name}', + 'delete_confirm' => 'Are you sure to delete this {$this->model_name}?', + 'deleted' => 'Delete {$this->model_name} succeded.', + 'undeleted' => '{$this->model_name} not deleted.', // Attributes - 'name' => '{$this->modelName} Name', - 'description' => '{$this->modelName} Description', + 'name' => '{$this->model_name} Name', + 'description' => '{$this->model_name} Description', ]; "; $this->assertEquals($langFileContent, file_get_contents($langPath)); diff --git a/tests/Generators/MigrationGeneratorTest.php b/tests/Generators/MigrationGeneratorTest.php index 1afebdb..2946ad1 100644 --- a/tests/Generators/MigrationGeneratorTest.php +++ b/tests/Generators/MigrationGeneratorTest.php @@ -9,9 +9,9 @@ class MigrationGeneratorTest extends TestCase /** @test */ public function it_creates_correct_migration_class_content() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->tableName.'_table.php'); + $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->table_name.'_table.php'); $this->assertFileExists($migrationFilePath); $modelClassContent = "pluralModelName}Table extends Migration +class Create{$this->plural_model_name}Table extends Migration { /** * Run the migrations. @@ -28,7 +28,7 @@ class Create{$this->pluralModelName}Table extends Migration */ public function up() { - Schema::create('{$this->tableName}', function (Blueprint \$table) { + Schema::create('{$this->table_name}', function (Blueprint \$table) { \$table->increments('id'); \$table->string('name', 60); \$table->string('description')->nullable(); @@ -43,7 +43,7 @@ class Create{$this->pluralModelName}Table extends Migration */ public function down() { - Schema::dropIfExists('{$this->tableName}'); + Schema::dropIfExists('{$this->table_name}'); } } "; diff --git a/tests/Generators/ModelFactoryGeneratorTest.php b/tests/Generators/ModelFactoryGeneratorTest.php index cce5e85..43ffbdd 100644 --- a/tests/Generators/ModelFactoryGeneratorTest.php +++ b/tests/Generators/ModelFactoryGeneratorTest.php @@ -9,16 +9,16 @@ class ModelFactoryGeneratorTest extends TestCase /** @test */ public function it_creates_correct_model_factory_content() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $modelFactoryPath = database_path('factories/'.$this->modelName.'Factory.php'); + $modelFactoryPath = database_path('factories/'.$this->model_name.'Factory.php'); $this->assertFileExists($modelFactoryPath); $modelFactoryContent = "modelName}; +use App\\{$this->model_name}; use Faker\Generator as Faker; -\$factory->define({$this->modelName}::class, function (Faker \$faker) { +\$factory->define({$this->model_name}::class, function (Faker \$faker) { return [ 'name' => \$faker->word, diff --git a/tests/Generators/ModelGeneratorTest.php b/tests/Generators/ModelGeneratorTest.php index 7fb9591..17559d7 100644 --- a/tests/Generators/ModelGeneratorTest.php +++ b/tests/Generators/ModelGeneratorTest.php @@ -9,9 +9,9 @@ class ModelGeneratorTest extends TestCase /** @test */ public function it_creates_correct_model_class_content() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $modelPath = app_path($this->modelName.'.php'); + $modelPath = app_path($this->model_name.'.php'); $this->assertFileExists($modelPath); $modelClassContent = "modelName} extends Model +class {$this->model_name} extends Model { protected \$fillable = ['name', 'description']; } diff --git a/tests/Generators/ModelTestGeneratorTest.php b/tests/Generators/ModelTestGeneratorTest.php index a42abd0..41f3ab4 100644 --- a/tests/Generators/ModelTestGeneratorTest.php +++ b/tests/Generators/ModelTestGeneratorTest.php @@ -9,29 +9,29 @@ class ModelTestGeneratorTest extends TestCase /** @test */ public function it_creates_correct_unit_test_class_content() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $this->assertFileExists(base_path("tests/Unit/Models/{$this->modelName}Test.php")); + $this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); $modelClassContent = "modelName}; +use App\\{$this->model_name}; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; -class {$this->modelName}Test extends TestCase +class {$this->model_name}Test extends TestCase { use DatabaseMigrations; /** @test */ public function it_has_name_attribute() { - \${$this->singleModelName} = factory({$this->modelName}::class)->create(['name' => '{$this->modelName} 1 name']); - \$this->assertEquals('{$this->modelName} 1 name', \${$this->singleModelName}->name); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']); + \$this->assertEquals('{$this->model_name} 1 name', \${$this->single_model_var_name}->name); } } "; - $this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Unit/Models/{$this->modelName}Test.php"))); + $this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Unit/Models/{$this->model_name}Test.php"))); } } diff --git a/tests/Generators/RouteWebGeneratorTest.php b/tests/Generators/RouteWebGeneratorTest.php index deef0e6..bc3d463 100644 --- a/tests/Generators/RouteWebGeneratorTest.php +++ b/tests/Generators/RouteWebGeneratorTest.php @@ -9,13 +9,13 @@ class RouteWebGeneratorTest extends TestCase /** @test */ public function it_creates_correct_web_route_content() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); $routeWebPath = base_path('routes/web.php'); $this->assertFileExists($routeWebPath); $routeWebFileContent = "tableName}', '{$this->pluralModelName}Controller'); +Route::apiResource('{$this->table_name}', '{$this->plural_model_name}Controller'); "; $this->assertEquals($routeWebFileContent, file_get_contents($routeWebPath)); } diff --git a/tests/Generators/ViewsGeneratorTest.php b/tests/Generators/ViewsGeneratorTest.php index 3ba83ca..587bab8 100644 --- a/tests/Generators/ViewsGeneratorTest.php +++ b/tests/Generators/ViewsGeneratorTest.php @@ -9,70 +9,70 @@ class ViewsGeneratorTest extends TestCase /** @test */ public function it_creates_correct_index_view_content() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $indexViewPath = resource_path("views/{$this->tableName}/index.blade.php"); + $indexViewPath = resource_path("views/{$this->table_name}/index.blade.php"); $this->assertFileExists($indexViewPath); $indexViewContent = "@extends('layouts.app') -@section('title', trans('{$this->singleModelName}.list')) +@section('title', trans('{$this->single_model_var_name}.list')) @section('content')
- {{ link_to_route('{$this->tableName}.index', trans('{$this->singleModelName}.create'), ['action' => 'create'], ['class' => 'btn btn-success']) }} + {{ link_to_route('{$this->table_name}.index', trans('{$this->single_model_var_name}.create'), ['action' => 'create'], ['class' => 'btn btn-success']) }}

- {{ trans('{$this->singleModelName}.list') }} - {{ trans('app.total') }} : {{ \${$this->tableName}->total() }} {{ trans('{$this->singleModelName}.{$this->singleModelName}') }} + {{ trans('{$this->single_model_var_name}.list') }} + {{ trans('app.total') }} : {{ \${$this->table_name}->total() }} {{ trans('{$this->single_model_var_name}.{$this->single_model_var_name}') }}

{{ Form::open(['method' => 'get','class' => 'form-inline']) }} - {!! FormField::text('q', ['value' => request('q'), 'label' => trans('{$this->singleModelName}.search'), 'class' => 'input-sm']) !!} - {{ Form::submit(trans('{$this->singleModelName}.search'), ['class' => 'btn btn-sm']) }} - {{ link_to_route('{$this->tableName}.index', trans('app.reset')) }} + {!! FormField::text('q', ['value' => request('q'), 'label' => trans('{$this->single_model_var_name}.search'), 'class' => 'input-sm']) !!} + {{ Form::submit(trans('{$this->single_model_var_name}.search'), ['class' => 'btn btn-sm']) }} + {{ link_to_route('{$this->table_name}.index', trans('app.reset')) }} {{ Form::close() }}
- - + + - @foreach(\${$this->tableName} as \$key => \${$this->singleModelName}) + @foreach(\${$this->table_name} as \$key => \${$this->single_model_var_name}) - - - + + + @endforeach
{{ trans('app.table_no') }}{{ trans('{$this->singleModelName}.name') }}{{ trans('{$this->singleModelName}.description') }}{{ trans('{$this->single_model_var_name}.name') }}{{ trans('{$this->single_model_var_name}.description') }} {{ trans('app.action') }}
{{ \${$this->tableName}->firstItem() + \$key }}{{ \${$this->singleModelName}->name }}{{ \${$this->singleModelName}->description }}{{ \${$this->table_name}->firstItem() + \$key }}{{ \${$this->single_model_var_name}->name }}{{ \${$this->single_model_var_name}->description }} {!! link_to_route( - '{$this->tableName}.index', + '{$this->table_name}.index', trans('app.edit'), - ['action' => 'edit', 'id' => \${$this->singleModelName}->id] + Request::only('page', 'q'), - ['id' => 'edit-{$this->singleModelName}-' . \${$this->singleModelName}->id] + ['action' => 'edit', 'id' => \${$this->single_model_var_name}->id] + Request::only('page', 'q'), + ['id' => 'edit-{$this->single_model_var_name}-' . \${$this->single_model_var_name}->id] ) !!} | {!! link_to_route( - '{$this->tableName}.index', + '{$this->table_name}.index', trans('app.delete'), - ['action' => 'delete', 'id' => \${$this->singleModelName}->id] + Request::only('page', 'q'), - ['id' => 'del-{$this->singleModelName}-' . \${$this->singleModelName}->id] + ['action' => 'delete', 'id' => \${$this->single_model_var_name}->id] + Request::only('page', 'q'), + ['id' => 'del-{$this->single_model_var_name}-' . \${$this->single_model_var_name}->id] ) !!}
-
{{ \${$this->tableName}->appends(Request::except('page'))->render() }}
+
{{ \${$this->table_name}->appends(Request::except('page'))->render() }}
- @includeWhen(Request::has('action'), '{$this->tableName}.forms') + @includeWhen(Request::has('action'), '{$this->table_name}.forms')
@endsection @@ -83,20 +83,20 @@ class ViewsGeneratorTest extends TestCase /** @test */ public function it_creates_correct_forms_view_content() { - $this->artisan('make:crud', ['name' => $this->modelName, '--no-interaction' => true]); + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $formViewPath = resource_path("views/{$this->tableName}/forms.blade.php"); + $formViewPath = resource_path("views/{$this->table_name}/forms.blade.php"); $this->assertFileExists($formViewPath); $formViewContent = "@if (Request::get('action') == 'create') - {!! Form::open(['route' => '{$this->tableName}.store']) !!} + {!! Form::open(['route' => '{$this->table_name}.store']) !!} {!! FormField::text('name', ['required' => true]) !!} {!! FormField::textarea('description') !!} - {!! Form::submit(trans('{$this->singleModelName}.create'), ['class' => 'btn btn-success']) !!} - {{ link_to_route('{$this->tableName}.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} + {!! Form::submit(trans('{$this->single_model_var_name}.create'), ['class' => 'btn btn-success']) !!} + {{ link_to_route('{$this->table_name}.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} {!! Form::close() !!} @endif -@if (Request::get('action') == 'edit' && \$editable{$this->modelName}) - {!! Form::model(\$editable{$this->modelName}, ['route' => ['{$this->tableName}.update', \$editable{$this->modelName}->id],'method' => 'patch']) !!} +@if (Request::get('action') == 'edit' && \$editable{$this->model_name}) + {!! Form::model(\$editable{$this->model_name}, ['route' => ['{$this->table_name}.update', \$editable{$this->model_name}->id],'method' => 'patch']) !!} {!! FormField::text('name', ['required' => true]) !!} {!! FormField::textarea('description') !!} @if (request('q')) @@ -105,32 +105,32 @@ class ViewsGeneratorTest extends TestCase @if (request('page')) {{ Form::hidden('page', request('page')) }} @endif - {!! Form::submit(trans('{$this->singleModelName}.update'), ['class' => 'btn btn-success']) !!} - {{ link_to_route('{$this->tableName}.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} + {!! Form::submit(trans('{$this->single_model_var_name}.update'), ['class' => 'btn btn-success']) !!} + {{ link_to_route('{$this->table_name}.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} {!! Form::close() !!} @endif -@if (Request::get('action') == 'delete' && \$editable{$this->modelName}) +@if (Request::get('action') == 'delete' && \$editable{$this->model_name})
-

{{ trans('{$this->singleModelName}.delete') }}

+

{{ trans('{$this->single_model_var_name}.delete') }}

- -

{{ \$editable{$this->modelName}->name }}

- {!! \$errors->first('{$this->singleModelName}_id', ':message') !!} + +

{{ \$editable{$this->model_name}->name }}

+ {!! \$errors->first('{$this->single_model_var_name}_id', ':message') !!}

{{ trans('app.delete_confirm') }}
{!! FormField::delete( - ['route'=>['{$this->tableName}.destroy',\$editable{$this->modelName}->id]], + ['route'=>['{$this->table_name}.destroy',\$editable{$this->model_name}->id]], trans('app.delete_confirm_button'), ['class'=>'btn btn-danger'], [ - '{$this->singleModelName}_id' => \$editable{$this->modelName}->id, + '{$this->single_model_var_name}_id' => \$editable{$this->model_name}->id, 'page' => request('page'), 'q' => request('q'), ] ) !!} - {{ link_to_route('{$this->tableName}.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} + {{ link_to_route('{$this->table_name}.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }}
@endif diff --git a/tests/TestCase.php b/tests/TestCase.php index 72886d9..e04ead1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,19 +6,23 @@ use Orchestra\Testbench\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { - protected $modelName; - protected $pluralModelName; - protected $tableName; - protected $singleModelName; + protected $model_name; + protected $plural_model_name; + protected $table_name; + protected $lang_name; + protected $collection_model_var_name; + protected $single_model_var_name; public function setUp() { parent::setUp(); - $this->modelName = 'Category'; + $this->model_name = 'Category'; - $this->pluralModelName = str_plural($this->modelName); - $this->tableName = strtolower($this->pluralModelName); - $this->singleModelName = strtolower($this->modelName); + $this->plural_model_name = str_plural($this->model_name); + $this->table_name = snake_case($this->plural_model_name); + $this->lang_name = snake_case($this->model_name); + $this->collection_model_var_name = camel_case($this->plural_model_name); + $this->single_model_var_name = camel_case($this->model_name); } public function tearDown() @@ -30,12 +34,12 @@ abstract class TestCase extends BaseTestCase protected function cleanUpGeneratedFiles() { - $this->removeFileOrDir(app_path($this->modelName.'.php')); + $this->removeFileOrDir(app_path($this->model_name.'.php')); $this->removeFileOrDir(app_path('Http')); $this->removeFileOrDir(database_path('migrations')); $this->removeFileOrDir(database_path('factories')); - $this->removeFileOrDir(resource_path('views/'.$this->tableName)); - $this->removeFileOrDir(resource_path("lang/en/{$this->singleModelName}.php")); + $this->removeFileOrDir(resource_path('views/'.$this->table_name)); + $this->removeFileOrDir(resource_path("lang/en/{$this->single_model_var_name}.php")); $this->removeFileOrDir(base_path('routes')); $this->removeFileOrDir(base_path('tests/BrowserKitTest.php')); $this->removeFileOrDir(base_path('tests/Feature'));