first commit

This commit is contained in:
Noor E Ilahi
2026-01-09 12:54:53 +05:30
commit 7ccf44f7da
1070 changed files with 113036 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
<?php
namespace Tests\Factories;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
abstract class Factory
{
protected bool $timestampsHidden = true;
protected bool $randomRelations = false;
abstract protected function getModelInstance(): Model;
abstract public function createRandomModelData(): array;
abstract public function create(): Model;
public function createMany(int $amount = 1): Collection
{
$models = array_map(fn () => $this->create(), range(0, $amount));
return collect($models);
}
protected function hideTimestamps(): void
{
$this->getModelInstance()->makeHidden(['created_at', 'updated_at', 'deleted_at']);
}
protected function hideCanAttribute(): void
{
$this->getModelInstance()->makeHidden(['can']);
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace Tests\Factories;
use App\Models\Task;
use App\Models\TimeInterval;
use App\Models\User;
use Carbon\Carbon;
use Faker\Factory as FakerFactory;
use Tests\Facades\TaskFactory;
use Tests\Facades\UserFactory;
class IntervalFactory extends Factory
{
private ?User $user = null;
private ?Task $task = null;
private TimeInterval $interval;
public function createRandomModelDataWithRelation(): array
{
return array_merge($this->createRandomModelData(), [
'task_id' => TaskFactory::create()->id,
'user_id' => UserFactory::create()->id,
]);
}
public function createRandomModelData(): array
{
$randomDateTime = FakerFactory::create()->unique()->dateTimeThisYear();
$randomDateTime = Carbon::instance($randomDateTime);
return [
'end_at' => $randomDateTime->toIso8601String(),
'start_at' => $randomDateTime->subSeconds(random_int(1, 3600))->toIso8601String(),
'activity_fill' => random_int(1, 100),
'mouse_fill' => random_int(1, 100),
'keyboard_fill' => random_int(1, 100),
];
}
public function forUser(User $user): self
{
$this->user = $user;
return $this;
}
public function forTask(Task $task): self
{
$this->task = $task;
return $this;
}
public function withRandomRelations(): self
{
$this->randomRelations = true;
return $this;
}
public function create(): TimeInterval
{
$modelData = $this->createRandomModelData();
$this->interval = TimeInterval::make($modelData);
$this->defineUser();
$this->defineTask();
if ($this->timestampsHidden) {
$this->hideTimestamps();
}
$this->interval->save();
return $this->interval;
}
private function defineUser(): void
{
if ($this->randomRelations || !$this->user) {
$this->user = UserFactory::create();
}
$this->interval->user_id = $this->user->id;
}
private function defineTask(): void
{
if ($this->randomRelations || !$this->task) {
$this->task = TaskFactory::forUser($this->user)->create();
}
$this->interval->task_id = $this->task->id;
}
protected function getModelInstance(): TimeInterval
{
return $this->interval;
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Tests\Factories;
use App\Models\Invitation;
use Faker\Factory as FakerFactory;
use Illuminate\Database\Eloquent\Model;
class InvitationFactory extends Factory
{
private Invitation $invitation;
protected function getModelInstance(): Model
{
return $this->invitation;
}
public function createRequestData(): array
{
$faker = FakerFactory::create();
return [
'users' => [
[
'email' => $faker->unique()->email,
'role_id' => 1
]
],
];
}
public function createRandomModelData(): array
{
$faker = FakerFactory::create();
return [
'email' => $faker->unique()->email,
'key' => $faker->uuid,
'expires_at' => now()->addDays(1),
];
}
public function create(): Model
{
$modelData = $this->createRandomModelData();
$this->invitation = Invitation::make($modelData);
$this->invitation->save();
return $this->invitation;
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Tests\Factories;
use App\Models\Project;
use Faker\Factory as FakerFactory;
use Tests\Facades\TaskFactory;
class ProjectFactory extends Factory
{
private const COMPANY_ID = 1;
private const DESCRIPTION_LENGTH = 300;
private int $needsTasks = 0;
private int $needsIntervals = 0;
private array $users = [];
private ?Project $project = null;
protected function getModelInstance(): Project
{
return $this->project;
}
public function withTasks(int $quantity = 1): self
{
$this->needsTasks = $quantity;
return $this;
}
public function create(): Project
{
$modelData = $this->createRandomModelData();
$this->project = Project::create($modelData);
if ($this->users) {
foreach ($this->users as $user) {
$this->project->users()->attach($user->id);
}
}
if ($this->needsTasks) {
$this->createTasks();
}
if ($this->timestampsHidden) {
$this->hideTimestamps();
}
$this->hideCanAttribute();
return $this->project;
}
public function forUsers(array $users): self
{
$this->users = $users;
return $this;
}
public function createRandomModelData(): array
{
$faker = FakerFactory::create();
return [
'company_id' => self::COMPANY_ID,
'name' => $faker->company,
'description' => $faker->text(self::DESCRIPTION_LENGTH),
'source' => 'internal',
];
}
protected function createTasks(): void
{
do {
TaskFactory::withIntervals($this->needsIntervals)
->forProject($this->project)
->create();
} while (--$this->needsTasks);
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Tests\Factories;
use App\Models\ProjectsUsers;
use App\Models\User;
use Illuminate\Support\Arr;
use Tests\Facades\ProjectFactory;
use Tests\Facades\UserFactory;
class ProjectUserFactory extends Factory
{
public const USER_ROLE = 2;
public const MANAGER_ROLE = 1;
public const AUDITOR_ROLE = 3;
private ProjectsUsers $projectUser;
private ?User $user = null;
private ?int $roleId = null;
protected function getModelInstance(): ProjectsUsers
{
return $this->projectUser;
}
public function createRandomModelData(): array
{
return $this->createModelDataWithRelations();
}
public function createModelDataWithRelations(): array
{
return [
'project_id' => ProjectFactory::create()->id,
'user_id' => UserFactory::create()->id,
'role_id' =>$this->getRandomRoleId()
];
}
private function defineModelData(): array
{
$this->user ??= UserFactory::create();
$this->roleId ??= $this->getRandomRoleId();
return [
'project_id' => ProjectFactory::create()->id,
'user_id' => $this->user->id,
'role_id' => $this->roleId
];
}
private function getRandomRoleId()
{
return Arr::random([self::USER_ROLE, self::MANAGER_ROLE, self::AUDITOR_ROLE]);
}
public function forUser(User $user): self
{
$this->user = $user;
return $this;
}
public function setRole(int $roleId): self
{
$this->roleId = $roleId;
return $this;
}
public function create(): ProjectsUsers
{
$this->projectUser = ProjectsUsers::create($this->defineModelData());
if ($this->timestampsHidden) {
$this->hideTimestamps();
}
return $this->projectUser;
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace Tests\Factories;
use App\Models\Screenshot;
use App\Models\TimeInterval;
use Faker\Factory as FakerFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Storage;
use Tests\Facades\IntervalFactory;
class ScreenshotFactory extends Factory
{
private ?TimeInterval $interval = null;
private Screenshot $screenshot;
private bool $fakeStorage = false;
public function fake(): self
{
$this->fakeStorage = true;
return $this;
}
protected function getModelInstance(): Model
{
return $this->screenshot;
}
public function createRandomModelData(): array
{
return $this->generateScreenshotData();
}
private function generateScreenshotData(): array
{
$name = FakerFactory::create()->unique()->firstName . '.jpg';
$image = UploadedFile::fake()->image($name);
$path = Storage::put('uploads/screenshots', $image);
$thumbnail = Storage::put('uploads/screenshots', UploadedFile::fake()->image($name));
return compact('path', 'thumbnail');
}
public function create(array $attributes = []): Screenshot
{
if ($this->fakeStorage) {
Storage::fake();
}
$modelData = $this->createRandomModelData();
$this->screenshot = Screenshot::make($modelData);
$this->defineInterval();
$this->screenshot->save();
if ($this->timestampsHidden) {
$this->hideTimestamps();
}
return $this->screenshot;
}
public function withRandomRelations(): self
{
$this->randomRelations = true;
return $this;
}
public function forInterval(TimeInterval $interval): self
{
$this->interval = $interval;
return $this;
}
private function defineInterval(): void
{
if ($this->randomRelations || !$this->interval) {
$this->interval = IntervalFactory::create();
}
$this->screenshot->time_interval_id = $this->interval->id;
}
}

View File

@@ -0,0 +1,114 @@
<?php
namespace Tests\Factories;
use App\Models\Priority;
use App\Models\Project;
use App\Models\Status;
use App\Models\Task;
use App\Models\User;
use Faker\Factory as FakerFactory;
use Illuminate\Database\Eloquent\Model;
use Tests\Facades\IntervalFactory;
use Tests\Facades\ProjectFactory;
use Tests\Facades\UserFactory;
class TaskFactory extends Factory
{
private const DESCRIPTION_LENGTH = 10;
private int $intervalsAmount = 0;
private ?User $user = null;
private ?Project $project = null;
private Task $task;
protected function getModelInstance(): Model
{
return $this->task;
}
public function withIntervals(int $quantity = 1): self
{
$this->intervalsAmount = $quantity;
return $this;
}
public function forUser(User $user): self
{
$this->user = $user;
return $this;
}
public function forProject(Project $project): self
{
$this->project = $project;
return $this;
}
public function create(array $attributes = []): Task
{
$modelData = $this->createRandomModelData();
$this->task = Task::make($modelData);
$this->defineProject();
$this->defineUser();
$this->task->save();
if (isset($this->user)) {
$this->task->users()->attach($this->user->id);
}
if ($this->intervalsAmount) {
$this->createIntervals();
}
if ($this->timestampsHidden) {
$this->hideTimestamps();
}
return $this->task;
}
public function createRandomModelData(): array
{
$faker = FakerFactory::create();
return [
'task_name' => $faker->jobTitle,
'description' => $faker->text(self::DESCRIPTION_LENGTH),
'priority_id' => Priority::min('id'),
'status_id' => Status::min('id'),
];
}
private function defineProject(): void
{
if (!isset($this->project)) {
$this->project = ProjectFactory::create();
}
$this->task->project_id = $this->project->id;
}
private function defineUser(): void
{
if (!isset($this->user)) {
$this->user = UserFactory::create();
}
}
private function createIntervals(): void
{
do {
IntervalFactory::forUser($this->user)
->forTask($this->task)
->create();
} while (--$this->intervalsAmount);
}
}

View File

@@ -0,0 +1,149 @@
<?php
namespace Tests\Factories;
use App\Enums\ScreenshotsState;
use App\Models\User;
use Carbon\Carbon;
use Faker\Factory as FakerFactory;
use Illuminate\Database\Eloquent\Model;
use Tymon\JWTAuth\Facades\JWTAuth;
class UserFactory extends Factory
{
public const USER_ROLE = 2;
public const MANAGER_ROLE = 1;
public const AUDITOR_ROLE = 3;
private int $tokensAmount = 0;
private ?int $roleId = null;
private User $user;
private bool $isAdmin = false;
protected function getModelInstance(): Model
{
return $this->user;
}
public function create(): User
{
$modelData = $this->createRandomModelData();
if ($this->isAdmin) {
$modelData['is_admin'] = true;
}
$this->user = User::create($modelData);
if ($this->tokensAmount) {
$this->createTokens();
}
if ($this->roleId) {
$this->assignRole();
}
$this->user->save();
if ($this->timestampsHidden) {
$this->hideTimestamps();
}
return $this->user;
}
public function createRandomModelData(): array
{
$faker = FakerFactory::create();
$fullName = $faker->name;
return [
'full_name' => $fullName,
'email' => $faker->unique()->safeEmail,
'url' => '',
'company_id' => 1,
'avatar' => '',
'screenshots_state' => ScreenshotsState::REQUIRED,
'manual_time' => 0,
'computer_time_popup' => 300,
'blur_screenshots' => 0,
'web_and_app_monitoring' => 1,
'screenshots_interval' => 5,
'active' => 1,
'password' => $fullName,
'user_language' => 'en',
'role_id' => 2,
'type' => 'employee',
'nonce' => 0,
'last_activity' => Carbon::now()->subMinutes(rand(1, 55)),
];
}
public function createRandomRegistrationModelData(): array
{
$faker = FakerFactory::create();
return [
'full_name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'active' => 1,
'password' => $faker->password,
'screenshots_interval' => 5,
'user_language' => 'en',
'screenshots_state' => ScreenshotsState::REQUIRED,
'computer_time_popup' => 10,
'timezone' => 'UTC',
'role_id' => 2,
'type' => 'employee'
];
}
public function withTokens(int $quantity = 1): self
{
$this->tokensAmount = $quantity;
return $this;
}
public function asAdmin(): self
{
$this->roleId = self::USER_ROLE;
$this->isAdmin = true;
return $this;
}
public function asManager(): self
{
$this->roleId = self::MANAGER_ROLE;
return $this;
}
public function asAuditor(): self
{
$this->roleId = self::AUDITOR_ROLE;
return $this;
}
public function asUser(): self
{
$this->roleId = self::USER_ROLE;
return $this;
}
protected function createTokens(): void
{
$tokens = array_map(fn() => [
'token' => JWTAuth::fromUser($this->user),
'expires_at' => now()->addDay()
], range(0, $this->tokensAmount));
cache(["testing:{$this->user->id}:tokens" => $tokens]);
}
protected function assignRole(): void
{
$this->user->role_id = $this->roleId;
}
}