From ea21376daca059a5780816dc3a9aca2fce833ad9 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 8 Mar 2018 20:21:31 +0800 Subject: [PATCH] Add create form validation test --- src/stubs/test-feature-full.stub | 46 ++++++++++---- tests/Generators/FeatureTestGeneratorTest.php | 92 ++++++++++++++++++++------- 2 files changed, 102 insertions(+), 36 deletions(-) diff --git a/src/stubs/test-feature-full.stub b/src/stubs/test-feature-full.stub index c606ba7..8ce0c38 100644 --- a/src/stubs/test-feature-full.stub +++ b/src/stubs/test-feature-full.stub @@ -13,13 +13,19 @@ class ManageMastersTest extends TestCase /** @test */ public function user_can_see_master_list_in_master_index_page() { - $singleMstr1 = factory(Master::class)->create(['name' => 'Testing name', 'description' => 'Testing 123']); - $singleMstr2 = factory(Master::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']); + $singleMstr = factory(Master::class)->create(); $this->loginAsUser(); $this->visit(route('masters.index')); - $this->see($singleMstr1->name); - $this->see($singleMstr2->name); + $this->see($singleMstr->name); + } + + private function getCreateFields(array $overrides = []) + { + return array_merge([ + 'name' => 'Master 1 name', + 'description' => 'Master 1 description', + ], $overrides); } /** @test */ @@ -31,17 +37,33 @@ class ManageMastersTest extends TestCase $this->click(trans('master.create')); $this->seePageIs(route('masters.create')); - $this->submitForm(trans('master.create'), [ - 'name' => 'Master 1 name', - 'description' => 'Master 1 description', - ]); + $this->submitForm(trans('master.create'), $this->getCreateFields()); $this->seePageIs(route('masters.show', Master::first())); - $this->seeInDatabase('masters', [ - 'name' => 'Master 1 name', - 'description' => 'Master 1 description', - ]); + $this->seeInDatabase('masters', $this->getCreateFields()); + } + + /** @test */ + public function create_master_action_must_pass_validations() + { + $this->loginAsUser(); + + // Name empty + $this->post(route('masters.store'), $this->getCreateFields(['name' => ''])); + $this->assertSessionHasErrors('name'); + + // Name 70 characters + $this->post(route('masters.store'), $this->getCreateFields([ + 'name' => str_repeat('Test Title', 7), + ])); + $this->assertSessionHasErrors('name'); + + // Description 256 characters + $this->post(route('masters.store'), $this->getCreateFields([ + 'description' => str_repeat('Long description', 16), + ])); + $this->assertSessionHasErrors('description'); } /** @test */ diff --git a/tests/Generators/FeatureTestGeneratorTest.php b/tests/Generators/FeatureTestGeneratorTest.php index 4ab2896..ede8ded 100644 --- a/tests/Generators/FeatureTestGeneratorTest.php +++ b/tests/Generators/FeatureTestGeneratorTest.php @@ -64,13 +64,19 @@ class Manage{$this->plural_model_name}Test extends TestCase /** @test */ public function user_can_see_{$this->lang_name}_list_in_{$this->lang_name}_index_page() { - \${$this->single_model_var_name}1 = factory({$this->model_name}::class)->create(['name' => 'Testing name', 'description' => 'Testing 123']); - \${$this->single_model_var_name}2 = factory({$this->model_name}::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); \$this->loginAsUser(); \$this->visit(route('{$this->table_name}.index')); - \$this->see(\${$this->single_model_var_name}1->name); - \$this->see(\${$this->single_model_var_name}2->name); + \$this->see(\${$this->single_model_var_name}->name); + } + + private function getCreateFields(array \$overrides = []) + { + return array_merge([ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], \$overrides); } /** @test */ @@ -82,17 +88,33 @@ class Manage{$this->plural_model_name}Test extends TestCase \$this->click(trans('{$this->lang_name}.create')); \$this->seePageIs(route('{$this->table_name}.create')); - \$this->submitForm(trans('{$this->lang_name}.create'), [ - 'name' => '{$this->model_name} 1 name', - 'description' => '{$this->model_name} 1 description', - ]); + \$this->submitForm(trans('{$this->lang_name}.create'), \$this->getCreateFields()); \$this->seePageIs(route('{$this->table_name}.show', {$this->model_name}::first())); - \$this->seeInDatabase('{$this->table_name}', [ - 'name' => '{$this->model_name} 1 name', - 'description' => '{$this->model_name} 1 description', - ]); + \$this->seeInDatabase('{$this->table_name}', \$this->getCreateFields()); + } + + /** @test */ + public function create_{$this->lang_name}_action_must_pass_validations() + { + \$this->loginAsUser(); + + // Name empty + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields(['name' => ''])); + \$this->assertSessionHasErrors('name'); + + // Name 70 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'name' => str_repeat('Test Title', 7), + ])); + \$this->assertSessionHasErrors('name'); + + // Description 256 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'description' => str_repeat('Long description', 16), + ])); + \$this->assertSessionHasErrors('description'); } /** @test */ @@ -207,13 +229,19 @@ class Manage{$this->plural_model_name}Test extends TestCase /** @test */ public function user_can_see_{$this->lang_name}_list_in_{$this->lang_name}_index_page() { - \${$this->single_model_var_name}1 = factory({$this->model_name}::class)->create(['name' => 'Testing name', 'description' => 'Testing 123']); - \${$this->single_model_var_name}2 = factory({$this->model_name}::class)->create(['name' => 'Testing name', 'description' => 'Testing 456']); + \${$this->single_model_var_name} = factory({$this->model_name}::class)->create(); \$this->loginAsUser(); \$this->visit(route('{$this->table_name}.index')); - \$this->see(\${$this->single_model_var_name}1->name); - \$this->see(\${$this->single_model_var_name}2->name); + \$this->see(\${$this->single_model_var_name}->name); + } + + private function getCreateFields(array \$overrides = []) + { + return array_merge([ + 'name' => '{$this->model_name} 1 name', + 'description' => '{$this->model_name} 1 description', + ], \$overrides); } /** @test */ @@ -225,17 +253,33 @@ class Manage{$this->plural_model_name}Test extends TestCase \$this->click(trans('{$this->lang_name}.create')); \$this->seePageIs(route('{$this->table_name}.create')); - \$this->submitForm(trans('{$this->lang_name}.create'), [ - 'name' => '{$this->model_name} 1 name', - 'description' => '{$this->model_name} 1 description', - ]); + \$this->submitForm(trans('{$this->lang_name}.create'), \$this->getCreateFields()); \$this->seePageIs(route('{$this->table_name}.show', {$this->model_name}::first())); - \$this->seeInDatabase('{$this->table_name}', [ - 'name' => '{$this->model_name} 1 name', - 'description' => '{$this->model_name} 1 description', - ]); + \$this->seeInDatabase('{$this->table_name}', \$this->getCreateFields()); + } + + /** @test */ + public function create_{$this->lang_name}_action_must_pass_validations() + { + \$this->loginAsUser(); + + // Name empty + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields(['name' => ''])); + \$this->assertSessionHasErrors('name'); + + // Name 70 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'name' => str_repeat('Test Title', 7), + ])); + \$this->assertSessionHasErrors('name'); + + // Description 256 characters + \$this->post(route('{$this->table_name}.store'), \$this->getCreateFields([ + 'description' => str_repeat('Long description', 16), + ])); + \$this->assertSessionHasErrors('description'); } /** @test */