7 changed files with 337 additions and 12 deletions
-
2src/CrudApiMake.php
-
5src/Generators/BaseGenerator.php
-
5src/Generators/ControllerGenerator.php
-
8src/Generators/FeatureTestGenerator.php
-
96src/stubs/test-feature-api.stub
-
10tests/CrudApiMakeCommandTest.php
-
223tests/Generators/Api/ApiFeatureTestGeneratorTest.php
@ -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, |
|||
]); |
|||
} |
|||
} |
|||
@ -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"))); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue