part fix for tests

This commit is contained in:
Mark Gregory
2026-06-23 16:49:30 +01:00
parent 3b0393a1fe
commit 4765c34889
6 changed files with 119 additions and 19 deletions
@@ -6,7 +6,11 @@ namespace Modules\Helpdesk\Tests\Feature;
use App\Models\Company;
use App\Models\User;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Modules\Helpdesk\Models\Priority;
use Modules\Helpdesk\Models\Status;
use Modules\Helpdesk\Models\Ticket;
use Tests\TestCase;
@@ -20,7 +24,6 @@ final class TicketTimeLogTest extends TestCase
*/
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, [
@@ -48,7 +51,6 @@ final class TicketTimeLogTest extends TestCase
*/
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, [
@@ -65,12 +67,21 @@ final class TicketTimeLogTest extends TestCase
->assertSessionHasErrors('minutes');
}
/**
* Create a company for ticket time-log feature tests.
* Create a company for ticket time-log feature tests without requiring a CompanyFactory.
*/
private function createCompany(): Company
{
return Company::factory()->create();
return Company::query()->create([
'name' => 'Helpdesk Test Company '.Str::random(8),
'slug' => 'helpdesk-test-company-'.Str::lower(Str::random(8)),
'status' => 'active',
'timezone' => 'Europe/London',
'billing_email' => 'helpdesk-test-'.Str::lower(Str::random(8)).'@example.test',
'is_system_company' => false,
]);
}
/**
@@ -84,15 +95,79 @@ final class TicketTimeLogTest extends TestCase
}
/**
* Create a company-scoped ticket for ticket time-log feature tests.
* Create a company-scoped ticket using only columns present in the schema.
*
* @param array<string, mixed> $attributes
*/
private function createTicketForCompany(Company $company, array $attributes = []): Ticket
{
return Ticket::factory()->create(array_merge([
'company_id' => $company->getKey(),
], $attributes));
$ticket = new Ticket();
$table = $ticket->getTable();
$data = [];
$put = static function (string $column, mixed $value) use (&$data, $table): void {
if (Schema::hasColumn($table, $column)) {
$data[$column] = $value;
}
};
$status = Status::query()->firstOrCreate(
['slug' => 'open'],
[
'name' => 'Open',
'color' => 'blue',
'sort_order' => 10,
'is_closed' => false,
],
);
$priority = Priority::query()->firstOrCreate(
['slug' => 'normal'],
[
'name' => 'Normal',
'color' => 'blue',
'sort_order' => 20,
],
);
$userId = $attributes['created_by_id']
?? $attributes['user_id']
?? $attributes['owner_id']
?? $attributes['assigned_to_id']
?? User::factory()->create(['company_id' => $company->getKey()])->getKey();
$put('company_id', $company->getKey());
$put('user_id', $userId);
$put('owner_id', $userId);
$put('created_by_id', $userId);
$put('assigned_to_id', $attributes['assigned_to_id'] ?? $userId);
$put('agent_id', $attributes['assigned_to_id'] ?? $userId);
$put('status_id', $attributes['status_id'] ?? $status->getKey());
$put('priority_id', $attributes['priority_id'] ?? $priority->getKey());
$put('reference', 'HD-'.now()->format('Y').'-'.str_pad((string) random_int(1, 9999), 4, '0', STR_PAD_LEFT));
$put('subject', 'Time log test ticket');
$put('title', 'Time log test ticket');
$put('body', '<p>Time log test ticket body.</p>');
$put('details', '<p>Time log test ticket body.</p>');
$put('description', '<p>Time log test ticket body.</p>');
$put('search_text', 'time log test ticket body');
$put('last_activity_at', now());
$unid = strtoupper(str_replace('-', '', (string) Str::uuid()));
$put('guid', (string) Str::uuid());
$put('uuid', (string) Str::uuid());
$put('unid', $unid);
$put('root_unid', $unid);
$put('sync_sequence', 1);
$put('source_system', 'phpunit');
foreach ($attributes as $column => $value) {
$put((string) $column, $value);
}
return Ticket::query()->create($data);
}
}