Files
WMS/Modules/Helpdesk/Tests/Concerns/BuildsHelpdeskFixtures.php
T

193 lines
6.5 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 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' => fake()->unique()->slug(2),
'color' => 'blue',
'sort_order' => 10,
'is_closed' => false,
], $attributes));
}
protected function closedStatus(array $attributes = []): Status
{
return $this->ticketStatus(array_merge([
'name' => 'Closed',
'slug' => fake()->unique()->slug(2),
'color' => 'emerald',
'sort_order' => 99,
'is_closed' => true,
], $attributes));
}
protected function priority(array $attributes = []): Priority
{
return Priority::query()->create(array_merge([
'name' => 'Normal',
'slug' => fake()->unique()->slug(2),
'color' => 'blue',
'sort_order' => 10,
], $attributes));
}
protected function category(array $attributes = []): Category
{
return Category::query()->create(array_merge([
'company_id' => null,
'name' => 'Support',
'slug' => fake()->unique()->slug(2),
'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' => fake()->unique()->slug(2),
'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' => fake()->unique()->slug(2),
'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 = $this->ticketStatus(['name' => 'Open', 'slug' => 'open']);
$closed = $this->closedStatus(['name' => 'Closed', 'slug' => 'closed']);
$normal = $this->priority(['name' => 'Normal', 'slug' => 'normal']);
$high = $this->priority(['name' => 'High', 'slug' => 'high', 'color' => 'red', 'sort_order' => 1]);
$category = $this->category(['name' => 'Support', 'slug' => 'support']);
$team = $this->team(['name' => 'Support Team', 'slug' => 'support-team']);
$project = $this->project(['title' => 'WMS Core', 'slug' => 'wms-core', 'team_id' => $team->id]);
$board = KanbanBoard::query()->create(['name' => 'Standard Support', 'slug' => 'standard-support', 'description' => 'Default support board', 'is_default' => true, 'sort_order' => 1]);
$board->statuses()->sync([$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);
}
}