From 1683201aef1c5d3e376d13f9a6152618d2251a0f Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 28 Aug 2018 22:26:56 +0800 Subject: [PATCH] Make sure same base test case do not use alias --- src/Generators/FeatureTestGenerator.php | 2 + src/Generators/ModelPolicyTestGenerator.php | 2 + src/Generators/ModelTestGenerator.php | 1 + .../Generators/Api/ApiFeatureTestGeneratorTest.php | 2 +- tests/Generators/FeatureTestGeneratorTest.php | 151 ++++++++++++++++++++- tests/Generators/ModelPolicyTestGeneratorTest.php | 62 ++++++++- tests/Generators/ModelTestGeneratorTest.php | 53 +++++++- .../Generators/Simple/FeatureTestGeneratorTest.php | 2 +- 8 files changed, 270 insertions(+), 5 deletions(-) diff --git a/src/Generators/FeatureTestGenerator.php b/src/Generators/FeatureTestGenerator.php index 4d160b5..cb03f02 100644 --- a/src/Generators/FeatureTestGenerator.php +++ b/src/Generators/FeatureTestGenerator.php @@ -38,6 +38,8 @@ class FeatureTestGenerator extends BaseGenerator $stub = $this->getStubFileContent($stubName); $baseTestClass = config('simple-crud.base_test_class'); $stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub); + $stub = str_replace('use Tests\TestCase as TestCase', 'use Tests\TestCase', $stub); + return $this->replaceStubString($stub); } diff --git a/src/Generators/ModelPolicyTestGenerator.php b/src/Generators/ModelPolicyTestGenerator.php index 6b729fe..52a8cad 100644 --- a/src/Generators/ModelPolicyTestGenerator.php +++ b/src/Generators/ModelPolicyTestGenerator.php @@ -30,6 +30,8 @@ class ModelPolicyTestGenerator extends BaseGenerator $stub = $this->getStubFileContent($stubName); $baseTestClass = config('simple-crud.base_test_class'); $stub = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $stub); + $stub = str_replace('use Tests\TestCase as TestCase', 'use Tests\TestCase', $stub); + return $this->replaceStubString($stub); } } diff --git a/src/Generators/ModelTestGenerator.php b/src/Generators/ModelTestGenerator.php index c80f0d2..93f9cba 100644 --- a/src/Generators/ModelTestGenerator.php +++ b/src/Generators/ModelTestGenerator.php @@ -31,6 +31,7 @@ class ModelTestGenerator extends BaseGenerator $baseTestClass = config('simple-crud.base_test_class'); $modelFileContent = str_replace('use Tests\BrowserKitTest', 'use '.$baseTestClass, $modelFileContent); + $modelFileContent = str_replace('use Tests\TestCase as TestCase', 'use Tests\TestCase', $modelFileContent); $userModel = config('auth.providers.users.model'); diff --git a/tests/Generators/Api/ApiFeatureTestGeneratorTest.php b/tests/Generators/Api/ApiFeatureTestGeneratorTest.php index 499ee34..d46a8e6 100644 --- a/tests/Generators/Api/ApiFeatureTestGeneratorTest.php +++ b/tests/Generators/Api/ApiFeatureTestGeneratorTest.php @@ -141,7 +141,7 @@ class Manage{$this->model_name}Test extends TestCase namespace Tests\Feature\Api; use {$this->full_model_name}; -use Tests\TestCase as TestCase; +use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; class Manage{$this->model_name}Test extends TestCase diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index f2fc59d..f5cd6a6 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -245,6 +245,155 @@ abstract class {$baseTestClass} extends BaseTestCase /** @test */ public function it_creates_correct_feature_test_class_with_base_test_class_based_on_config_file() { + config(['simple-crud.base_test_path' => 'tests/MyTestCase.php']); + config(['simple-crud.base_test_class' => 'Tests\MyTestCase']); + + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + + $this->assertFileExists(base_path("tests/Feature/Manage{$this->model_name}Test.php")); + $modelClassContent = "full_model_name}; +use Tests\MyTestCase as TestCase; +use Illuminate\Foundation\Testing\DatabaseMigrations; + +class Manage{$this->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} = factory({$this->model_name}::class)->create(); + + \$this->loginAsUser(); + \$this->visitRoute('{$this->table_name}.index'); + \$this->see(\${$this->single_model_var_name}->name); + } + + private function getCreateFields(array \$overrides = []) + { + return array_merge([ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], \$overrides); + } + + /** @test */ + public function user_can_create_a_{$this->lang_name}() + { + \$this->loginAsUser(); + \$this->visitRoute('{$this->table_name}.index'); + + \$this->click(__('{$this->lang_name}.create')); + \$this->seeRouteIs('{$this->table_name}.create'); + + \$this->submitForm(__('{$this->lang_name}.create'), \$this->getCreateFields()); + + \$this->seeRouteIs('{$this->table_name}.show', {$this->model_name}::first()); + + \$this->seeInDatabase('{$this->table_name}', \$this->getCreateFields()); + } + + /** @test */ + public function create_{$this->lang_name}_action_must_pass_validations() + { + \$this->loginAsUser(); + + // Name empty + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields(['name' => ''])); + \$this->assertSessionHasErrors('name'); + + // Name 70 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'name' => str_repeat('Test Title', 7), + ])); + \$this->assertSessionHasErrors('name'); + + // Description 256 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'description' => str_repeat('Long description', 16), + ])); + \$this->assertSessionHasErrors('description'); + } + + private function getEditFields(array \$overrides = []) + { + return array_merge([ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], \$overrides); + } + + /** @test */ + 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->visitRoute('{$this->table_name}.show', \${$this->single_model_var_name}); + \$this->click('edit-{$this->lang_name}-'.\${$this->single_model_var_name}->id); + \$this->seeRouteIs('{$this->table_name}.edit', \${$this->single_model_var_name}); + + \$this->submitForm(__('{$this->lang_name}.update'), \$this->getEditFields()); + + \$this->seeRouteIs('{$this->table_name}.show', \${$this->single_model_var_name}); + + \$this->seeInDatabase('{$this->table_name}', \$this->getEditFields([ + 'id' => \${$this->single_model_var_name}->id, + ])); + } + + /** @test */ + public function edit_{$this->lang_name}_action_must_pass_validations() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + // Name empty + \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields(['name' => ''])); + \$this->assertSessionHasErrors('name'); + + // Name 70 characters + \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ + 'name' => str_repeat('Test Title', 7), + ])); + \$this->assertSessionHasErrors('name'); + + // Description 256 characters + \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ + 'description' => str_repeat('Long description', 16), + ])); + \$this->assertSessionHasErrors('description'); + } + + /** @test */ + public function user_can_delete_a_{$this->lang_name}() + { + \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + factory({$this->model_name}::class)->create(); + + \$this->visitRoute('{$this->table_name}.edit', \${$this->single_model_var_name}); + \$this->click('del-{$this->lang_name}-'.\${$this->single_model_var_name}->id); + \$this->seeRouteIs('{$this->table_name}.edit', [\${$this->single_model_var_name}, 'action' => 'delete']); + + \$this->press(__('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->model_name}Test.php"))); + } + + /** @test */ + public function same_base_test_case_class_name_dont_use_alias() + { config(['simple-crud.base_test_path' => 'tests/TestCase.php']); config(['simple-crud.base_test_class' => 'Tests\TestCase']); @@ -256,7 +405,7 @@ abstract class {$baseTestClass} extends BaseTestCase namespace Tests\Feature; use {$this->full_model_name}; -use Tests\TestCase as TestCase; +use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; class Manage{$this->model_name}Test extends TestCase diff --git a/tests/Generators/ModelPolicyTestGeneratorTest.php b/tests/Generators/ModelPolicyTestGeneratorTest.php index c772768..49c7fc5 100644 --- a/tests/Generators/ModelPolicyTestGeneratorTest.php +++ b/tests/Generators/ModelPolicyTestGeneratorTest.php @@ -66,6 +66,66 @@ class {$this->model_name}PolicyTest extends TestCase /** @test */ public function it_creates_correct_model_policy_test_class_with_base_test_class_based_on_config_file() { + config(['simple-crud.base_test_path' => 'tests/MyTestCase.php']); + config(['simple-crud.base_test_class' => 'Tests\MyTestCase']); + + $userModel = config('auth.providers.users.model'); + + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + + $modelPolicyPath = base_path("tests/Unit/Policies/{$this->model_name}PolicyTest.php"); + $this->assertFileExists($modelPolicyPath); + + $modelPolicyContent = "full_model_name}; +use Illuminate\Foundation\Testing\DatabaseMigrations; +use Tests\MyTestCase as TestCase; + +class {$this->model_name}PolicyTest extends TestCase +{ + use DatabaseMigrations; + + /** @test */ + public function user_can_create_{$this->lang_name}() + { + \$user = \$this->createUser(); + \$this->assertTrue(\$user->can('create', new {$this->model_name})); + } + + /** @test */ + public function user_can_view_{$this->lang_name}() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + \$this->assertTrue(\$user->can('view', \${$this->single_model_var_name})); + } + + /** @test */ + public function user_can_update_{$this->lang_name}() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + \$this->assertTrue(\$user->can('update', \${$this->single_model_var_name})); + } + + /** @test */ + public function user_can_delete_{$this->lang_name}() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + \$this->assertTrue(\$user->can('delete', \${$this->single_model_var_name})); + } +} +"; + $this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath)); + } + + /** @test */ + public function same_base_test_case_class_name_dont_use_alias() + { config(['simple-crud.base_test_path' => 'tests/TestCase.php']); config(['simple-crud.base_test_class' => 'Tests\TestCase']); @@ -82,7 +142,7 @@ namespace Tests\Unit\Policies; use {$this->full_model_name}; use Illuminate\Foundation\Testing\DatabaseMigrations; -use Tests\TestCase as TestCase; +use Tests\TestCase; class {$this->model_name}PolicyTest extends TestCase { diff --git a/tests/Generators/ModelTestGeneratorTest.php b/tests/Generators/ModelTestGeneratorTest.php index 525c06f..cfe7940 100644 --- a/tests/Generators/ModelTestGeneratorTest.php +++ b/tests/Generators/ModelTestGeneratorTest.php @@ -57,6 +57,57 @@ class {$this->model_name}Test extends TestCase /** @test */ public function it_creates_correct_unit_test_class_with_base_test_class_based_on_config_file() { + config(['simple-crud.base_test_path' => 'tests/MyTestCase.php']); + config(['simple-crud.base_test_class' => 'Tests\MyTestCase']); + + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); + + $uniTestPath = base_path("tests/Unit/Models/{$this->model_name}Test.php"); + $this->assertFileExists($uniTestPath); + $modelClassContent = "full_model_name}; +use Illuminate\Foundation\Testing\DatabaseMigrations; +use Tests\MyTestCase as TestCase; + +class {$this->model_name}Test extends TestCase +{ + use DatabaseMigrations; + + /** @test */ + public function a_{$this->lang_name}_has_name_link_attribute() + { + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + + \$this->assertEquals( + link_to_route('{$this->table_name}.show', \${$this->single_model_var_name}->name, [\${$this->single_model_var_name}], [ + 'title' => __( + 'app.show_detail_title', + ['name' => \${$this->single_model_var_name}->name, 'type' => __('{$this->lang_name}.{$this->lang_name}')] + ), + ]), \${$this->single_model_var_name}->name_link + ); + } + + /** @test */ + public function a_{$this->lang_name}_has_belongs_to_creator_relation() + { + \${$this->single_model_var_name} = factory({$this->model_name}::class)->make(); + + \$this->assertInstanceOf(User::class, \${$this->single_model_var_name}->creator); + \$this->assertEquals(\${$this->single_model_var_name}->creator_id, \${$this->single_model_var_name}->creator->id); + } +} +"; + $this->assertEquals($modelClassContent, file_get_contents($uniTestPath)); + } + + /** @test */ + public function same_base_test_case_class_name_dont_use_alias() + { config(['simple-crud.base_test_path' => 'tests/TestCase.php']); config(['simple-crud.base_test_class' => 'Tests\TestCase']); @@ -71,7 +122,7 @@ namespace Tests\Unit\Models; use App\User; use {$this->full_model_name}; use Illuminate\Foundation\Testing\DatabaseMigrations; -use Tests\TestCase as TestCase; +use Tests\TestCase; class {$this->model_name}Test extends TestCase { diff --git a/tests/Generators/Simple/FeatureTestGeneratorTest.php b/tests/Generators/Simple/FeatureTestGeneratorTest.php index d816aa0..0a1cbbd 100644 --- a/tests/Generators/Simple/FeatureTestGeneratorTest.php +++ b/tests/Generators/Simple/FeatureTestGeneratorTest.php @@ -209,7 +209,7 @@ abstract class {$baseTestClass} extends BaseTestCase namespace Tests\Feature; use {$this->full_model_name}; -use Tests\TestCase as TestCase; +use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; class Manage{$this->model_name}Test extends TestCase