Browse Source

Generate feature test for crud-api command

tags/1.1.0
Nafies Luthfi 8 years ago
parent
commit
fadfd746df
  1. 2
      src/CrudApiMake.php
  2. 5
      src/Generators/BaseGenerator.php
  3. 5
      src/Generators/ControllerGenerator.php
  4. 8
      src/Generators/FeatureTestGenerator.php
  5. 96
      src/stubs/test-feature-api.stub
  6. 10
      tests/CrudApiMakeCommandTest.php
  7. 223
      tests/Generators/Api/ApiFeatureTestGeneratorTest.php

2
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();
}

5
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';
}
}

5
src/Generators/ControllerGenerator.php

@ -71,9 +71,4 @@ class ControllerGenerator extends BaseGenerator
return $controllerFileContent;
}
private function isForApi()
{
return $this->command->getName() == 'make:crud-api';
}
}

8
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",

96
src/stubs/test-feature-api.stub

@ -0,0 +1,96 @@
<?php
namespace Tests\Feature\Api;
use fullMstr;
use Tests\BrowserKitTest as TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class ManageMastersTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function user_can_see_master_list_in_master_index_page()
{
$user = $this->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,
]);
}
}

10
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"));
}
}

223
tests/Generators/Api/ApiFeatureTestGeneratorTest.php

@ -0,0 +1,223 @@
<?php
namespace Tests\Generators\Api;
use Tests\TestCase;
class ApiFeatureTestGeneratorTest extends TestCase
{
/** @test */
public function it_creates_correct_feature_test_class_content()
{
$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 = "<?php
namespace Tests\Feature\Api;
use {$this->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 = "<?php
namespace Tests\Feature\Api;
use {$this->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")));
}
}
Loading…
Cancel
Save