refactor(factory): menyesuaikan per enum an dan juga menyelaraskan menggunakan $this->faker

This commit is contained in:
Yoga Pangestu 2025-12-09 09:21:29 +07:00
parent e3769b7b8b
commit aab7491a8c
14 changed files with 105 additions and 57 deletions

View File

@ -15,6 +15,7 @@ class ArticleFactory extends Factory
public function definition(): array
{
$title = $this->faker->sentence(6);
$status = $this->faker->randomElement(ArticleStatus::cases());
return [
'author_id' => User::factory(),
@ -22,19 +23,27 @@ public function definition(): array
'slug' => Str::slug($title.'-'.$this->faker->unique()->randomNumber()),
'excerpt' => $this->faker->paragraph(),
'content' => $this->faker->paragraphs(3, true),
'status' => ArticleStatus::PUBLISHED,
'published_at' => $this->faker->optional()->dateTimeBetween('-10 days', 'now'),
'status' => $status,
'published_at' => $status === ArticleStatus::PUBLISHED
? $this->faker->dateTimeBetween('-10 days', 'now')
: null,
'views' => $this->faker->numberBetween(0, 1000),
];
}
public function draft(): Factory
{
return $this->state(fn () => ['status' => ArticleStatus::DRAFT, 'published_at' => null]);
return $this->state(fn () => [
'status' => ArticleStatus::DRAFT,
'published_at' => null,
]);
}
public function published(): Factory
{
return $this->state(fn () => ['status' => ArticleStatus::PUBLISHED, 'published_at' => now()]);
return $this->state(fn () => [
'status' => ArticleStatus::PUBLISHED,
'published_at' => now(),
]);
}
}

View File

@ -14,12 +14,22 @@ public function definition(): array
{
return [
'name' => $this->faker->name(),
'phone_number' => fake()->randomElement([
fake()->numerify('0812 #### ###'),
fake()->numerify('0812 #### ####'),
fake()->numerify('0812 #### #####'),
'phone_number' => $this->faker->randomElement([
$this->faker->numerify('0812 #### ###'),
$this->faker->numerify('0812 #### ####'),
$this->faker->numerify('0812 #### #####'),
]),
'gender' => $this->faker->randomElement(Gender::cases()),
];
}
public function male(): Factory
{
return $this->state(fn () => ['gender' => Gender::MALE]);
}
public function female(): Factory
{
return $this->state(fn () => ['gender' => Gender::FEMALE]);
}
}

View File

@ -16,25 +16,18 @@ public function definition(): array
return [
'full_name' => $this->faker->name(),
'code' => Employee::generateEmployeeCode(),
'phone_number' => fake()->randomElement([
fake()->numerify('0812 #### ###'),
fake()->numerify('0812 #### ####'),
fake()->numerify('0812 #### #####'),
'phone_number' => $this->faker->randomElement([
$this->faker->numerify('0812 #### ###'),
$this->faker->numerify('0812 #### ####'),
$this->faker->numerify('0812 #### #####'),
]),
'base_salary' => $this->faker->randomNumber(6),
'address' => $this->faker->address(),
'birthdate' => $this->faker->dateTimeBetween('-60 years', '-18 years')->format('Y-m-d'),
'hire_date' => $this->faker->date(),
'resign_date' => $this->faker->optional()->date(),
'gender' => fake()->randomElement([
Gender::MALE,
Gender::FEMALE,
]),
'status' => fake()->randomElement([
EmploymentStatus::PERMANENT,
EmploymentStatus::CONTRACT,
EmploymentStatus::INTERN,
]),
'gender' => $this->faker->randomElement(Gender::cases()),
'status' => $this->faker->randomElement(EmploymentStatus::cases()),
];
}

View File

@ -19,7 +19,7 @@ public function definition(): array
'outlet_id' => Outlet::factory(),
'amount' => $this->faker->numberBetween(10000, 200000),
'description' => $this->faker->sentence(6),
'type' => ExpenseType::OPERATIONAL,
'type' => $this->faker->randomElement(ExpenseType::cases()),
];
}

View File

