Skip to content

Commit 88b4c86

Browse files
authored
[Bug] Result factory with fake data (alexjustesen#1268)
1 parent 5976e8f commit 88b4c86

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

database/factories/ResultFactory.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Database\Factories;
44

5+
use App\Enums\ResultStatus;
56
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Lottery;
69

710
/**
811
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Result>
@@ -16,15 +19,34 @@ class ResultFactory extends Factory
1619
*/
1720
public function definition(): array
1821
{
22+
$response = Lottery::odds(1, 100)
23+
->winner(function () {
24+
return '{"type": "log", "level": "error", "message": "A faked error message.", "timestamp": "2024-03-01T01:00:00Z"}';
25+
})
26+
->loser(function () {
27+
return '{"isp": "Speedtest Communications", "ping": {"low": 17.841, "high": 24.077, "jitter": 1.878, "latency": 19.133}, "type": "result", "result": {"id": "d6fe2fb3-f4f8-4cc5-b898-7b42109e67c2", "url": "https://docs.speedtest-tracker.dev", "persisted": true}, "server": {"id": 0, "ip": "127.0.0.1", "host": "docs.speedtest-tracker.dev", "name": "Speedtest", "port": 8080, "country": "United States", "location": "New York City, NY"}, "upload": {"bytes": 124297377, "elapsed": 9628, "latency": {"iqm": 341.111, "low": 16.663, "high": 529.86, "jitter": 37.587}, "bandwidth": 113750000}, "download": {"bytes": 230789788, "elapsed": 14301, "latency": {"iqm": 104.125, "low": 23.72, "high": 269.563, "jitter": 13.447}, "bandwidth": 115625000}, "interface": {"name": "eth0", "isVpn": false, "macAddr": "00:00:00:00:00:00", "externalIp": "127.0.0.1", "internalIp": "127.0.0.1"}, "timestamp": "2024-03-01T01:00:00Z", "packetLoss": 0}';
28+
})
29+
->choose();
30+
31+
$output = json_decode($response, true);
32+
33+
if (Arr::exists($output, 'level')) {
34+
return [
35+
'service' => 'faker',
36+
'data' => $output,
37+
'status' => ResultStatus::Failed,
38+
'scheduled' => false,
39+
];
40+
}
41+
1942
return [
20-
'ping' => fake()->randomFloat(2, 0, 100),
21-
'download' => fake()->randomNumber(),
22-
'upload' => fake()->randomNumber(),
23-
'server_id' => fake()->randomNumber(5, true),
24-
'server_host' => fake()->url(),
25-
'server_name' => fake()->word(),
26-
'url' => fake()->url(),
27-
'data' => json_encode(fake()->words()),
43+
'service' => 'faker',
44+
'ping' => Arr::get($output, 'ping.latency'),
45+
'download' => Arr::get($output, 'download.bandwidth'),
46+
'upload' => Arr::get($output, 'upload.bandwidth'),
47+
'data' => $output,
48+
'status' => ResultStatus::Completed,
49+
'scheduled' => false,
2850
];
2951
}
3052
}

0 commit comments

Comments
 (0)