Files
WMS/Modules/Helpdesk/Tests/Concerns/BuildsHelpdeskFixtures.php
T
2026-06-25 12:12:06 +01:00

268 lines
8.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Tests\Concerns;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Modules\Helpdesk\Models\Category;
use Modules\Helpdesk\Models\GitRepository;
use Modules\Helpdesk\Models\KanbanBoard;
use Modules\Helpdesk\Models\Priority;
use Modules\Helpdesk\Models\Project;
use Modules\Helpdesk\Models\Status;
use Modules\Helpdesk\Models\Team;
use Modules\Helpdesk\Models\Ticket;
trait BuildsHelpdeskFixtures
{
protected function user(array $attributes = []): User
{
/** @var User $user */
$user = User::factory()->create(array_merge([
'name' => 'Helpdesk User',
'email' => fake()->unique()->safeEmail(),
'password' => Hash::make('password'),
], $attributes));
return $user;
}
protected function agent(array $attributes = []): User
{
$user = $this->user($attributes);
$user->forceFill(['helpdesk_agent' => true])->save();
return $user;
}
protected function admin(array $attributes = []): User
{
$user = $this->user($attributes);
$user->forceFill(['helpdesk_agent' => true, 'helpdesk_admin' => true])->save();
return $user;
}
protected function ticketStatus(array $attributes = []): Status
{
return Status::query()->create(array_merge([
'name' => 'Open',
'slug' => 'test-' . Str::uuid()->toString(),
'color' => 'blue',
'sort_order' => 10,
'is_closed' => false,
], $attributes));
}
protected function closedStatus(array $attributes = []): Status
{
return $this->ticketStatus(array_merge([
'name' => 'Closed',
'slug' => 'test-' . Str::uuid()->toString(),
'color' => 'green',
'sort_order' => 99,
'is_closed' => true,
], $attributes));
}
protected function priority(array $attributes = []): Priority
{
return Priority::query()->create(array_merge([
'name' => 'Normal',
'slug' => 'test-' . Str::uuid()->toString(),
'color' => 'blue',
'sort_order' => 10,
], $attributes));
}
protected function category(array $attributes = []): Category
{
return Category::query()->create(array_merge([
'company_id' => null,
'name' => 'Support',
'slug' => 'test-' . Str::uuid()->toString(),
'color' => 'blue',
'description' => 'Support requests',
], $attributes));
}
protected function team(array $attributes = []): Team
{
return Team::query()->create(array_merge([
'company_id' => null,
'name' => 'Support Team',
'slug' => 'test-' . Str::uuid()->toString(),
'color' => 'indigo',
'description' => 'Primary support team',
], $attributes));
}
protected function project(array $attributes = []): Project
{
return Project::query()->create(array_merge([
'company_id' => null,
'team_id' => null,
'title' => 'WMS Project',
'slug' => 'test-' . Str::uuid()->toString(),
'color' => 'blue',
'description' => 'Warehouse platform project',
], $attributes));
}
protected function repository(array $attributes = []): GitRepository
{
return GitRepository::query()->create(array_merge([
'company_id' => null,
'project_id' => null,
'name' => 'Core',
'provider' => 'git',
'remote_url' => 'git@example.com:wms/core.git',
'default_branch' => 'develop',
'webhook_secret' => 'test-secret',
'is_active' => true,
], $attributes));
}
protected function ticket(array $attributes = []): Ticket
{
$owner = $attributes['owner'] ?? $this->user(['name' => 'Requester']);
$agent = $attributes['agent'] ?? null;
unset($attributes['owner'], $attributes['agent']);
return Ticket::query()->create(array_merge([
'reference' => 'HD-' . now()->format('Y') . '-' . str_pad((string) random_int(1, 99999), 5, '0', STR_PAD_LEFT),
'user_id' => $owner->id,
'agent_id' => $agent?->id,
'status_id' => $this->ticketStatus()->id,
'board_id' => $attributes['board_id'] ?? null,
'priority_id' => $this->priority()->id,
'category_id' => null,
'project_id' => null,
'team_id' => null,
'subject' => 'Test ticket subject',
'body' => '<p>Detailed helpdesk ticket body.</p>',
'last_activity_at' => now(),
], $attributes));
}
protected function seedLookupData(): array
{
$open = Status::query()->updateOrCreate(
['slug' => 'open'],
[
'name' => 'Open',
'color' => 'blue',
'sort_order' => 10,
'is_closed' => false,
],
);
$closed = Status::query()->updateOrCreate(
['slug' => 'closed'],
[
'name' => 'Closed',
'color' => 'green',
'sort_order' => 99,
'is_closed' => true,
],
);
$normal = Priority::query()->updateOrCreate(
['slug' => 'normal'],
[
'name' => 'Normal',
'color' => 'blue',
'sort_order' => 10,
],
);
$high = Priority::query()->updateOrCreate(
['slug' => 'high'],
[
'name' => 'High',
'color' => 'red',
'sort_order' => 1,
],
);
$category = Category::query()->updateOrCreate(
['slug' => 'support'],
[
'company_id' => null,
'name' => 'Support',
'color' => 'blue',
'description' => 'Support requests',
],
);
$team = Team::query()->updateOrCreate(
['slug' => 'support-team'],
[
'company_id' => null,
'name' => 'Support Team',
'color' => 'indigo',
'description' => 'Primary support team',
],
);
$project = Project::query()->updateOrCreate(
['slug' => 'wms-core'],
[
'company_id' => null,
'team_id' => $team->id,
'title' => 'WMS Core',
'color' => 'blue',
'description' => 'Warehouse platform project',
],
);
$board = KanbanBoard::query()->updateOrCreate(
['slug' => 'standard-support'],
[
'name' => 'Standard Support',
'description' => 'Default support board',
'is_default' => true,
'sort_order' => 1,
],
);
$board->statuses()->syncWithoutDetaching([
$open->id => ['sort_order' => 10],
$closed->id => ['sort_order' => 20],
]);
return compact('open', 'closed', 'normal', 'high', 'category', 'team', 'project', 'board');
}
protected function ensureCompanyTableHasOne(): ?int
{
if (! Schema::hasTable('companies')) {
return null;
}
$columns = Schema::getColumnListing('companies');
$payload = [];
foreach ($columns as $column) {
if (in_array($column, ['id', 'created_at', 'updated_at', 'deleted_at'], true)) {
continue;
}
if ($column === 'name') {
$payload[$column] = 'MPBA Test Company';
}
}
if (! array_key_exists('name', $payload) && in_array('company_name', $columns, true)) {
$payload['company_name'] = 'MPBA Test Company';
}
return (int) DB::table('companies')->insertGetId($payload);
}
}