From 276cfe2a888efd2bb1b2ebdc8f48d1ab9022b1a1 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 11 Feb 2018 11:33:52 +0800 Subject: [PATCH] Update correct make:crud test and stub for feature test generator Separate make:crud-simple test for feature test generator --- src/CrudSimpleMake.php | 2 +- src/Generators/FeatureTestGenerator.php | 2 +- src/stubs/test-feature-full.stub | 87 +++++++ ...{test-feature.stub => test-feature-simple.stub} | 0 tests/Generators/FeatureTestGeneratorTest.php | 88 +++---- .../Generators/Simple/FeatureTestGeneratorTest.php | 287 +++++++++++++++++++++ 6 files changed, 419 insertions(+), 47 deletions(-) create mode 100644 src/stubs/test-feature-full.stub rename src/stubs/{test-feature.stub => test-feature-simple.stub} (100%) create mode 100644 tests/Generators/Simple/FeatureTestGeneratorTest.php diff --git a/src/CrudSimpleMake.php b/src/CrudSimpleMake.php index f86fbf5..8b75b5b 100644 --- a/src/CrudSimpleMake.php +++ b/src/CrudSimpleMake.php @@ -62,7 +62,7 @@ class CrudSimpleMake extends GeneratorCommand public function generateTestFiles() { app('Luthfi\CrudGenerator\Generators\ModelTestGenerator', ['command' => $this])->generate(); - app('Luthfi\CrudGenerator\Generators\FeatureTestGenerator', ['command' => $this])->generate(); + app('Luthfi\CrudGenerator\Generators\FeatureTestGenerator', ['command' => $this])->generate('simple'); app('Luthfi\CrudGenerator\Generators\ModelPolicyTestGenerator', ['command' => $this])->generate(); } diff --git a/src/Generators/FeatureTestGenerator.php b/src/Generators/FeatureTestGenerator.php index 3f8e105..db12a77 100644 --- a/src/Generators/FeatureTestGenerator.php +++ b/src/Generators/FeatureTestGenerator.php @@ -18,7 +18,7 @@ class FeatureTestGenerator extends BaseGenerator $this->generateFile( "{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", - $this->getContent('test-feature') + $this->getContent('test-feature-'.$type) ); $this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.'); diff --git a/src/stubs/test-feature-full.stub b/src/stubs/test-feature-full.stub new file mode 100644 index 0000000..e51684d --- /dev/null +++ b/src/stubs/test-feature-full.stub @@ -0,0 +1,87 @@ +create(['name' => 'Testing name', 'description' => 'Testing 123']); + $singleMstr2 = factory(Master::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']); + + $this->loginAsUser(); + $this->visit(route('masters.index')); + $this->see($singleMstr1->name); + $this->see($singleMstr2->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.create')); + + $this->submitForm(trans('master.create'), [ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ]); + + $this->seePageIs(route('masters.show', Master::first())); + + $this->seeInDatabase('masters', [ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ]); + } + + /** @test */ + public function user_can_edit_a_master() + { + $this->loginAsUser(); + $singleMstr = factory(Master::class)->create(['name' => 'Testing 123']); + + $this->visit(route('masters.show', $singleMstr)); + $this->click('edit-singleMstr-'.$singleMstr->id); + $this->seePageIs(route('masters.edit', $singleMstr)); + + $this->submitForm(trans('master.update'), [ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ]); + + $this->seePageIs(route('masters.show', $singleMstr)); + + $this->seeInDatabase('masters', [ + 'id' => $singleMstr->id, + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ]); + } + + /** @test */ + public function user_can_delete_a_master() + { + $this->loginAsUser(); + $singleMstr = factory(Master::class)->create(); + + $this->visit(route('masters.edit', $singleMstr)); + $this->click('del-singleMstr-'.$singleMstr->id); + $this->seePageIs(route('masters.edit', [$singleMstr, 'action' => 'delete'])); + + $this->press(trans('app.delete_confirm_button')); + + $this->dontSeeInDatabase('masters', [ + 'id' => $singleMstr->id, + ]); + } +} diff --git a/src/stubs/test-feature.stub b/src/stubs/test-feature-simple.stub similarity index 100% rename from src/stubs/test-feature.stub rename to src/stubs/test-feature-simple.stub diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index d8eef14..a472587 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -80,39 +80,42 @@ class Manage{$this->plural_model_name}Test extends TestCase \$this->visit(route('{$this->table_name}.index')); \$this->click(trans('{$this->lang_name}.create')); - \$this->seePageIs(route('{$this->table_name}.index', ['action' => 'create'])); + \$this->seePageIs(route('{$this->table_name}.create')); - \$this->type('{$this->model_name} 1 name', 'name'); - \$this->type('{$this->model_name} 1 description', 'description'); - \$this->press(trans('{$this->lang_name}.create')); + \$this->submitForm(trans('{$this->lang_name}.create'), [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ]); - \$this->seePageIs(route('{$this->table_name}.index')); + \$this->seePageIs(route('{$this->table_name}.show', {$this->model_name}::first())); \$this->seeInDatabase('{$this->table_name}', [ - 'name' => '{$this->model_name} 1 name', - 'description' => '{$this->model_name} 1 description', + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', ]); } /** @test */ - public function user_can_edit_a_{$this->lang_name}_within_search_query() + public function user_can_edit_a_{$this->lang_name}() { \$this->loginAsUser(); \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); - \$this->visit(route('{$this->table_name}.index', ['q' => '123'])); + \$this->visit(route('{$this->table_name}.show', \${$this->single_model_var_name})); \$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->seePageIs(route('{$this->table_name}.edit', \${$this->single_model_var_name})); - \$this->type('{$this->model_name} 1 name', 'name'); - \$this->type('{$this->model_name} 1 description', 'description'); - \$this->press(trans('{$this->lang_name}.update')); + \$this->submitForm(trans('{$this->lang_name}.update'), [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ]); - \$this->seePageIs(route('{$this->table_name}.index', ['q' => '123'])); + \$this->seePageIs(route('{$this->table_name}.show', \${$this->single_model_var_name})); \$this->seeInDatabase('{$this->table_name}', [ - 'name' => '{$this->model_name} 1 name', - 'description' => '{$this->model_name} 1 description', + 'id' => \${$this->single_model_var_name}->id, + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', ]); } @@ -122,13 +125,9 @@ class Manage{$this->plural_model_name}Test extends TestCase \$this->loginAsUser(); \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); - \$this->visit(route('{$this->table_name}.index', [\${$this->single_model_var_name}->id])); + \$this->visit(route('{$this->table_name}.edit', \${$this->single_model_var_name})); \$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->table_name}', [ - 'id' => \${$this->single_model_var_name}->id, - ]); + \$this->seePageIs(route('{$this->table_name}.edit', [\${$this->single_model_var_name}, 'action' => 'delete'])); \$this->press(trans('app.delete_confirm_button')); @@ -147,7 +146,7 @@ class Manage{$this->plural_model_name}Test extends TestCase config(['simple-crud.base_test_path' => 'tests/TestCase.php']); config(['simple-crud.base_test_class' => 'Tests\TestCase']); - $baseTestPath = base_path('tests/TestCase.php'); + $baseTestPath = base_path('tests/TestCase.php'); $baseTestClass = 'TestCase'; $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); @@ -224,39 +223,42 @@ class Manage{$this->plural_model_name}Test extends TestCase \$this->visit(route('{$this->table_name}.index')); \$this->click(trans('{$this->lang_name}.create')); - \$this->seePageIs(route('{$this->table_name}.index', ['action' => 'create'])); + \$this->seePageIs(route('{$this->table_name}.create')); - \$this->type('{$this->model_name} 1 name', 'name'); - \$this->type('{$this->model_name} 1 description', 'description'); - \$this->press(trans('{$this->lang_name}.create')); + \$this->submitForm(trans('{$this->lang_name}.create'), [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ]); - \$this->seePageIs(route('{$this->table_name}.index')); + \$this->seePageIs(route('{$this->table_name}.show', {$this->model_name}::first())); \$this->seeInDatabase('{$this->table_name}', [ - 'name' => '{$this->model_name} 1 name', - 'description' => '{$this->model_name} 1 description', + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', ]); } /** @test */ - public function user_can_edit_a_{$this->lang_name}_within_search_query() + public function user_can_edit_a_{$this->lang_name}() { \$this->loginAsUser(); \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); - \$this->visit(route('{$this->table_name}.index', ['q' => '123'])); + \$this->visit(route('{$this->table_name}.show', \${$this->single_model_var_name})); \$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->seePageIs(route('{$this->table_name}.edit', \${$this->single_model_var_name})); - \$this->type('{$this->model_name} 1 name', 'name'); - \$this->type('{$this->model_name} 1 description', 'description'); - \$this->press(trans('{$this->lang_name}.update')); + \$this->submitForm(trans('{$this->lang_name}.update'), [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ]); - \$this->seePageIs(route('{$this->table_name}.index', ['q' => '123'])); + \$this->seePageIs(route('{$this->table_name}.show', \${$this->single_model_var_name})); \$this->seeInDatabase('{$this->table_name}', [ - 'name' => '{$this->model_name} 1 name', - 'description' => '{$this->model_name} 1 description', + 'id' => \${$this->single_model_var_name}->id, + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', ]); } @@ -266,13 +268,9 @@ class Manage{$this->plural_model_name}Test extends TestCase \$this->loginAsUser(); \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); - \$this->visit(route('{$this->table_name}.index', [\${$this->single_model_var_name}->id])); + \$this->visit(route('{$this->table_name}.edit', \${$this->single_model_var_name})); \$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->table_name}', [ - 'id' => \${$this->single_model_var_name}->id, - ]); + \$this->seePageIs(route('{$this->table_name}.edit', [\${$this->single_model_var_name}, 'action' => 'delete'])); \$this->press(trans('app.delete_confirm_button')); diff --git a/tests/Generators/Simple/FeatureTestGeneratorTest.php b/tests/Generators/Simple/FeatureTestGeneratorTest.php new file mode 100644 index 0000000..ee27019 --- /dev/null +++ b/tests/Generators/Simple/FeatureTestGeneratorTest.php @@ -0,0 +1,287 @@ +artisan('make:crud-simple', ['name' => $this->model_name, '--no-interaction' => true]); + + $this->assertFileExists(base_path("tests/BrowserKitTest.php")); + $browserKitTestClassContent = "create(); + \$this->actingAs(\$user); + + return \$user; + } +} +"; + $this->assertEquals($browserKitTestClassContent, file_get_contents(base_path("tests/BrowserKitTest.php"))); + } + + /** @test */ + public function it_creates_correct_feature_test_class_content() + { + $this->artisan('make:crud-simple', ['name' => $this->model_name, '--no-interaction' => true]); + + $this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); + $modelClassContent = "full_model_name}; +use Tests\BrowserKitTest as TestCase; +use Illuminate\Foundation\Testing\DatabaseMigrations; + +class Manage{$this->plural_model_name}Test extends TestCase +{ + use DatabaseMigrations; + + /** @test */ + public function user_can_see_{$this->lang_name}_list_in_{$this->lang_name}_index_page() + { + \${$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->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->lang_name}() + { + \$this->loginAsUser(); + \$this->visit(route('{$this->table_name}.index')); + + \$this->click(trans('{$this->lang_name}.create')); + \$this->seePageIs(route('{$this->table_name}.index', ['action' => 'create'])); + + \$this->type('{$this->model_name} 1 name', 'name'); + \$this->type('{$this->model_name} 1 description', 'description'); + \$this->press(trans('{$this->lang_name}.create')); + + \$this->seePageIs(route('{$this->table_name}.index')); + + \$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->lang_name}_within_search_query() + { + \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 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->model_name} 1 name', 'name'); + \$this->type('{$this->model_name} 1 description', 'description'); + \$this->press(trans('{$this->lang_name}.update')); + + \$this->seePageIs(route('{$this->table_name}.index', ['q' => '123'])); + + \$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->lang_name}() + { + \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + + \$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->table_name}', [ + 'id' => \${$this->single_model_var_name}->id, + ]); + + \$this->press(trans('app.delete_confirm_button')); + + \$this->dontSeeInDatabase('{$this->table_name}', [ + 'id' => \${$this->single_model_var_name}->id, + ]); + } +} +"; + $this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php"))); + } + + /** @test */ + public function it_generates_base_test_class_based_on_config_file() + { + config(['simple-crud.base_test_path' => 'tests/TestCase.php']); + config(['simple-crud.base_test_class' => 'Tests\TestCase']); + + $baseTestPath = base_path('tests/TestCase.php'); + $baseTestClass = 'TestCase'; + + $this->artisan('make:crud-simple', ['name' => $this->model_name, '--no-interaction' => true]); + + $this->assertFileExists($baseTestPath); + $browserKitTestClassContent = "create(); + \$this->actingAs(\$user); + + return \$user; + } +} +"; + $this->assertEquals($browserKitTestClassContent, file_get_contents($baseTestPath)); + } + + /** @test */ + public function it_creates_correct_feature_test_class_with_base_test_class_based_on_config_file() + { + config(['simple-crud.base_test_path' => 'tests/TestCase.php']); + config(['simple-crud.base_test_class' => 'Tests\TestCase']); + + $this->artisan('make:crud-simple', ['name' => $this->model_name, '--no-interaction' => true]); + + $this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); + $modelClassContent = "full_model_name}; +use Tests\TestCase as TestCase; +use Illuminate\Foundation\Testing\DatabaseMigrations; + +class Manage{$this->plural_model_name}Test extends TestCase +{ + use DatabaseMigrations; + + /** @test */ + public function user_can_see_{$this->lang_name}_list_in_{$this->lang_name}_index_page() + { + \${$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->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->lang_name}() + { + \$this->loginAsUser(); + \$this->visit(route('{$this->table_name}.index')); + + \$this->click(trans('{$this->lang_name}.create')); + \$this->seePageIs(route('{$this->table_name}.index', ['action' => 'create'])); + + \$this->type('{$this->model_name} 1 name', 'name'); + \$this->type('{$this->model_name} 1 description', 'description'); + \$this->press(trans('{$this->lang_name}.create')); + + \$this->seePageIs(route('{$this->table_name}.index')); + + \$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->lang_name}_within_search_query() + { + \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 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->model_name} 1 name', 'name'); + \$this->type('{$this->model_name} 1 description', 'description'); + \$this->press(trans('{$this->lang_name}.update')); + + \$this->seePageIs(route('{$this->table_name}.index', ['q' => '123'])); + + \$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->lang_name}() + { + \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + + \$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->table_name}', [ + 'id' => \${$this->single_model_var_name}->id, + ]); + + \$this->press(trans('app.delete_confirm_button')); + + \$this->dontSeeInDatabase('{$this->table_name}', [ + 'id' => \${$this->single_model_var_name}->id, + ]); + } +} +"; + $this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php"))); + } +}