From d4426e4686dd6106de39660cbede793be5f1c504 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 9 Sep 2024 11:03:00 -0700 Subject: [PATCH 1/2] Add simple tests for company creation via UI --- database/factories/UserFactory.php | 5 ++ .../Companies/Ui/CreateCompaniesTest.php | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/Feature/Companies/Ui/CreateCompaniesTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 151c114310..746d88a589 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -241,6 +241,11 @@ class UserFactory extends Factory return $this->appendPermission(['components.view' => '1']); } + public function createCompanies() + { + return $this->appendPermission(['companies.create' => '1']); + } + public function createComponents() { return $this->appendPermission(['components.create' => '1']); diff --git a/tests/Feature/Companies/Ui/CreateCompaniesTest.php b/tests/Feature/Companies/Ui/CreateCompaniesTest.php new file mode 100644 index 0000000000..a00e208350 --- /dev/null +++ b/tests/Feature/Companies/Ui/CreateCompaniesTest.php @@ -0,0 +1,53 @@ +actingAs(User::factory()->create()) + ->get(route('companies.create')) + ->assertForbidden(); + } + + public function testCreateCompanyPageRenders() + { + $this->actingAs(User::factory()->createCompanies()->create()) + ->get(route('companies.create')) + ->assertOk() + ->assertViewIs('companies.edit'); + } + + public function testValidDataRequiredToCreateCompany() + { + $this->actingAs(User::factory()->createCompanies()->create()) + ->post(route('companies.store'), [ + // + ]) + ->assertSessionHasErrors([ + 'name', + ]); + } + + public function testCanCreateCompany() + { + $data = [ + 'email' => 'email@example.com', + 'fax' => '619-666-6666', + 'name' => 'My New Company', + 'phone' => '619-555-5555', + ]; + + $user = User::factory()->createCompanies()->create(); + + $this->actingAs($user) + ->post(route('companies.store'), array_merge($data, ['redirect_option' => 'index'])) + ->assertRedirect(route('companies.index')); + + $this->assertDatabaseHas('companies', array_merge($data)); + } +} From 5d5f421294195c4a88db5c4f8cf87fe47ef2c326 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 9 Sep 2024 16:49:43 -0700 Subject: [PATCH 2/2] Add test case --- tests/Feature/Companies/Ui/CreateCompaniesTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Feature/Companies/Ui/CreateCompaniesTest.php b/tests/Feature/Companies/Ui/CreateCompaniesTest.php index a00e208350..18d95d401a 100644 --- a/tests/Feature/Companies/Ui/CreateCompaniesTest.php +++ b/tests/Feature/Companies/Ui/CreateCompaniesTest.php @@ -22,6 +22,13 @@ class CreateCompaniesTest extends TestCase ->assertViewIs('companies.edit'); } + public function testRequiresPermissionToCreateCompany() + { + $this->actingAs(User::factory()->create()) + ->post(route('companies.store')) + ->assertForbidden(); + } + public function testValidDataRequiredToCreateCompany() { $this->actingAs(User::factory()->createCompanies()->create())