|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Javaabu\Imports\Tests\TestSupport\Factories; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Factories\Factory; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use Javaabu\Imports\Tests\TestSupport\Models\Category; |
| 8 | +use Javaabu\Imports\Tests\TestSupport\Models\CategoryType; |
| 9 | + |
| 10 | +class CategoryFactory extends Factory |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The name of the factory's corresponding model. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $model = Category::class; |
| 18 | + |
| 19 | + /** |
| 20 | + * Define the model's default state. |
| 21 | + * |
| 22 | + * @return array<string, mixed> |
| 23 | + */ |
| 24 | + public function definition(): array |
| 25 | + { |
| 26 | + $name = $this->faker->word(); |
| 27 | + |
| 28 | + return [ |
| 29 | + 'name' => $name, |
| 30 | + 'slug' => Str::slug($name), |
| 31 | + 'category_type_id' => CategoryType::factory(), |
| 32 | + 'icon' => null, |
| 33 | + ]; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Indicate that the category has an icon. |
| 38 | + * |
| 39 | + * @return Factory |
| 40 | + */ |
| 41 | + public function withIcon(): Factory |
| 42 | + { |
| 43 | + return $this->state(function (array $attributes) { |
| 44 | + return [ |
| 45 | + 'icon' => 'fa-' . $this->faker->word(), |
| 46 | + ]; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Indicate that the category belongs to a specific category type. |
| 52 | + * |
| 53 | + * @param CategoryType|int $categoryType |
| 54 | + * @return Factory |
| 55 | + */ |
| 56 | + public function forCategoryType($categoryType): Factory |
| 57 | + { |
| 58 | + return $this->state(function (array $attributes) use ($categoryType) { |
| 59 | + return [ |
| 60 | + 'category_type_id' => $categoryType instanceof CategoryType ? $categoryType->id : $categoryType, |
| 61 | + ]; |
| 62 | + }); |
| 63 | + } |
| 64 | +} |
0 commit comments