mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-12 12:51:06 +03:30
feat: add factory for models
This commit is contained in:
Executable
+81
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use App\Models\User;
|
||||
|
||||
class TransferFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
// Get a random model for polymorphic relationship
|
||||
$transferableModels = [
|
||||
\App\Models\Package::class,
|
||||
\App\Models\Item::class,
|
||||
];
|
||||
|
||||
$transferableType = $this->faker->randomElement($transferableModels);
|
||||
$transferable = $transferableType::factory()->create();
|
||||
|
||||
// Generate from_date and ensure to_date is after from_date
|
||||
$fromDate = $this->faker->dateTimeBetween('-1 year', 'now');
|
||||
$toDate = $this->faker->dateTimeBetween($fromDate, '+1 year');
|
||||
|
||||
return [
|
||||
'transferable_type' => $transferableType,
|
||||
'transferable_id' => $transferable->id,
|
||||
'user_id' => User::factory(),
|
||||
'from_date' => $fromDate,
|
||||
'to_date' => $toDate,
|
||||
'status' => $this->faker->randomElement(['approved', 'rejected', 'suspended']),
|
||||
'user_transfer_id' => User::factory(),
|
||||
'deleted_at' => null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the transfer is approved.
|
||||
*/
|
||||
public function approved(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'status' => 'approved',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the transfer is rejected.
|
||||
*/
|
||||
public function rejected(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'status' => 'rejected',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the transfer is suspended.
|
||||
*/
|
||||
public function suspended(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'status' => 'suspended',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the transfer is soft deleted.
|
||||
*/
|
||||
public function deleted(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'deleted_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user