@ -37,8 +37,8 @@ public function definition(): array
'subtotal' => $subtotal,
'discount' => $discount,
'total' => $total,
'channel' => fake()->randomElement(OrderChannel::cases()),
'status' => fake()->randomElement(OrderStatus::cases()),
'channel' => $this->faker->randomElement(OrderChannel::cases()),
'status' => $this->faker->randomElement(OrderStatus::cases()),
'ordered_at' => $this->faker->dateTimeBetween('-1 year', 'now'),
];
}
@ -171,12 +171,18 @@ public function withoutDiscount(): Factory
public function today(): Factory
{
return $this->state(fn () => ['ordered_at' => now(), 'created_at' => now()]);
return $this->state(fn () => [
'ordered_at' => now(),
'created_at' => now(),
]);
}
public function yesterday(): Factory
{
return $this->state(fn () => tap(now()->subDay(), fn ($d) => null) && ['ordered_at' => $d = now()->subDay(), 'created_at' => $d]);
return $this->state(fn () => tap(now()->subDay(), fn ($d) => null) && [
'ordered_at' => $d = now()->subDay(),
'created_at' => $d,
]);
}
public function thisWeek(): Factory

View File

@ -14,21 +14,16 @@ public function definition(): array
{
return [
'name' => $this->faker->company(),
'phone_number' => sprintf(
'%04d %04d %03d',
$this->faker->numberBetween(8000, 8999),
$this->faker->numberBetween(1000, 9999),
$this->faker->numberBetween(100, 999)
),
'phone_number' => $this->faker->randomElement([
$this->faker->numerify('0812 #### ###'),
$this->faker->numerify('0812 #### ####'),
$this->faker->numerify('0812 #### #####'),
]),
'address' => $this->faker->address(),
'landmark' => $this->faker->streetName(),
'maps_url' => 'https://goo.gl/maps/'.$this->faker->regexify('[A-Za-z0-9]{6,10}'),
'opened_date' => $this->faker->date(),
'status' => fake()->randomElement([
OutletStatus::OPERATIONAL,
OutletStatus::UNDER_CONSTRUCTION,
OutletStatus::TERMINATED,
]),
'status' => $this->faker->randomElement(OutletStatus::cases()),
];
}

View File

@ -18,10 +18,10 @@ public function definition(): array
return [
'order_id' => Order::factory(),
'user_id' => User::factory(),
'method' => PaymentMethod::CASH,
'method' => $this->faker->randomElement(PaymentMethod::cases()),
'amount' => $this->faker->numberBetween(20000, 400000),
'paid_at' => $this->faker->dateTimeBetween('-7 days', 'now'),
'status' => PaymentStatus::PENDING,
'status' => $this->faker->randomElement(PaymentStatus::cases()),
];
}

View File

@ -15,7 +15,7 @@ public function definition(): array
{
return [
'payroll_id' => Payroll::factory(),
'type' => SalaryAdjustmentType::BONUS,
'type' => $this->faker->randomElement(SalaryAdjustmentType::cases()),
'description' => $this->faker->sentence(4),
'amount' => $this->faker->numberBetween(50000, 500000),
];

View File

@ -17,6 +17,8 @@ public function definition(): array
$bonus = $this->faker->numberBetween(0, 2000000);
$deduction = $this->faker->numberBetween(0, 500000);
$isPaid = $this->faker->randomElement(IsPaid::cases());
return [
'user_id' => User::factory(),
'period_month' => $this->faker->date('Y-m'),
@ -24,8 +26,10 @@ public function definition(): array
'bonus' => $bonus,
'deduction' => $deduction,
'total_salary' => $base + $bonus - $deduction,
'is_paid' => IsPaid::NOT_PAID,
'paid_at' => null,
'is_paid' => $isPaid,
'paid_at' => $isPaid === IsPaid::PAID
? $this->faker->dateTimeBetween('-3 days', 'now')
: null,
];
}
@ -36,4 +40,12 @@ public function paid(): Factory
'paid_at' => $this->faker->dateTimeBetween('-3 days', 'now'),
]);
}
public function notPaid(): Factory
{
return $this->state(fn () => [
'is_paid' => IsPaid::NOT_PAID,
'paid_at' => null,
]);
}
}

View File

