From 6764661f90a99cb3203ec388504981a1dc167de6 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 7 Oct 2018 14:53:44 +0800 Subject: [PATCH 1/4] Separate field validation test on full feature test --- src/stubs/testcases/feature/full.stub | 14 ++++++++- tests/Generators/FeatureTestGeneratorTest.php | 42 +++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/stubs/testcases/feature/full.stub b/src/stubs/testcases/feature/full.stub index 16cf7c1..f0325b4 100644 --- a/src/stubs/testcases/feature/full.stub +++ b/src/stubs/testcases/feature/full.stub @@ -45,19 +45,31 @@ class ManageMasterTest extends TestCase } /** @test */ - public function create_master_action_must_pass_validations() + public function validate_master_name_is_required() { $this->loginAsUser(); // Name empty $this->post(route('masters.store'), $this->getCreateFields(['name' => ''])); $this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_master_name_is_not_more_than_60_characters() + { + $this->loginAsUser(); // Name 70 characters $this->post(route('masters.store'), $this->getCreateFields([ 'name' => str_repeat('Test Title', 7), ])); $this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_master_description_is_not_more_than_255_characters() + { + $this->loginAsUser(); // Description 256 characters $this->post(route('masters.store'), $this->getCreateFields([ diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index f5cd6a6..f58398d 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -101,19 +101,31 @@ class Manage{$this->model_name}Test extends TestCase } /** @test */ - public function create_{$this->lang_name}_action_must_pass_validations() + public function validate_{$this->lang_name}_name_is_required() { \$this->loginAsUser(); // Name empty \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields(['name' => ''])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_not_more_than_60_characters() + { + \$this->loginAsUser(); // Name 70 characters \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ 'name' => str_repeat('Test Title', 7), ])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_is_not_more_than_255_characters() + { + \$this->loginAsUser(); // Description 256 characters \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ @@ -298,19 +310,31 @@ class Manage{$this->model_name}Test extends TestCase } /** @test */ - public function create_{$this->lang_name}_action_must_pass_validations() + public function validate_{$this->lang_name}_name_is_required() { \$this->loginAsUser(); // Name empty \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields(['name' => ''])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_not_more_than_60_characters() + { + \$this->loginAsUser(); // Name 70 characters \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ 'name' => str_repeat('Test Title', 7), ])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_is_not_more_than_255_characters() + { + \$this->loginAsUser(); // Description 256 characters \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ @@ -447,19 +471,31 @@ class Manage{$this->model_name}Test extends TestCase } /** @test */ - public function create_{$this->lang_name}_action_must_pass_validations() + public function validate_{$this->lang_name}_name_is_required() { \$this->loginAsUser(); // Name empty \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields(['name' => ''])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_not_more_than_60_characters() + { + \$this->loginAsUser(); // Name 70 characters \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ 'name' => str_repeat('Test Title', 7), ])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_is_not_more_than_255_characters() + { + \$this->loginAsUser(); // Description 256 characters \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ From 53ca5bd697e3d5693022e44d94c9e28807647e68 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 7 Oct 2018 15:01:43 +0800 Subject: [PATCH 2/4] Separate edit field validation test on feature test --- src/stubs/testcases/feature/full.stub | 16 ++++++++- tests/Generators/FeatureTestGeneratorTest.php | 48 +++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/stubs/testcases/feature/full.stub b/src/stubs/testcases/feature/full.stub index f0325b4..65ff3f2 100644 --- a/src/stubs/testcases/feature/full.stub +++ b/src/stubs/testcases/feature/full.stub @@ -106,7 +106,7 @@ class ManageMasterTest extends TestCase } /** @test */ - public function edit_master_action_must_pass_validations() + public function validate_master_name_update_is_required() { $this->loginAsUser(); $master = factory(Master::class)->create(['name' => 'Testing 123']); @@ -114,12 +114,26 @@ class ManageMasterTest extends TestCase // Name empty $this->patch(route('masters.update', $master), $this->getEditFields(['name' => ''])); $this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_master_name_update_is_not_more_than_60_characters() + { + $this->loginAsUser(); + $master = factory(Master::class)->create(['name' => 'Testing 123']); // Name 70 characters $this->patch(route('masters.update', $master), $this->getEditFields([ 'name' => str_repeat('Test Title', 7), ])); $this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_master_description_update_is_not_more_than_255_characters() + { + $this->loginAsUser(); + $master = factory(Master::class)->create(['name' => 'Testing 123']); // Description 256 characters $this->patch(route('masters.update', $master), $this->getEditFields([ diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index f58398d..8a9238f 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -162,7 +162,7 @@ class Manage{$this->model_name}Test extends TestCase } /** @test */ - public function edit_{$this->lang_name}_action_must_pass_validations() + public function validate_{$this->lang_name}_name_update_is_required() { \$this->loginAsUser(); \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); @@ -170,12 +170,26 @@ class Manage{$this->model_name}Test extends TestCase // Name empty \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields(['name' => ''])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_not_more_than_60_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); // Name 70 characters \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ 'name' => str_repeat('Test Title', 7), ])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_update_is_not_more_than_255_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); // Description 256 characters \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ @@ -371,7 +385,7 @@ class Manage{$this->model_name}Test extends TestCase } /** @test */ - public function edit_{$this->lang_name}_action_must_pass_validations() + public function validate_{$this->lang_name}_name_update_is_required() { \$this->loginAsUser(); \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); @@ -379,12 +393,26 @@ class Manage{$this->model_name}Test extends TestCase // Name empty \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields(['name' => ''])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_not_more_than_60_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); // Name 70 characters \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ 'name' => str_repeat('Test Title', 7), ])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_update_is_not_more_than_255_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); // Description 256 characters \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ @@ -532,7 +560,7 @@ class Manage{$this->model_name}Test extends TestCase } /** @test */ - public function edit_{$this->lang_name}_action_must_pass_validations() + public function validate_{$this->lang_name}_name_update_is_required() { \$this->loginAsUser(); \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); @@ -540,12 +568,26 @@ class Manage{$this->model_name}Test extends TestCase // Name empty \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields(['name' => ''])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_not_more_than_60_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); // Name 70 characters \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ 'name' => str_repeat('Test Title', 7), ])); \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_update_is_not_more_than_255_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); // Description 256 characters \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ From 5cc2fc6cdb35a6552d4976a4f7c712724df78091 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 7 Oct 2018 16:18:41 +0800 Subject: [PATCH 3/4] Add validation tests on simple feature test --- src/stubs/testcases/feature/simple.stub | 87 +++++++++++ .../Generators/Simple/FeatureTestGeneratorTest.php | 174 +++++++++++++++++++++ 2 files changed, 261 insertions(+) diff --git a/src/stubs/testcases/feature/simple.stub b/src/stubs/testcases/feature/simple.stub index 6503141..988d381 100644 --- a/src/stubs/testcases/feature/simple.stub +++ b/src/stubs/testcases/feature/simple.stub @@ -42,6 +42,48 @@ class ManageMasterTest extends TestCase ]); } + private function getCreateFields(array $overrides = []) + { + return array_merge([ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ], $overrides); + } + + /** @test */ + public function validate_master_name_is_required() + { + $this->loginAsUser(); + + // Name empty + $this->post(route('masters.store'), $this->getCreateFields(['name' => ''])); + $this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_master_name_is_not_more_than_60_characters() + { + $this->loginAsUser(); + + // Name 70 characters + $this->post(route('masters.store'), $this->getCreateFields([ + 'name' => str_repeat('Test Title', 7), + ])); + $this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_master_description_is_not_more_than_255_characters() + { + $this->loginAsUser(); + + // Description 256 characters + $this->post(route('masters.store'), $this->getCreateFields([ + 'description' => str_repeat('Long description', 16), + ])); + $this->assertSessionHasErrors('description'); + } + /** @test */ public function user_can_edit_a_master_within_search_query() { @@ -65,6 +107,51 @@ class ManageMasterTest extends TestCase ]); } + private function getEditFields(array $overrides = []) + { + return array_merge([ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ], $overrides); + } + + /** @test */ + public function validate_master_name_update_is_required() + { + $this->loginAsUser(); + $master = factory(Master::class)->create(['name' => 'Testing 123']); + + // Name empty + $this->patch(route('masters.update', $master), $this->getEditFields(['name' => ''])); + $this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_master_name_update_is_not_more_than_60_characters() + { + $this->loginAsUser(); + $master = factory(Master::class)->create(['name' => 'Testing 123']); + + // Name 70 characters + $this->patch(route('masters.update', $master), $this->getEditFields([ + 'name' => str_repeat('Test Title', 7), + ])); + $this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_master_description_update_is_not_more_than_255_characters() + { + $this->loginAsUser(); + $master = factory(Master::class)->create(['name' => 'Testing 123']); + + // Description 256 characters + $this->patch(route('masters.update', $master), $this->getEditFields([ + 'description' => str_repeat('Long description', 16), + ])); + $this->assertSessionHasErrors('description'); + } + /** @test */ public function user_can_delete_a_master() { diff --git a/tests/Generators/Simple/FeatureTestGeneratorTest.php b/tests/Generators/Simple/FeatureTestGeneratorTest.php index 0a1cbbd..ed1f68a 100644 --- a/tests/Generators/Simple/FeatureTestGeneratorTest.php +++ b/tests/Generators/Simple/FeatureTestGeneratorTest.php @@ -98,6 +98,48 @@ class Manage{$this->model_name}Test extends TestCase ]); } + private function getCreateFields(array \$overrides = []) + { + return array_merge([ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], \$overrides); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_required() + { + \$this->loginAsUser(); + + // Name empty + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields(['name' => ''])); + \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_not_more_than_60_characters() + { + \$this->loginAsUser(); + + // Name 70 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'name' => str_repeat('Test Title', 7), + ])); + \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_is_not_more_than_255_characters() + { + \$this->loginAsUser(); + + // Description 256 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'description' => str_repeat('Long description', 16), + ])); + \$this->assertSessionHasErrors('description'); + } + /** @test */ public function user_can_edit_a_{$this->lang_name}_within_search_query() { @@ -121,6 +163,51 @@ class Manage{$this->model_name}Test extends TestCase ]); } + private function getEditFields(array \$overrides = []) + { + return array_merge([ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], \$overrides); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_required() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + // Name empty + \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields(['name' => ''])); + \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_not_more_than_60_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + // Name 70 characters + \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ + 'name' => str_repeat('Test Title', 7), + ])); + \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_update_is_not_more_than_255_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + // Description 256 characters + \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ + 'description' => str_repeat('Long description', 16), + ])); + \$this->assertSessionHasErrors('description'); + } + /** @test */ public function user_can_delete_a_{$this->lang_name}() { @@ -248,6 +335,48 @@ class Manage{$this->model_name}Test extends TestCase ]); } + private function getCreateFields(array \$overrides = []) + { + return array_merge([ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], \$overrides); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_required() + { + \$this->loginAsUser(); + + // Name empty + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields(['name' => ''])); + \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_is_not_more_than_60_characters() + { + \$this->loginAsUser(); + + // Name 70 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'name' => str_repeat('Test Title', 7), + ])); + \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_is_not_more_than_255_characters() + { + \$this->loginAsUser(); + + // Description 256 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'description' => str_repeat('Long description', 16), + ])); + \$this->assertSessionHasErrors('description'); + } + /** @test */ public function user_can_edit_a_{$this->lang_name}_within_search_query() { @@ -271,6 +400,51 @@ class Manage{$this->model_name}Test extends TestCase ]); } + private function getEditFields(array \$overrides = []) + { + return array_merge([ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], \$overrides); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_required() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + // Name empty + \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields(['name' => ''])); + \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_name_update_is_not_more_than_60_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + // Name 70 characters + \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ + 'name' => str_repeat('Test Title', 7), + ])); + \$this->assertSessionHasErrors('name'); + } + + /** @test */ + public function validate_{$this->lang_name}_description_update_is_not_more_than_255_characters() + { + \$this->loginAsUser(); + \${$this->lang_name} = factory({$this->model_name}::class)->create(['name' => 'Testing 123']); + + // Description 256 characters + \$this->patch(route('{$this->table_name}.update', \${$this->lang_name}), \$this->getEditFields([ + 'description' => str_repeat('Long description', 16), + ])); + \$this->assertSessionHasErrors('description'); + } + /** @test */ public function user_can_delete_a_{$this->lang_name}() { From e8cfd23676718594d5bf2ed75a51ed4412a79cbb Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 7 Oct 2018 17:02:20 +0800 Subject: [PATCH 4/4] Add validation tests on API feature test --- src/stubs/testcases/feature/api.stub | 121 +++++++++++ .../Generators/Api/ApiFeatureTestGeneratorTest.php | 242 +++++++++++++++++++++ 2 files changed, 363 insertions(+) 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}() {