2024-09-12 15:38:30 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Companies\Api;
|
|
|
|
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\User;
|
|
|
|
use Tests\Concerns\TestsPermissionsRequirement;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class DeleteCompaniesTest extends TestCase implements TestsPermissionsRequirement
|
|
|
|
{
|
|
|
|
public function testRequiresPermission()
|
|
|
|
{
|
|
|
|
$company = Company::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
|
|
->deleteJson(route('api.companies.destroy', $company))
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCannotDeleteCompanyThatHasAssociatedItems()
|
|
|
|
{
|
|
|
|
$companyWithAssets = Company::factory()->hasAssets()->create();
|
|
|
|
$companyWithAccessories = Company::factory()->hasAccessories()->create();
|
|
|
|
$companyWithConsumables = Company::factory()->hasConsumables()->create();
|
|
|
|
$companyWithComponents = Company::factory()->hasComponents()->create();
|
|
|
|
$companyWithUsers = Company::factory()->hasUsers()->create();
|
|
|
|
|
|
|
|
$actor = $this->actingAsForApi(User::factory()->deleteCompanies()->create());
|
|
|
|
|
|
|
|
$actor->deleteJson(route('api.companies.destroy', $companyWithAssets))->assertStatusMessageIs('error');
|
|
|
|
$actor->deleteJson(route('api.companies.destroy', $companyWithAccessories))->assertStatusMessageIs('error');
|
|
|
|
$actor->deleteJson(route('api.companies.destroy', $companyWithConsumables))->assertStatusMessageIs('error');
|
|
|
|
$actor->deleteJson(route('api.companies.destroy', $companyWithComponents))->assertStatusMessageIs('error');
|
|
|
|
$actor->deleteJson(route('api.companies.destroy', $companyWithUsers))->assertStatusMessageIs('error');
|
2024-09-16 14:20:24 -07:00
|
|
|
|
|
|
|
$this->assertDatabaseHas('companies', ['id' => $companyWithAssets->id]);
|
|
|
|
$this->assertDatabaseHas('companies', ['id' => $companyWithAccessories->id]);
|
|
|
|
$this->assertDatabaseHas('companies', ['id' => $companyWithConsumables->id]);
|
|
|
|
$this->assertDatabaseHas('companies', ['id' => $companyWithComponents->id]);
|
|
|
|
$this->assertDatabaseHas('companies', ['id' => $companyWithUsers->id]);
|
2024-09-12 15:38:30 -07:00
|
|
|
}
|
2024-09-16 14:25:11 -07:00
|
|
|
|
|
|
|
public function testCanDeleteCompany()
|
|
|
|
{
|
|
|
|
$company = Company::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->deleteCompanies()->create())
|
|
|
|
->deleteJson(route('api.companies.destroy', $company))
|
|
|
|
->assertStatusMessageIs('success');
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('companies', ['id' => $company->id]);
|
|
|
|
}
|
2024-09-12 15:38:30 -07:00
|
|
|
}
|