@ -21,7 +21,7 @@ public function definition(): array
'name' => Str::title($name),
'slug' => Str::slug($name.'-'.$this->faker->unique()->randomNumber()),
'sku' => strtoupper($this->faker->unique()->bothify('PER-#####')),
'concentration' => Concentration::EXTRAIT_DE_PERFUME,
'concentration' => $this->faker->randomElement(Concentration::cases()),
'cost_price' => $this->faker->numberBetween(50000, 250000),
'sale_price' => $this->faker->numberBetween(80000, 400000),
'point_per_ml' => $this->faker->numberBetween(1, 30),

View File

@ -21,17 +21,17 @@ public function definition(): array
'change' => $change,
'type' => PointRecordType::REWARD,
'description' => $this->faker->sentence(4),
'is_addition' => $isAddition,
'is_addition' => true,
];
}
public function reward(): Factory
{
return $this->state(fn () => ['type' => PointRecordType::REWARD, 'is_addition' => true]);
return $this->state(fn () => ['type' => PointRecordType::REWARD]);
}
public function redemption(): Factory
public function tier(): Factory
{
return $this->state(fn () => ['type' => PointRecordType::REDEMPTION, 'is_addition' => false]);
return $this->state(fn () => ['type' => PointRecordType::TIER]);
}
}

View File

@ -13,26 +13,39 @@ class PriceRequestFactory extends Factory
public function definition(): array
{
$status = $this->faker->randomElement(PriceRequestStatus::cases());
return [
'user_id' => User::factory(),
'module' => $this->faker->randomElement(['product', 'perfume', 'bottle']),
'status' => PriceRequestStatus::PENDING,
'finalized_at' => null,
'status' => $status,
'finalized_at' => $status === PriceRequestStatus::PENDING
? null
: $this->faker->dateTime(),
];
}
public function pending(): Factory
{
return $this->state(fn () => ['status' => PriceRequestStatus::PENDING, 'finalized_at' => null]);
return $this->state(fn () => [
'status' => PriceRequestStatus::PENDING,
'finalized_at' => null,
]);
}
public function approved(): Factory
{
return $this->state(fn () => ['status' => PriceRequestStatus::APPROVED, 'finalized_at' => $this->faker->dateTime()]);
return $this->state(fn () => [
'status' => PriceRequestStatus::APPROVED,
'finalized_at' => $this->faker->dateTime(),
]);
}
public function rejected(): Factory
{
return $this->state(fn () => ['status' => PriceRequestStatus::REJECTED, 'finalized_at' => $this->faker->dateTime()]);
return $this->state(fn () => [
'status' => PriceRequestStatus::REJECTED,
'finalized_at' => $this->faker->dateTime(),
]);
}
}

View File

@ -16,7 +16,7 @@ public function definition(): array
return [
'outlet_id' => Outlet::factory(),
'period_month' => $this->faker->date('Y-m'),
'status' => StockOpnameStatus::DRAFT,
'status' => $this->faker->randomElement(StockOpnameStatus::cases()),
'note' => $this->faker->optional()->sentence(8),
];
}
@ -26,8 +26,18 @@ public function draft(): Factory
return $this->state(fn () => ['status' => StockOpnameStatus::DRAFT]);
}
public function finalized(): Factory
public function process(): Factory
{
return $this->state(fn () => ['status' => StockOpnameStatus::FINALIZED]);
return $this->state(fn () => ['status' => StockOpnameStatus::PROCESS]);
}
public function pendingApproval(): Factory
{
return $this->state(fn () => ['status' => StockOpnameStatus::PENDING_APPROVAL]);
}
public function completed(): Factory
{
return $this->state(fn () => ['status' => StockOpnameStatus::COMPLETED]);
}
}

View File

@ -15,9 +15,9 @@ public function definition(): array
{
return [
'email' => $this->faker->unique()->safeEmail(),
'username' => fake()->unique()->regexify('[A-Za-z0-9]{8}'),
'username' => $this->faker->unique()->regexify('[A-Za-z0-9]{8}'),
'password' => Hash::make(config('myconfig.password_default')),
'',
'status' => $this->faker->randomElement(UserStatus::cases()),
];
}