diff --git a/src/CrudApiMake.php b/src/CrudApiMake.php index 166331b..f60a1bf 100644 --- a/src/CrudApiMake.php +++ b/src/CrudApiMake.php @@ -62,7 +62,7 @@ class CrudApiMake extends GeneratorCommand public function generateTestFiles() { app('Luthfi\CrudGenerator\Generators\ModelTestGenerator', ['command' => $this])->generate(); - app('Luthfi\CrudGenerator\Generators\FeatureTestGenerator', ['command' => $this])->generate('simple'); + app('Luthfi\CrudGenerator\Generators\FeatureTestGenerator', ['command' => $this])->generate('api'); app('Luthfi\CrudGenerator\Generators\ModelPolicyTestGenerator', ['command' => $this])->generate(); } diff --git a/src/Generators/BaseGenerator.php b/src/Generators/BaseGenerator.php index b456709..e735cce 100644 --- a/src/Generators/BaseGenerator.php +++ b/src/Generators/BaseGenerator.php @@ -135,4 +135,9 @@ abstract class BaseGenerator { return $this->files->get(__DIR__.'/../stubs/'.$stubName.'.stub'); } + + protected function isForApi() + { + return $this->command->getName() == 'make:crud-api'; + } } diff --git a/src/Generators/ControllerGenerator.php b/src/Generators/ControllerGenerator.php index 3152ea7..450c313 100644 --- a/src/Generators/ControllerGenerator.php +++ b/src/Generators/ControllerGenerator.php @@ -71,9 +71,4 @@ class ControllerGenerator extends BaseGenerator return $controllerFileContent; } - - private function isForApi() - { - return $this->command->getName() == 'make:crud-api'; - } } diff --git a/src/Generators/FeatureTestGenerator.php b/src/Generators/FeatureTestGenerator.php index db12a77..aabef95 100644 --- a/src/Generators/FeatureTestGenerator.php +++ b/src/Generators/FeatureTestGenerator.php @@ -14,7 +14,13 @@ class FeatureTestGenerator extends BaseGenerator { $this->createBrowserKitBaseTestClass(); - $featureTestPath = $this->makeDirectory(base_path('tests/Feature')); + $featureTestPath = 'tests/Feature'; + + if ($this->isForApi()) { + $featureTestPath .= '/Api'; + } + + $featureTestPath = $this->makeDirectory(base_path($featureTestPath)); $this->generateFile( "{$featureTestPath}/Manage{$this->modelNames['plural_model_name']}Test.php", diff --git a/src/stubs/test-feature-api.stub b/src/stubs/test-feature-api.stub new file mode 100644 index 0000000..bce7c6a --- /dev/null +++ b/src/stubs/test-feature-api.stub @@ -0,0 +1,96 @@ +createUser(); + $singleMstr = factory(Master::class)->create(); + + $this->getJson(route('api.masters.index'), [ + 'Authorization' => 'Bearer '.$user->api_token + ]); + + $this->seeJson(['name' => $singleMstr->name]); + } + + /** @test */ + public function user_can_create_a_master() + { + $user = $this->createUser(); + + $this->postJson(route('api.masters.store'), [ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ], [ + 'Authorization' => 'Bearer '.$user->api_token + ]); + + $this->seeInDatabase('masters', [ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ]); + + $this->seeJson(['name' => 'Master 1 name']); + } + + /** @test */ + public function user_can_get_a_master_detail() + { + $user = $this->createUser(); + $singleMstr = factory(Master::class)->create(['name' => 'Testing 123']); + + $this->getJson(route('api.masters.show', $singleMstr), [ + 'Authorization' => 'Bearer '.$user->api_token + ]); + + $this->seeJson(['name' => 'Testing 123']); + } + + /** @test */ + public function user_can_update_a_master() + { + $user = $this->createUser(); + $singleMstr = factory(Master::class)->create(['name' => 'Testing 123']); + + $this->patchJson(route('api.masters.update', $singleMstr), [ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ], [ + 'Authorization' => 'Bearer '.$user->api_token + ]); + + $this->seeInDatabase('masters', [ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ]); + + $this->seeJson(['name' => 'Master 1 name']); + } + + /** @test */ + public function user_can_delete_a_master() + { + $user = $this->createUser(); + $singleMstr = factory(Master::class)->create(); + + $this->deleteJson(route('api.masters.delete', $singleMstr), [ + 'master_id' => $singleMstr->id, + ], [ + 'Authorization' => 'Bearer '.$user->api_token + ]); + + $this->dontSeeInDatabase('masters', [ + 'id' => $singleMstr->id, + ]); + } +} diff --git a/tests/CrudApiMakeCommandTest.php b/tests/CrudApiMakeCommandTest.php index f2e0474..06739ea 100644 --- a/tests/CrudApiMakeCommandTest.php +++ b/tests/CrudApiMakeCommandTest.php @@ -29,7 +29,7 @@ class CrudApiMakeCommandTest extends TestCase $this->assertFileExists(app_path("Policies/{$this->model_name}Policy.php")); $this->assertFileExists(database_path("factories/{$this->model_name}Factory.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); - $this->assertFileExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); + $this->assertFileExists(base_path("tests/Feature/Api/Manage{$this->plural_model_name}Test.php")); } /** @test */ @@ -53,7 +53,7 @@ class CrudApiMakeCommandTest extends TestCase $this->assertFileNotExists(app_path("Policies/{$this->model_name}Policy.php")); $this->assertFileNotExists(database_path("factories/{$this->model_name}Factory.php")); $this->assertFileNotExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); - $this->assertFileNotExists(base_path("tests/Feature/Manage{$this->plural_model_name}Test.php")); + $this->assertFileNotExists(base_path("tests/Feature/Api/Manage{$this->plural_model_name}Test.php")); } /** @test */ @@ -79,7 +79,7 @@ class CrudApiMakeCommandTest extends TestCase $this->assertFileNotExists(app_path("Policies/ProblemPolicy.php")); $this->assertFileNotExists(database_path("factories/ProblemFactory.php")); $this->assertFileNotExists(base_path("tests/Unit/Models/ProblemTest.php")); - $this->assertFileNotExists(base_path("tests/Feature/ManageProblemsTest.php")); + $this->assertFileNotExists(base_path("tests/Feature/Api/ManageProblemsTest.php")); $this->removeFileOrDir(app_path('Entities/Projects')); $this->removeFileOrDir(resource_path('views/problems')); @@ -115,7 +115,7 @@ class CrudApiMakeCommandTest extends TestCase $this->assertFileExists(app_path("Policies/{$modelName}Policy.php")); $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); - $this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php")); + $this->assertFileExists(base_path("tests/Feature/Api/Manage{$pluralModelName}Test.php")); } /** @test */ @@ -148,6 +148,6 @@ class CrudApiMakeCommandTest extends TestCase $this->assertFileExists(database_path("factories/{$modelName}Factory.php")); $this->assertFileExists(base_path("tests/Unit/Models/{$modelName}Test.php")); $this->assertFileExists(app_path("Policies/{$parentName}/{$modelName}Policy.php")); - $this->assertFileExists(base_path("tests/Feature/Manage{$pluralModelName}Test.php")); + $this->assertFileExists(base_path("tests/Feature/Api/Manage{$pluralModelName}Test.php")); } } diff --git a/tests/Generators/Api/ApiFeatureTestGeneratorTest.php b/tests/Generators/Api/ApiFeatureTestGeneratorTest.php new file mode 100644 index 0000000..659b25e --- /dev/null +++ b/tests/Generators/Api/ApiFeatureTestGeneratorTest.php @@ -0,0 +1,223 @@ +artisan('make:crud-api', ['name' => $this->model_name, '--no-interaction' => true]); + + $this->assertFileExists(base_path("tests/Feature/Api/Manage{$this->plural_model_name}Test.php")); + $featureTestClassContent = "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() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + + \$this->getJson(route('api.{$this->table_name}.index'), [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->seeJson(['name' => \${$this->single_model_var_name}->name]); + } + + /** @test */ + public function user_can_create_a_{$this->lang_name}() + { + \$user = \$this->createUser(); + + \$this->postJson(route('api.{$this->table_name}.store'), [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->seeInDatabase('{$this->table_name}', [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ]); + + \$this->seeJson(['name' => '{$this->model_name} 1 name']); + } + + /** @test */ + public function user_can_get_a_{$this->lang_name}_detail() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + \$this->getJson(route('api.{$this->table_name}.show', \${$this->single_model_var_name}), [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->seeJson(['name' => 'Testing 123']); + } + + /** @test */ + public function user_can_update_a_{$this->lang_name}() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + \$this->patchJson(route('api.{$this->table_name}.update', \${$this->single_model_var_name}), [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->seeInDatabase('{$this->table_name}', [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ]); + + \$this->seeJson(['name' => '{$this->model_name} 1 name']); + } + + /** @test */ + public function user_can_delete_a_{$this->lang_name}() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + + \$this->deleteJson(route('api.{$this->table_name}.delete', \${$this->single_model_var_name}), [ + '{$this->lang_name}_id' => \${$this->single_model_var_name}->id, + ], [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->dontSeeInDatabase('{$this->table_name}', [ + 'id' => \${$this->single_model_var_name}->id, + ]); + } +} +"; + $this->assertEquals($featureTestClassContent, file_get_contents(base_path("tests/Feature/Api/Manage{$this->plural_model_name}Test.php"))); + } + + /** @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-api', ['name' => $this->model_name, '--no-interaction' => true]); + + $this->assertFileExists(base_path("tests/Feature/Api/Manage{$this->plural_model_name}Test.php")); + $featureTestClassContent = "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() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + + \$this->getJson(route('api.{$this->table_name}.index'), [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->seeJson(['name' => \${$this->single_model_var_name}->name]); + } + + /** @test */ + public function user_can_create_a_{$this->lang_name}() + { + \$user = \$this->createUser(); + + \$this->postJson(route('api.{$this->table_name}.store'), [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->seeInDatabase('{$this->table_name}', [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ]); + + \$this->seeJson(['name' => '{$this->model_name} 1 name']); + } + + /** @test */ + public function user_can_get_a_{$this->lang_name}_detail() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + \$this->getJson(route('api.{$this->table_name}.show', \${$this->single_model_var_name}), [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->seeJson(['name' => 'Testing 123']); + } + + /** @test */ + public function user_can_update_a_{$this->lang_name}() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + \$this->patchJson(route('api.{$this->table_name}.update', \${$this->single_model_var_name}), [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->seeInDatabase('{$this->table_name}', [ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ]); + + \$this->seeJson(['name' => '{$this->model_name} 1 name']); + } + + /** @test */ + public function user_can_delete_a_{$this->lang_name}() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); + + \$this->deleteJson(route('api.{$this->table_name}.delete', \${$this->single_model_var_name}), [ + '{$this->lang_name}_id' => \${$this->single_model_var_name}->id, + ], [ + 'Authorization' => 'Bearer '.\$user->api_token + ]); + + \$this->dontSeeInDatabase('{$this->table_name}', [ + 'id' => \${$this->single_model_var_name}->id, + ]); + } +} +"; + $this->assertEquals($featureTestClassContent, file_get_contents(base_path("tests/Feature/Api/Manage{$this->plural_model_name}Test.php"))); + } +}