From 52bccfde612fdeba72ce6f9eea370aa3cf7bf215 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 24 Oct 2017 20:49:16 +0800 Subject: [PATCH] Apply base_test_path and base_test_class config on test class generators --- src/Generators/FeatureTestGenerator.php | 27 ++-- src/Generators/ModelPolicyTestGenerator.php | 10 +- src/Generators/ModelTestGenerator.php | 10 +- tests/Generators/FeatureTestGeneratorTest.php | 144 ++++++++++++++++++++++ tests/Generators/ModelPolicyTestGeneratorTest.php | 60 +++++++++ tests/Generators/ModelTestGeneratorTest.php | 38 +++++- tests/TestCase.php | 22 ++-- 7 files changed, 281 insertions(+), 30 deletions(-) diff --git a/src/Generators/FeatureTestGenerator.php b/src/Generators/FeatureTestGenerator.php index dca3235..47b1c46 100644 --- a/src/Generators/FeatureTestGenerator.php +++ b/src/Generators/FeatureTestGenerator.php @@ -3,8 +3,8 @@ namespace Luthfi\CrudGenerator\Generators; /** -* Feature Test Generator Class -*/ + * Feature Test Generator Class + */ class FeatureTestGenerator extends BaseGenerator { /** @@ -16,7 +16,7 @@ class FeatureTestGenerator extends BaseGenerator $featureTestPath = $this->makeDirectory(base_path('tests/Feature')); $this->generateFile("{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", $this->getContent()); - $this->command->info('Manage'.$this->modelNames['plural_model_name'].'Test generated.'); + $this->command->info('Manage' . $this->modelNames['plural_model_name'] . 'Test generated.'); } /** @@ -24,7 +24,9 @@ class FeatureTestGenerator extends BaseGenerator */ protected function getContent() { - $stub = $this->files->get(__DIR__.'/../stubs/test-feature.stub'); + $stub = $this->files->get(__DIR__ . '/../stubs/test-feature.stub'); + $baseTestClass = config('simple-crud.base_test_class'); + $stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub); return $this->replaceStubString($stub); } @@ -36,12 +38,21 @@ class FeatureTestGenerator extends BaseGenerator private function createBrowserKitBaseTestClass() { $testsPath = base_path('tests'); - if (! $this->files->isDirectory($testsPath)) { + if (!$this->files->isDirectory($testsPath)) { $this->files->makeDirectory($testsPath, 0777, true, true); } - if (! $this->files->exists($testsPath.'/BrowserKitTest.php')) { - $this->generateFile($testsPath.'/BrowserKitTest.php', $this->getBrowserKitBaseTestContent()); + $baseTestPath = base_path(config('simple-crud.base_test_path')); + $baseTestClass = class_basename(config('simple-crud.base_test_class')); + + if (!$this->files->exists($baseTestPath)) { + $browserKitTestClassContent = str_replace( + 'class BrowserKitTest extends', + "class {$baseTestClass} extends", + $this->getBrowserKitBaseTestContent() + ); + + $this->generateFile($baseTestPath, $browserKitTestClassContent); $this->command->info('BrowserKitTest generated.'); } @@ -54,6 +65,6 @@ class FeatureTestGenerator extends BaseGenerator */ public function getBrowserKitBaseTestContent() { - return $this->files->get(__DIR__.'/../stubs/test-browserkit-base-class.stub'); + return $this->files->get(__DIR__ . '/../stubs/test-browserkit-base-class.stub'); } } diff --git a/src/Generators/ModelPolicyTestGenerator.php b/src/Generators/ModelPolicyTestGenerator.php index dec33e0..475f970 100644 --- a/src/Generators/ModelPolicyTestGenerator.php +++ b/src/Generators/ModelPolicyTestGenerator.php @@ -3,8 +3,8 @@ namespace Luthfi\CrudGenerator\Generators; /** -* Model Test Generator Class -*/ + * Model Test Generator Class + */ class ModelPolicyTestGenerator extends BaseGenerator { /** @@ -14,7 +14,7 @@ class ModelPolicyTestGenerator extends BaseGenerator { $modelPolicyTestPath = $this->makeDirectory(base_path('tests/Unit/Policies')); $this->generateFile("{$modelPolicyTestPath}/{$this->modelNames['model_name']}PolicyTest.php", $this->getContent()); - $this->command->info($this->modelNames['model_name'].'PolicyTest (model policy) generated.'); + $this->command->info($this->modelNames['model_name'] . 'PolicyTest (model policy) generated.'); } /** @@ -22,7 +22,9 @@ class ModelPolicyTestGenerator extends BaseGenerator */ protected function getContent() { - $stub = $this->files->get(__DIR__.'/../stubs/test-policy.stub'); + $stub = $this->files->get(__DIR__ . '/../stubs/test-policy.stub'); + $baseTestClass = config('simple-crud.base_test_class'); + $stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub); return $this->replaceStubString($stub); } } diff --git a/src/Generators/ModelTestGenerator.php b/src/Generators/ModelTestGenerator.php index 09c827a..f4dca19 100644 --- a/src/Generators/ModelTestGenerator.php +++ b/src/Generators/ModelTestGenerator.php @@ -3,8 +3,8 @@ namespace Luthfi\CrudGenerator\Generators; /** -* Model Test Generator Class -*/ + * Model Test Generator Class + */ class ModelTestGenerator extends BaseGenerator { /** @@ -14,7 +14,7 @@ class ModelTestGenerator extends BaseGenerator { $unitTestPath = $this->makeDirectory(base_path('tests/Unit/Models')); $this->generateFile("{$unitTestPath}/{$this->modelNames['model_name']}Test.php", $this->getContent()); - $this->command->info($this->modelNames['model_name'].'Test (model) generated.'); + $this->command->info($this->modelNames['model_name'] . 'Test (model) generated.'); } /** @@ -22,7 +22,9 @@ class ModelTestGenerator extends BaseGenerator */ protected function getContent() { - $stub = $this->files->get(__DIR__.'/../stubs/test-unit.stub'); + $stub = $this->files->get(__DIR__ . '/../stubs/test-unit.stub'); + $baseTestClass = config('simple-crud.base_test_class'); + $stub = str_replace('use Tests\BrowserKitTest', 'use ' . $baseTestClass, $stub); return $this->replaceStubString($stub); } } diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index 272352d..d8eef14 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -140,4 +140,148 @@ class Manage{$this->plural_model_name}Test extends TestCase "; $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', ['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', ['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"))); + } } diff --git a/tests/Generators/ModelPolicyTestGeneratorTest.php b/tests/Generators/ModelPolicyTestGeneratorTest.php index ca0a40a..f9f488e 100644 --- a/tests/Generators/ModelPolicyTestGeneratorTest.php +++ b/tests/Generators/ModelPolicyTestGeneratorTest.php @@ -62,4 +62,64 @@ class {$this->model_name}Test extends TestCase "; $this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath)); } + + /** @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/TestCase.php']); + config(['simple-crud.base_test_class' => 'Tests\TestCase']); + + $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\TestCase as TestCase; + +class {$this->model_name}Test extends TestCase +{ + use DatabaseMigrations; + + /** @test */ + public function user_can_create_{$this->lang_name}() + { + \$user = \$this->loginAsUser(); + \$this->assertTrue(\$user->can('create', new {$this->model_name})); + } + + /** @test */ + public function user_can_view_{$this->lang_name}() + { + \$user = \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']); + \$this->assertTrue(\$user->can('view', \${$this->single_model_var_name})); + } + + /** @test */ + public function user_can_update_{$this->lang_name}() + { + \$user = \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']); + \$this->assertTrue(\$user->can('update', \${$this->single_model_var_name})); + } + + /** @test */ + public function user_can_delete_{$this->lang_name}() + { + \$user = \$this->loginAsUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => '{$this->model_name} 1 name']); + \$this->assertTrue(\$user->can('delete', \${$this->single_model_var_name})); + } +} +"; + $this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath)); + } } diff --git a/tests/Generators/ModelTestGeneratorTest.php b/tests/Generators/ModelTestGeneratorTest.php index 82b2d21..36e2cb5 100644 --- a/tests/Generators/ModelTestGeneratorTest.php +++ b/tests/Generators/ModelTestGeneratorTest.php @@ -11,7 +11,8 @@ class ModelTestGeneratorTest extends TestCase { $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); + $uniTestPath = base_path("tests/Unit/Models/{$this->model_name}Test.php"); + $this->assertFileExists($uniTestPath); $modelClassContent = "model_name}Test extends TestCase } } "; - $this->assertEquals($modelClassContent, file_get_contents(base_path("tests/Unit/Models/{$this->model_name}Test.php"))); + $this->assertEquals($modelClassContent, file_get_contents($uniTestPath)); + } + + /** @test */ + public function it_creates_correct_unit_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', ['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\TestCase as TestCase; + +class {$this->model_name}Test extends TestCase +{ + use DatabaseMigrations; + + /** @test */ + public function it_has_name_attribute() + { + \${$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($uniTestPath)); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 5614f02..75a52d9 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -19,12 +19,12 @@ abstract class TestCase extends BaseTestCase parent::setUp(); $this->model_name = class_basename('References/Category'); - $this->full_model_name = 'App\\'.$this->model_name; - $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->full_model_name = 'App\\' . $this->model_name; + $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); + $this->single_model_var_name = camel_case($this->model_name); } public function tearDown() @@ -36,30 +36,28 @@ abstract class TestCase extends BaseTestCase protected function cleanUpGeneratedFiles() { - $this->removeFileOrDir(app_path($this->model_name.'.php')); + $this->removeFileOrDir(app_path($this->model_name . '.php')); $this->removeFileOrDir(app_path('Entities')); $this->removeFileOrDir(app_path('Http')); $this->removeFileOrDir(database_path('migrations')); $this->removeFileOrDir(database_path('factories')); - $this->removeFileOrDir(resource_path('views/'.$this->table_name)); + $this->removeFileOrDir(resource_path('views/' . $this->table_name)); $this->removeFileOrDir(resource_path("lang/en/app.php")); $this->removeFileOrDir(resource_path("lang/en/{$this->lang_name}.php")); $this->removeFileOrDir(base_path('routes')); $this->removeFileOrDir(app_path('Policies')); $this->removeFileOrDir(app_path('Providers')); - $this->removeFileOrDir(base_path('tests/BrowserKitTest.php')); - $this->removeFileOrDir(base_path('tests/Feature')); - $this->removeFileOrDir(base_path('tests/Unit')); + $this->removeFileOrDir(base_path('tests')); } protected function removeFileOrDir($path) { if (file_exists($path) && is_file($path)) { - exec('rm '.$path); + exec('rm ' . $path); } if (file_exists($path) && is_dir($path)) { - exec('rm -r '.$path); + exec('rm -r ' . $path); } }