Skip to content

Commit

Permalink
Add tests for delete depreciation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusmoore committed Sep 16, 2024
1 parent 2f76c1b commit 79a4bb7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ public function deleteCustomFieldsets()
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) {
Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/Depreciations/Api/DeleteDepreciationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Tests\Feature\Depreciations\Api;

use App\Models\Depreciation;
use App\Models\User;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;

class DeleteDepreciationTest extends TestCase implements TestsPermissionsRequirement
{
public function testRequiresPermission()
{
$depreciation = Depreciation::factory()->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');
}
}

0 comments on commit 79a4bb7

Please sign in to comment.