From 4a5515c8d20778bf53354636949b5af52d950a3e Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 24 Sep 2017 08:56:58 +0800 Subject: [PATCH] Set correct controller and feature test stub --- src/CrudMake.php | 4 +- src/stubs/controller.model.stub | 63 +++++++++++++------ src/stubs/test-feature.stub | 90 +++++++++++++++++++++++++++ src/stubs/{unit-test.stub => test-unit.stub} | 0 src/stubs/test.stub | 19 ------ tests/Generators/ControllerGeneratorTest.php | 63 +++++++++++++------ tests/Generators/FeatureTestGeneratorTest.php | 85 ++++++++++++++++++++++--- 7 files changed, 258 insertions(+), 66 deletions(-) create mode 100644 src/stubs/test-feature.stub rename src/stubs/{unit-test.stub => test-unit.stub} (100%) delete mode 100644 src/stubs/test.stub diff --git a/src/CrudMake.php b/src/CrudMake.php index b7bd5cb..c97233c 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -176,13 +176,13 @@ class CrudMake extends Command public function getFeatureTestContent() { - $stub = $this->files->get(__DIR__.'/stubs/test.stub'); + $stub = $this->files->get(__DIR__.'/stubs/test-feature.stub'); return $this->replaceStubString($stub); } public function getUnitTestContent() { - $stub = $this->files->get(__DIR__.'/stubs/unit-test.stub'); + $stub = $this->files->get(__DIR__.'/stubs/test-unit.stub'); return $this->replaceStubString($stub); } diff --git a/src/stubs/controller.model.stub b/src/stubs/controller.model.stub index 87aaa0c..401d8c2 100644 --- a/src/stubs/controller.model.stub +++ b/src/stubs/controller.model.stub @@ -4,44 +4,48 @@ namespace App\Http\Controllers; use App\Master; use Illuminate\Http\Request; -use App\Http\Controllers\Controller; class MastersController extends Controller { /** - * Display a listing of the resource. + * Display a listing of the master. * * @return \Illuminate\Http\Response */ public function index() { - // + $editableMaster = null; + $masters = Master::paginate(25); + + if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) { + $editableMaster = Master::find(request('id')); + } + + return view('masters.index', compact('masters', 'editableMaster')); } /** - * Store a newly created resource in storage. + * Store a newly created master in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { - // - } + $this->validate($request, [ + 'name' => 'required|max:60', + 'description' => 'required|max:255', + ]); - /** - * Display the specified resource. - * - * @param \App\Master $master - * @return \Illuminate\Http\Response - */ - public function show(Master $master) - { - // + Master::create($request->only('name', 'description')); + + flash(trans('master.created'), 'success'); + + return redirect()->route('masters.index'); } /** - * Update the specified resource in storage. + * Update the specified master in storage. * * @param \Illuminate\Http\Request $request * @param \App\Master $master @@ -49,17 +53,38 @@ class MastersController extends Controller */ public function update(Request $request, Master $master) { - // + $this->validate($request, [ + 'name' => 'required|max:60', + 'description' => 'required|max:255', + ]); + + $master = $master->update($request->only('name', 'description')); + + flash(trans('master.updated'), 'success'); + + return redirect()->route('masters.index'); } /** - * Remove the specified resource from storage. + * Remove the specified master from storage. * * @param \App\Master $master * @return \Illuminate\Http\Response */ public function destroy(Master $master) { - // + $this->validate(request(), [ + 'master_id' => 'required', + ]); + + if (request('master_id') == $master->id && $master->delete()) { + flash(trans('master.deleted'), 'success'); + + return redirect()->route('masters.index'); + } + + flash(trans('master.undeleted'), 'error'); + + return back(); } } diff --git a/src/stubs/test-feature.stub b/src/stubs/test-feature.stub new file mode 100644 index 0000000..05193f2 --- /dev/null +++ b/src/stubs/test-feature.stub @@ -0,0 +1,90 @@ +create(['name' => 'Testing name', 'description' => 'Testing 123']); + $master2 = factory(Master::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']); + + $this->loginAsUser(); + $this->visit(route('masters.index')); + $this->see($master1->name); + $this->see($master2->name); + } + + /** @test */ + public function user_can_create_a_master() + { + $this->loginAsUser(); + $this->visit(route('masters.index')); + + $this->click(trans('master.create')); + $this->seePageIs(route('masters.index', ['action' => 'create'])); + + $this->type('Master 1 name', 'name'); + $this->type('Master 1 description', 'description'); + $this->press(trans('master.create')); + + $this->seePageIs(route('masters.index')); + + $this->seeInDatabase('masters', [ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ]); + } + + /** @test */ + public function user_can_edit_a_master_within_search_query() + { + $this->loginAsUser(); + $master = factory(Master::class)->create(['description' => 'Testing 123']); + + $this->visit(route('masters.index', ['q' => '123'])); + $this->click('edit-master-'.$master->id); + $this->seePageIs(route('masters.index', ['action' => 'edit', 'id' => $master->id, 'q' => '123'])); + + $this->type('Master 1 name', 'name'); + $this->type('Master 1 description', 'description'); + $this->press(trans('master.update')); + + $this->visit(route('masters.index', ['q' => '123'])); + + $this->seeInDatabase('masters', [ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ]); + } + + /** @test */ + public function user_can_delete_a_master() + { + $this->loginAsUser(); + $master = factory(Master::class)->create(); + + $this->visit(route('masters.index', [$master->id])); + $this->click('del-master-'.$master->id); + $this->seePageIs(route('masters.index', ['action' => 'delete', 'id' => $master->id])); + + $this->seeInDatabase('masters', [ + 'id' => $master->id, + ]); + + $this->press(trans('app.delete_confirm_button')); + + $this->dontSeeInDatabase('masters', [ + 'id' => $master->id, + ]); + } +} diff --git a/src/stubs/unit-test.stub b/src/stubs/test-unit.stub similarity index 100% rename from src/stubs/unit-test.stub rename to src/stubs/test-unit.stub diff --git a/src/stubs/test.stub b/src/stubs/test.stub deleted file mode 100644 index 2d69e53..0000000 --- a/src/stubs/test.stub +++ /dev/null @@ -1,19 +0,0 @@ -assertTrue(true); - } -} diff --git a/tests/Generators/ControllerGeneratorTest.php b/tests/Generators/ControllerGeneratorTest.php index aafe30a..5b280bc 100644 --- a/tests/Generators/ControllerGeneratorTest.php +++ b/tests/Generators/ControllerGeneratorTest.php @@ -18,44 +18,48 @@ namespace App\Http\Controllers; use App\Item; use Illuminate\Http\Request; -use App\Http\Controllers\Controller; class ItemsController extends Controller { /** - * Display a listing of the resource. + * Display a listing of the item. * * @return \Illuminate\Http\Response */ public function index() { - // + \$editableItem = null; + \$items = Item::paginate(25); + + if (in_array(request('action'), ['edit', 'delete']) && request('id') != null) { + \$editableItem = Item::find(request('id')); + } + + return view('items.index', compact('items', 'editableItem')); } /** - * Store a newly created resource in storage. + * Store a newly created item in storage. * * @param \Illuminate\Http\Request \$request * @return \Illuminate\Http\Response */ public function store(Request \$request) { - // - } + \$this->validate(\$request, [ + 'name' => 'required|max:60', + 'description' => 'required|max:255', + ]); - /** - * Display the specified resource. - * - * @param \App\Item \$item - * @return \Illuminate\Http\Response - */ - public function show(Item \$item) - { - // + Item::create(\$request->only('name', 'description')); + + flash(trans('item.created'), 'success'); + + return redirect()->route('items.index'); } /** - * Update the specified resource in storage. + * Update the specified item in storage. * * @param \Illuminate\Http\Request \$request * @param \App\Item \$item @@ -63,18 +67,39 @@ class ItemsController extends Controller */ public function update(Request \$request, Item \$item) { - // + \$this->validate(\$request, [ + 'name' => 'required|max:60', + 'description' => 'required|max:255', + ]); + + \$item = \$item->update(\$request->only('name', 'description')); + + flash(trans('item.updated'), 'success'); + + return redirect()->route('items.index'); } /** - * Remove the specified resource from storage. + * Remove the specified item from storage. * * @param \App\Item \$item * @return \Illuminate\Http\Response */ public function destroy(Item \$item) { - // + \$this->validate(request(), [ + 'item_id' => 'required', + ]); + + if (request('item_id') == \$item->id && \$item->delete()) { + flash(trans('item.deleted'), 'success'); + + return redirect()->route('items.index'); + } + + flash(trans('item.undeleted'), 'error'); + + return back(); } } "; diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index c732a35..09c72cf 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -16,19 +16,90 @@ class FeatureTestGeneratorTest extends TestCase namespace Tests\Feature; +use App\Item; use Tests\TestCase; +use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Storage; use Illuminate\Foundation\Testing\DatabaseMigrations; class ManageItemsTest extends TestCase { - /** - * A basic test example. - * - * @return void - */ - public function testExample() + use DatabaseMigrations; + + /** @test */ + public function user_can_see_item_list_in_item_index_page() + { + \$item1 = factory(Item::class)->create(['name' => 'Testing name', 'description' => 'Testing 123']); + \$item2 = factory(Item::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']); + + \$this->loginAsUser(); + \$this->visit(route('items.index')); + \$this->see(\$item1->name); + \$this->see(\$item2->name); + } + + /** @test */ + public function user_can_create_a_item() { - \$this->assertTrue(true); + \$this->loginAsUser(); + \$this->visit(route('items.index')); + + \$this->click(trans('item.create')); + \$this->seePageIs(route('items.index', ['action' => 'create'])); + + \$this->type('Item 1 name', 'name'); + \$this->type('Item 1 description', 'description'); + \$this->press(trans('item.create')); + + \$this->seePageIs(route('items.index')); + + \$this->seeInDatabase('items', [ + 'name' => 'Item 1 name', + 'description' => 'Item 1 description', + ]); + } + + /** @test */ + public function user_can_edit_a_item_within_search_query() + { + \$this->loginAsUser(); + \$item = factory(Item::class)->create(['description' => 'Testing 123']); + + \$this->visit(route('items.index', ['q' => '123'])); + \$this->click('edit-item-'.\$item->id); + \$this->seePageIs(route('items.index', ['action' => 'edit', 'id' => \$item->id, 'q' => '123'])); + + \$this->type('Item 1 name', 'name'); + \$this->type('Item 1 description', 'description'); + \$this->press(trans('item.update')); + + \$this->visit(route('items.index', ['q' => '123'])); + + \$this->seeInDatabase('items', [ + 'name' => 'Item 1 name', + 'description' => 'Item 1 description', + ]); + } + + /** @test */ + public function user_can_delete_a_item() + { + \$this->loginAsUser(); + \$item = factory(Item::class)->create(); + + \$this->visit(route('items.index', [\$item->id])); + \$this->click('del-item-'.\$item->id); + \$this->seePageIs(route('items.index', ['action' => 'delete', 'id' => \$item->id])); + + \$this->seeInDatabase('items', [ + 'id' => \$item->id, + ]); + + \$this->press(trans('app.delete_confirm_button')); + + \$this->dontSeeInDatabase('items', [ + 'id' => \$item->id, + ]); } } ";