From 79a4bb73169445bb7a1e1e6bef4aff8a25dc1a12 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 16 Sep 2024 10:35:44 -0700 Subject: [PATCH] Add tests for delete depreciation endpoint --- database/factories/UserFactory.php | 5 +++ .../Api/DeleteDepreciationTest.php | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/Feature/Depreciations/Api/DeleteDepreciationTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index f30733854f..598de7c39c 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -336,6 +336,11 @@ class UserFactory extends Factory return $this->appendPermission(['customfields.delete' => '1']); } + public function deleteDepreciations() + { + return $this->appendPermission(['depreciations.delete' => '1']); + } + private function appendPermission(array $permission) { return $this->state(function ($currentState) use ($permission) { diff --git a/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php new file mode 100644 index 0000000000..2e93f92092 --- /dev/null +++ b/tests/Feature/Depreciations/Api/DeleteDepreciationTest.php @@ -0,0 +1,42 @@ +create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.depreciations.destroy', $depreciation)) + ->assertForbidden(); + } + + public function testCanDeleteDepreciation() + { + $depreciation = Depreciation::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteDepreciations()->create()) + ->deleteJson(route('api.depreciations.destroy', $depreciation)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('depreciations', ['id' => $depreciation->id]); + } + + public function testCannotDeleteDepreciationThatHasAssociatedModels() + { + $depreciation = Depreciation::factory()->hasModels()->create(); + + $this->actingAsForApi(User::factory()->deleteDepreciations()->create()) + ->deleteJson(route('api.depreciations.destroy', $depreciation)) + ->assertStatusMessageIs('error'); + + $this->assertNotNull($depreciation->fresh(), 'Depreciation unexpectedly deleted'); + } +}