diff --git a/src/stubs/testcases/feature/api.stub b/src/stubs/testcases/feature/api.stub index 586f9d1..0a98561 100644 --- a/src/stubs/testcases/feature/api.stub +++ b/src/stubs/testcases/feature/api.stub @@ -48,6 +48,65 @@ class ManageMasterTest extends TestCase ]); } + private function getCreateFields(array $overrides = []) + { + return array_merge([ + 'name' => 'Vehicle 1 name', + 'description' => 'Vehicle 1 description', + ], $overrides); + } + + /** @test */ + public function validate_master_name_is_required() + { + $user = $this->createUser(); + + // Name empty + $requestBody = $this->getCreateFields(['name' => '']); + $this->postJson( + route('api.masters.store'), + $requestBody, + ['Authorization' => 'Bearer '.$user->api_token] + ); + + $this->seeStatusCode(422); + $this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_master_name_is_not_more_than_60_characters() + { + $user = $this->createUser(); + + // Name 70 characters + $requestBody = $this->getCreateFields(['name' => str_repeat('Test Title', 7)]); + $this->postJson( + route('api.masters.store'), + $requestBody, + ['Authorization' => 'Bearer '.$user->api_token] + ); + + $this->seeStatusCode(422); + $this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_master_description_is_not_more_than_255_characters() + { + $user = $this->createUser(); + + // Description 256 characters + $requestBody = $this->getCreateFields(['description' => str_repeat('Long description', 16)]); + $this->postJson( + route('api.masters.store'), + $requestBody, + ['Authorization' => 'Bearer '.$user->api_token] + ); + + $this->seeStatusCode(422); + $this->seeJsonSubset(['errors' => ['description' => []]]); + } + /** @test */ public function user_can_get_a_master_detail() { @@ -87,6 +146,68 @@ class ManageMasterTest extends TestCase ]); } + private function getEditFields(array $overrides = []) + { + return array_merge([ + 'name' => 'Vehicle 1 name', + 'description' => 'Vehicle 1 description', + ], $overrides); + } + + /** @test */ + public function validate_master_name_update_is_required() + { + $user = $this->createUser(); + $singleMstr = factory(Vehicle::class)->create(); + + // Name empty + $requestBody = $this->getEditFields(['name' => '']); + $this->patchJson( + route('api.masters.update', $singleMstr), + $requestBody, + ['Authorization' => 'Bearer '.$user->api_token] + ); + + $this->seeStatusCode(422); + $this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_master_name_update_is_not_more_than_60_characters() + { + $user = $this->createUser(); + $singleMstr = factory(Vehicle::class)->create(); + + // Name 70 characters + $requestBody = $this->getEditFields(['name' => str_repeat('Test Title', 7)]); + $this->patchJson( + route('api.masters.update', $singleMstr), + $requestBody, + ['Authorization' => 'Bearer '.$user->api_token] + ); + + $this->seeStatusCode(422); + $this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_master_description_update_is_not_more_than_255_characters() + { + $user = $this->createUser(); + $singleMstr = factory(Vehicle::class)->create(['name' => 'Testing 123']); + + // Description 256 characters + $requestBody = $this->getEditFields(['description' => str_repeat('Long description', 16)]); + $this->patchJson( + route('api.masters.update', $singleMstr), + $requestBody, + ['Authorization' => 'Bearer '.$user->api_token] + ); + + $this->seeStatusCode(422); + $this->seeJsonSubset(['errors' => ['description' => []]]); + } + /** @test */ public function user_can_delete_a_master() { diff --git a/tests/Generators/Api/ApiFeatureTestGeneratorTest.php b/tests/Generators/Api/ApiFeatureTestGeneratorTest.php index d46a8e6..587e38b 100644 --- a/tests/Generators/Api/ApiFeatureTestGeneratorTest.php +++ b/tests/Generators/Api/ApiFeatureTestGeneratorTest.php @@ -62,6 +62,65 @@ class Manage{$this->model_name}Test extends TestCase ]); } + private function getCreateFields(array \$overrides = []) + { + return array_merge([ + 'name' => 'Vehicle 1 name', + 'description' => 'Vehicle 1 description', + ], \$overrides); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_required() + { + \$user = \$this->createUser(); + + // Name empty + \$requestBody = \$this->getCreateFields(['name' => '']); + \$this->postJson( + route('api.{$this->table_name}.store'), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_not_more_than_60_characters() + { + \$user = \$this->createUser(); + + // Name 70 characters + \$requestBody = \$this->getCreateFields(['name' => str_repeat('Test Title', 7)]); + \$this->postJson( + route('api.{$this->table_name}.store'), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_{$this->lang_name}_description_is_not_more_than_255_characters() + { + \$user = \$this->createUser(); + + // Description 256 characters + \$requestBody = \$this->getCreateFields(['description' => str_repeat('Long description', 16)]); + \$this->postJson( + route('api.{$this->table_name}.store'), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['description' => []]]); + } + /** @test */ public function user_can_get_a_{$this->lang_name}_detail() { @@ -101,6 +160,68 @@ class Manage{$this->model_name}Test extends TestCase ]); } + private function getEditFields(array \$overrides = []) + { + return array_merge([ + 'name' => 'Vehicle 1 name', + 'description' => 'Vehicle 1 description', + ], \$overrides); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_required() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory(Vehicle::class)->create(); + + // Name empty + \$requestBody = \$this->getEditFields(['name' => '']); + \$this->patchJson( + route('api.{$this->table_name}.update', \${$this->single_model_var_name}), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_not_more_than_60_characters() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory(Vehicle::class)->create(); + + // Name 70 characters + \$requestBody = \$this->getEditFields(['name' => str_repeat('Test Title', 7)]); + \$this->patchJson( + route('api.{$this->table_name}.update', \${$this->single_model_var_name}), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_{$this->lang_name}_description_update_is_not_more_than_255_characters() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory(Vehicle::class)->create(['name' => 'Testing 123']); + + // Description 256 characters + \$requestBody = \$this->getEditFields(['description' => str_repeat('Long description', 16)]); + \$this->patchJson( + route('api.{$this->table_name}.update', \${$this->single_model_var_name}), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['description' => []]]); + } + /** @test */ public function user_can_delete_a_{$this->lang_name}() { @@ -186,6 +307,65 @@ class Manage{$this->model_name}Test extends TestCase ]); } + private function getCreateFields(array \$overrides = []) + { + return array_merge([ + 'name' => 'Vehicle 1 name', + 'description' => 'Vehicle 1 description', + ], \$overrides); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_required() + { + \$user = \$this->createUser(); + + // Name empty + \$requestBody = \$this->getCreateFields(['name' => '']); + \$this->postJson( + route('api.{$this->table_name}.store'), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_not_more_than_60_characters() + { + \$user = \$this->createUser(); + + // Name 70 characters + \$requestBody = \$this->getCreateFields(['name' => str_repeat('Test Title', 7)]); + \$this->postJson( + route('api.{$this->table_name}.store'), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_{$this->lang_name}_description_is_not_more_than_255_characters() + { + \$user = \$this->createUser(); + + // Description 256 characters + \$requestBody = \$this->getCreateFields(['description' => str_repeat('Long description', 16)]); + \$this->postJson( + route('api.{$this->table_name}.store'), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['description' => []]]); + } + /** @test */ public function user_can_get_a_{$this->lang_name}_detail() { @@ -225,6 +405,68 @@ class Manage{$this->model_name}Test extends TestCase ]); } + private function getEditFields(array \$overrides = []) + { + return array_merge([ + 'name' => 'Vehicle 1 name', + 'description' => 'Vehicle 1 description', + ], \$overrides); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_required() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory(Vehicle::class)->create(); + + // Name empty + \$requestBody = \$this->getEditFields(['name' => '']); + \$this->patchJson( + route('api.{$this->table_name}.update', \${$this->single_model_var_name}), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_not_more_than_60_characters() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory(Vehicle::class)->create(); + + // Name 70 characters + \$requestBody = \$this->getEditFields(['name' => str_repeat('Test Title', 7)]); + \$this->patchJson( + route('api.{$this->table_name}.update', \${$this->single_model_var_name}), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['name' => []]]); + } + + /** @test */ + public function validate_{$this->lang_name}_description_update_is_not_more_than_255_characters() + { + \$user = \$this->createUser(); + \${$this->single_model_var_name} = factory(Vehicle::class)->create(['name' => 'Testing 123']); + + // Description 256 characters + \$requestBody = \$this->getEditFields(['description' => str_repeat('Long description', 16)]); + \$this->patchJson( + route('api.{$this->table_name}.update', \${$this->single_model_var_name}), + \$requestBody, + ['Authorization' => 'Bearer '.\$user->api_token] + ); + + \$this->seeStatusCode(422); + \$this->seeJsonSubset(['errors' => ['description' => []]]); + } + /** @test */ public function user_can_delete_a_{$this->lang_name}() {