Browse Source

Update model names

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
5469b4f164
  1. 8
      src/CrudMake.php
  2. 68
      tests/CrudMakeCommandTest.php
  3. 48
      tests/Generators/ControllerGeneratorTest.php
  4. 88
      tests/Generators/FeatureTestGeneratorTest.php
  5. 40
      tests/Generators/LangGeneratorTest.php
  6. 10
      tests/Generators/MigrationGeneratorTest.php
  7. 8
      tests/Generators/ModelFactoryGeneratorTest.php
  8. 6
      tests/Generators/ModelGeneratorTest.php
  9. 14
      tests/Generators/ModelTestGeneratorTest.php
  10. 4
      tests/Generators/RouteWebGeneratorTest.php
  11. 80
      tests/Generators/ViewsGeneratorTest.php
  12. 26
      tests/TestCase.php

8
src/CrudMake.php

@ -27,7 +27,7 @@ class CrudMake extends Command
* *
* @var array * @var array
*/ */
private $stubModelNames = ['Masters', 'Master', 'masters', 'master'];
public $stubModelNames = ['Masters', 'Master', 'masters', 'master'];
/** /**
* Construct CrudMake class * Construct CrudMake class
@ -85,11 +85,11 @@ class CrudMake extends Command
* *
* @return void * @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), 'plural_model_name' => str_plural($modelName),
'model_name' => $modelName, 'model_name' => $modelName,
'lowercase_plural_model_name' => strtolower(str_plural($modelName)), 'lowercase_plural_model_name' => strtolower(str_plural($modelName)),

68
tests/CrudMakeCommandTest.php

@ -3,49 +3,73 @@
namespace Tests; namespace Tests;
use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Console\Kernel;
use Luthfi\CrudGenerator\CrudMake;
class CrudMakeCommandTest extends TestCase class CrudMakeCommandTest extends TestCase
{ {
/** @test */ /** @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() 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($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("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 */ /** @test */
public function it_cannot_generate_crud_files_if_model_exists() 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($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("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"));
} }
} }

48
tests/Generators/ControllerGeneratorTest.php

@ -9,39 +9,39 @@ class ControllerGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_correct_controller_class_content() 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 = "<?php $ctrlClassContent = "<?php
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\\{$this->modelName};
use App\\{$this->model_name};
use Illuminate\Http\Request; 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 * @return \Illuminate\Http\Response
*/ */
public function index() 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').'%'); \$query->where('name', 'like', '%'.request('q').'%');
})->paginate(25); })->paginate(25);
if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) { 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 * @param \Illuminate\Http\Request \$request
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
@ -53,19 +53,19 @@ class {$this->pluralModelName}Controller extends Controller
'description' => 'nullable|max:255', '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 \Illuminate\Http\Request \$request
* @param \App\\{$this->modelName} \${$this->singleModelName}
* @param \App\\{$this->model_name} \${$this->single_model_var_name}
* @return \Illuminate\Http\Response * @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, [ \$this->validate(\$request, [
'name' => 'required|max:60', 'name' => 'required|max:60',
@ -74,33 +74,33 @@ class {$this->pluralModelName}Controller extends Controller
\$routeParam = request()->only('page', 'q'); \$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 * @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->validate(request(), [
'{$this->singleModelName}_id' => 'required',
'{$this->single_model_var_name}_id' => 'required',
]); ]);
\$routeParam = request()->only('page', 'q'); \$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(); 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")));
} }
} }

88
tests/Generators/FeatureTestGeneratorTest.php

