|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PDPhilip\Elasticsearch\Tests\Factories; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Factories\Factory; |
| 6 | +use Illuminate\Support\Carbon; |
| 7 | +use PDPhilip\Elasticsearch\Tests\Models\Product; |
| 8 | + |
| 9 | +class ProductFactory extends Factory |
| 10 | +{ |
| 11 | + protected $model = Product::class; |
| 12 | + |
| 13 | + public function randomFail() |
| 14 | + { |
| 15 | + $orders = rand(0, 250); |
| 16 | + // if ($orders == 10) { |
| 17 | + // return 'xx'; |
| 18 | + // } |
| 19 | + |
| 20 | + return $orders; |
| 21 | + } |
| 22 | + |
| 23 | + public function definition(): array |
| 24 | + { |
| 25 | + $tsMs = $this->randomTsAndMs(); |
| 26 | + |
| 27 | + return [ |
| 28 | + 'name' => $this->faker->name(), |
| 29 | + 'description' => $this->faker->realTextBetween(100), |
| 30 | + 'product_id' => $this->faker->uuid(), |
| 31 | + 'in_stock' => rand(0, 100), |
| 32 | + 'status' => rand(1, 9), |
| 33 | + 'color' => $this->faker->safeColorName(), |
| 34 | + 'is_active' => $this->faker->boolean(), |
| 35 | + 'price' => $this->faker->randomFloat(2, 0, 2000), |
| 36 | + 'orders' => $this->randomFail(), |
| 37 | + 'order_values' => $this->randomArrayOfInts(), |
| 38 | + 'last_order_datetime' => $tsMs['datetime'], |
| 39 | + 'last_order_ts' => $tsMs['ts'], |
| 40 | + 'last_order_ms' => $tsMs['ms'], |
| 41 | + |
| 42 | + 'manufacturer' => [ |
| 43 | + 'location' => [ |
| 44 | + 'lat' => $this->faker->latitude(), |
| 45 | + 'lon' => $this->faker->longitude(), |
| 46 | + ], |
| 47 | + 'name' => $this->faker->company(), |
| 48 | + 'country' => $this->faker->country(), |
| 49 | + 'owned_by' => [ |
| 50 | + 'name' => $this->faker->name(), |
| 51 | + 'country' => $this->faker->country(), |
| 52 | + ], |
| 53 | + ], |
| 54 | + 'datetime' => Carbon::now()->format('Y-m-d H:i:s'), |
| 55 | + 'created_at' => Carbon::now(), |
| 56 | + 'updated_at' => Carbon::now(), |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + public function randomTsAndMs() |
| 61 | + { |
| 62 | + $date = Carbon::now(); |
| 63 | + $date->subDays(rand(0, 14))->subMinutes(rand(0, 1440))->subSeconds(rand(0, 60)); |
| 64 | + |
| 65 | + return [ |
| 66 | + 'datetime' => $date->format('Y-m-d H:i:s'), |
| 67 | + 'ts' => $date->getTimestamp(), |
| 68 | + 'ms' => $date->getTimestampMs(), |
| 69 | + ]; |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + public function randomArrayOfInts() |
| 74 | + { |
| 75 | + $array = []; |
| 76 | + $i = 0; |
| 77 | + while ($i < rand(0, 50)) { |
| 78 | + $array[] = rand(5, 200); |
| 79 | + $i++; |
| 80 | + } |
| 81 | + |
| 82 | + return $array; |
| 83 | + } |
| 84 | + |
| 85 | + public function definitionUSA() |
| 86 | + { |
| 87 | + return [ |
| 88 | + 'name' => $this->faker->name(), |
| 89 | + 'product_id' => $this->faker->uuid(), |
| 90 | + 'in_stock' => rand(0, 100), |
| 91 | + 'status' => rand(1, 9), |
| 92 | + 'color' => $this->faker->safeColorName(), |
| 93 | + 'is_active' => $this->faker->boolean(), |
| 94 | + 'price' => $this->faker->randomFloat(2, 0, 2000), |
| 95 | + 'orders' => rand(0, 250), |
| 96 | + 'manufacturer' => [ |
| 97 | + 'location' => [ |
| 98 | + 'lat' => $this->faker->latitude(), |
| 99 | + 'lon' => $this->faker->longitude(), |
| 100 | + ], |
| 101 | + 'name' => $this->faker->company(), |
| 102 | + 'country' => 'United States of America', |
| 103 | + 'owned_by' => [ |
| 104 | + 'name' => $this->faker->name(), |
| 105 | + 'country' => $this->faker->country(), |
| 106 | + ], |
| 107 | + ], |
| 108 | + 'created_at' => Carbon::now(), |
| 109 | + 'updated_at' => Carbon::now(), |
| 110 | + ]; |
| 111 | + } |
| 112 | +} |
0 commit comments