99 lines
2.8 KiB
PHP
99 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Modules\Helpdesk\Tests\Feature;
|
|
|
|
use App\Models\Company;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Modules\Helpdesk\Models\Ticket;
|
|
use Tests\TestCase;
|
|
|
|
final class TicketTimeLogTest extends TestCase
|
|
{
|
|
|
|
use RefreshDatabase;
|
|
|
|
/**
|
|
* It stores a time log for an authorized ticket user.
|
|
*/
|
|
public function test_authorized_user_can_log_ticket_time(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$company = $this->createCompany();
|
|
$user = $this->createUserForCompany($company);
|
|
$ticket = $this->createTicketForCompany($company, [
|
|
'created_by_id' => $user->id,
|
|
'assigned_to_id' => $user->id,
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->post(route('helpdesk.tickets.time-logs.store', $ticket), [
|
|
'minutes' => 45,
|
|
'description' => 'Investigated the issue and updated the ticket.',
|
|
])
|
|
->assertRedirect();
|
|
|
|
$this->assertDatabaseHas('helpdesk_time_logs', [
|
|
'ticket_id' => $ticket->id,
|
|
'user_id' => $user->id,
|
|
'minutes' => 45,
|
|
'description' => 'Investigated the issue and updated the ticket.',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* It rejects invalid logged minutes.
|
|
*/
|
|
public function test_ticket_time_log_requires_valid_minutes(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$company = $this->createCompany();
|
|
$user = $this->createUserForCompany($company);
|
|
$ticket = $this->createTicketForCompany($company, [
|
|
'created_by_id' => $user->id,
|
|
'assigned_to_id' => $user->id,
|
|
]);
|
|
|
|
$this->actingAs($user)
|
|
->from(route('helpdesk.tickets.show', $ticket))
|
|
->post(route('helpdesk.tickets.time-logs.store', $ticket), [
|
|
'minutes' => 0,
|
|
])
|
|
->assertRedirect(route('helpdesk.tickets.show', $ticket))
|
|
->assertSessionHasErrors('minutes');
|
|
}
|
|
|
|
/**
|
|
* Create a company for ticket time-log feature tests.
|
|
*/
|
|
private function createCompany(): Company
|
|
{
|
|
return Company::factory()->create();
|
|
}
|
|
|
|
/**
|
|
* Create a company-scoped user for ticket time-log feature tests.
|
|
*/
|
|
private function createUserForCompany(Company $company): User
|
|
{
|
|
return User::factory()->create([
|
|
'company_id' => $company->getKey(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Create a company-scoped ticket for ticket time-log feature tests.
|
|
*
|
|
* @param array<string, mixed> $attributes
|
|
*/
|
|
private function createTicketForCompany(Company $company, array $attributes = []): Ticket
|
|
{
|
|
return Ticket::factory()->create(array_merge([
|
|
'company_id' => $company->getKey(),
|
|
], $attributes));
|
|
}
|
|
|
|
}
|