@ -9,7 +9,7 @@ class FeatureTestGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_browser_kit_base_test_class() 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")); $this->assertFileExists(base_path("tests/BrowserKitTest.php"));
$browserKitTestClassContent = "<?php $browserKitTestClassContent = "<?php
@ -46,98 +46,98 @@ abstract class BrowserKitTest extends BaseTestCase
/** @test */ /** @test */
public function it_creates_correct_feature_test_class_content() public function it_creates_correct_feature_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/Feature/Manage{$this->pluralModelName}Test.php"));
$this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php"));
$modelClassContent = "<?php $modelClassContent = "<?php
namespace Tests\Feature; namespace Tests\Feature;
use App\\{$this->modelName};
use App\\{$this->model_name};
use Tests\BrowserKitTest as TestCase; use Tests\BrowserKitTest as TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
class Manage{$this->pluralModelName}Test extends TestCase
class Manage{$this->plural_model_name}Test extends TestCase
{ {
use DatabaseMigrations; use DatabaseMigrations;
/** @test */ /** @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->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 */ /** @test */
public function user_can_create_a_{$this->singleModelName}()
public function user_can_create_a_{$this->single_model_var_name}()
{ {
\$this->loginAsUser(); \$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 */ /** @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->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 */ /** @test */
public function user_can_delete_a_{$this->singleModelName}()
public function user_can_delete_a_{$this->single_model_var_name}()
{ {
\$this->loginAsUser(); \$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->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")));
} }
} }

40
tests/Generators/LangGeneratorTest.php

@ -9,36 +9,36 @@ class LangGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_correct_model_lang_content() 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); $this->assertFileExists($langPath);
$langFileContent = "<?php $langFileContent = "<?php
return [ return [
// Labels // Labels
'{$this->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 // 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 // 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)); $this->assertEquals($langFileContent, file_get_contents($langPath));

10
tests/Generators/MigrationGeneratorTest.php

@ -9,9 +9,9 @@ class MigrationGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_correct_migration_class_content() 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); $this->assertFileExists($migrationFilePath);
$modelClassContent = "<?php $modelClassContent = "<?php
@ -19,7 +19,7 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
class Create{$this->pluralModelName}Table extends Migration
class Create{$this->plural_model_name}Table extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
@ -28,7 +28,7 @@ class Create{$this->pluralModelName}Table extends Migration
*/ */
public function up() public function up()
{ {
Schema::create('{$this->tableName}', function (Blueprint \$table) {
Schema::create('{$this->table_name}', function (Blueprint \$table) {
\$table->increments('id'); \$table->increments('id');
\$table->string('name', 60); \$table->string('name', 60);
\$table->string('description')->nullable(); \$table->string('description')->nullable();
@ -43,7 +43,7 @@ class Create{$this->pluralModelName}Table extends Migration
*/ */
public function down() public function down()
{ {
Schema::dropIfExists('{$this->tableName}');
Schema::dropIfExists('{$this->table_name}');
} }
} }
"; ";

8
tests/Generators/ModelFactoryGeneratorTest.php

@ -9,16 +9,16 @@ class ModelFactoryGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_correct_model_factory_content() 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); $this->assertFileExists($modelFactoryPath);
$modelFactoryContent = "<?php $modelFactoryContent = "<?php
use App\\{$this->modelName};
use App\\{$this->model_name};
use Faker\Generator as Faker; use Faker\Generator as Faker;
\$factory->define({$this->modelName}::class, function (Faker \$faker) {
\$factory->define({$this->model_name}::class, function (Faker \$faker) {
return [ return [
'name' => \$faker->word, 'name' => \$faker->word,

6
tests/Generators/ModelGeneratorTest.php

@ -9,9 +9,9 @@ class ModelGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_correct_model_class_content() 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); $this->assertFileExists($modelPath);
$modelClassContent = "<?php $modelClassContent = "<?php
@ -19,7 +19,7 @@ namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class {$this->modelName} extends Model
class {$this->model_name} extends Model
{ {
protected \$fillable = ['name', 'description']; protected \$fillable = ['name', 'description'];
} }

14
tests/Generators/ModelTestGeneratorTest.php

@ -9,29 +9,29 @@ class ModelTestGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_correct_unit_test_class_content() 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 = "<?php $modelClassContent = "<?php
namespace Tests\Unit\Models; namespace Tests\Unit\Models;
use App\\{$this->modelName};
use App\\{$this->model_name};
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase; use Tests\TestCase;
class {$this->modelName}Test extends TestCase
class {$this->model_name}Test extends TestCase
{ {
use DatabaseMigrations; use DatabaseMigrations;
/** @test */ /** @test */
public function it_has_name_attribute() 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")));
} }
} }

4
tests/Generators/RouteWebGeneratorTest.php

@ -9,13 +9,13 @@ class RouteWebGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_correct_web_route_content() 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'); $routeWebPath = base_path('routes/web.php');
$this->assertFileExists($routeWebPath); $this->assertFileExists($routeWebPath);
$routeWebFileContent = "<?php $routeWebFileContent = "<?php
Route::apiResource('{$this->tableName}', '{$this->pluralModelName}Controller');
Route::apiResource('{$this->table_name}', '{$this->plural_model_name}Controller');
"; ";
$this->assertEquals($routeWebFileContent, file_get_contents($routeWebPath)); $this->assertEquals($routeWebFileContent, file_get_contents($routeWebPath));
} }

80
tests/Generators/ViewsGeneratorTest.php

@ -9,70 +9,70 @@ class ViewsGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_correct_index_view_content() 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); $this->assertFileExists($indexViewPath);
$indexViewContent = "@extends('layouts.app') $indexViewContent = "@extends('layouts.app')
@section('title', trans('{$this->singleModelName}.list'))
@section('title', trans('{$this->single_model_var_name}.list'))
@section('content') @section('content')
<div class=\"pull-right\"> <div class=\"pull-right\">
{{ 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']) }}
</div> </div>
<h3 class=\"page-header\"> <h3 class=\"page-header\">
{{ trans('{$this->singleModelName}.list') }}
<small>{{ trans('app.total') }} : {{ \${$this->tableName}->total() }} {{ trans('{$this->singleModelName}.{$this->singleModelName}') }}</small>
{{ trans('{$this->single_model_var_name}.list') }}
<small>{{ trans('app.total') }} : {{ \${$this->table_name}->total() }} {{ trans('{$this->single_model_var_name}.{$this->single_model_var_name}') }}</small>
</h3> </h3>
<div class=\"row\"> <div class=\"row\">
<div class=\"col-md-8\"> <div class=\"col-md-8\">
<div class=\"panel panel-default table-responsive\"> <div class=\"panel panel-default table-responsive\">
<div class=\"panel-heading\"> <div class=\"panel-heading\">
{{ Form::open(['method' => 'get','class' => 'form-inline']) }} {{ 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() }} {{ Form::close() }}
</div> </div>
<table class=\"table table-condensed\"> <table class=\"table table-condensed\">
<thead> <thead>
<tr> <tr>
<th class=\"text-center\">{{ trans('app.table_no') }}</th> <th class=\"text-center\">{{ trans('app.table_no') }}</th>
<th>{{ trans('{$this->singleModelName}.name') }}</th>
<th>{{ trans('{$this->singleModelName}.description') }}</th>
<th>{{ trans('{$this->single_model_var_name}.name') }}</th>
<th>{{ trans('{$this->single_model_var_name}.description') }}</th>
<th class=\"text-center\">{{ trans('app.action') }}</th> <th class=\"text-center\">{{ trans('app.action') }}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach(\${$this->tableName} as \$key => \${$this->singleModelName})
@foreach(\${$this->table_name} as \$key => \${$this->single_model_var_name})
<tr> <tr>
<td class=\"text-center\">{{ \${$this->tableName}->firstItem() + \$key }}</td>
<td>{{ \${$this->singleModelName}->name }}</td>
<td>{{ \${$this->singleModelName}->description }}</td>
<td class=\"text-center\">{{ \${$this->table_name}->firstItem() + \$key }}</td>
<td>{{ \${$this->single_model_var_name}->name }}</td>
<td>{{ \${$this->single_model_var_name}->description }}</td>
<td class=\"text-center\"> <td class=\"text-center\">
{!! link_to_route( {!! link_to_route(
'{$this->tableName}.index',
'{$this->table_name}.index',
trans('app.edit'), 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( {!! link_to_route(
'{$this->tableName}.index',
'{$this->table_name}.index',
trans('app.delete'), 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]
) !!} ) !!}
</td> </td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
<div class=\"panel-body\">{{ \${$this->tableName}->appends(Request::except('page'))->render() }}</div>
<div class=\"panel-body\">{{ \${$this->table_name}->appends(Request::except('page'))->render() }}</div>
</div> </div>
</div> </div>
<div class=\"col-md-4\"> <div class=\"col-md-4\">
@includeWhen(Request::has('action'), '{$this->tableName}.forms')
@includeWhen(Request::has('action'), '{$this->table_name}.forms')
</div> </div>
</div> </div>
@endsection @endsection
@ -83,20 +83,20 @@ class ViewsGeneratorTest extends TestCase
/** @test */ /** @test */
public function it_creates_correct_forms_view_content() 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); $this->assertFileExists($formViewPath);
$formViewContent = "@if (Request::get('action') == 'create') $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::text('name', ['required' => true]) !!}
{!! FormField::textarea('description') !!} {!! 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() !!} {!! Form::close() !!}
@endif @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::text('name', ['required' => true]) !!}
{!! FormField::textarea('description') !!} {!! FormField::textarea('description') !!}
@if (request('q')) @if (request('q'))
@ -105,32 +105,32 @@ class ViewsGeneratorTest extends TestCase
@if (request('page')) @if (request('page'))
{{ Form::hidden('page', request('page')) }} {{ Form::hidden('page', request('page')) }}
@endif @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() !!} {!! Form::close() !!}
@endif @endif
@if (Request::get('action') == 'delete' && \$editable{$this->modelName})
@if (Request::get('action') == 'delete' && \$editable{$this->model_name})
<div class=\"panel panel-default\"> <div class=\"panel panel-default\">
<div class=\"panel-heading\"><h3 class=\"panel-title\">{{ trans('{$this->singleModelName}.delete') }}</h3></div>
<div class=\"panel-heading\"><h3 class=\"panel-title\">{{ trans('{$this->single_model_var_name}.delete') }}</h3></div>
<div class=\"panel-body\"> <div class=\"panel-body\">
<label class=\"control-label\">{{ trans('{$this->singleModelName}.name') }}</label>
<p>{{ \$editable{$this->modelName}->name }}</p>
{!! \$errors->first('{$this->singleModelName}_id', '<span class=\"form-error small\">:message</span>') !!}
<label class=\"control-label\">{{ trans('{$this->single_model_var_name}.name') }}</label>
<p>{{ \$editable{$this->model_name}->name }}</p>
{!! \$errors->first('{$this->single_model_var_name}_id', '<span class=\"form-error small\">:message</span>') !!}
</div> </div>
<hr style=\"margin:0\"> <hr style=\"margin:0\">
<div class=\"panel-body\">{{ trans('app.delete_confirm') }}</div> <div class=\"panel-body\">{{ trans('app.delete_confirm') }}</div>
<div class=\"panel-footer\"> <div class=\"panel-footer\">
{!! FormField::delete( {!! 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'), trans('app.delete_confirm_button'),
['class'=>'btn btn-danger'], ['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'), 'page' => request('page'),
'q' => request('q'), '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']) }}
</div> </div>
</div> </div>
@endif @endif

26
tests/TestCase.php

@ -6,19 +6,23 @@ use Orchestra\Testbench\TestCase as BaseTestCase;
abstract class TestCase extends 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() public function setUp()
{ {
parent::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() public function tearDown()
@ -30,12 +34,12 @@ abstract class TestCase extends BaseTestCase
protected function cleanUpGeneratedFiles() 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(app_path('Http'));
$this->removeFileOrDir(database_path('migrations')); $this->removeFileOrDir(database_path('migrations'));
$this->removeFileOrDir(database_path('factories')); $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('routes'));
$this->removeFileOrDir(base_path('tests/BrowserKitTest.php')); $this->removeFileOrDir(base_path('tests/BrowserKitTest.php'));
$this->removeFileOrDir(base_path('tests/Feature')); $this->removeFileOrDir(base_path('tests/Feature'));

Loading…
Cancel
Save