From 826d4017fa8d43f3babf3c54e1ab5e5275351ae9 Mon Sep 17 00:00:00 2001
From: Sam Light <sam@lightscale.co.uk>
Date: Sat, 19 Apr 2025 11:54:58 +0100
Subject: Setup a load of factory helper methods

---
 database/factories/AccessLogFactory.php | 81 +++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)

(limited to 'database/factories')

diff --git a/database/factories/AccessLogFactory.php b/database/factories/AccessLogFactory.php
index 1235b33..667ad47 100644
--- a/database/factories/AccessLogFactory.php
+++ b/database/factories/AccessLogFactory.php
@@ -10,4 +10,85 @@ class AccessLogFactory extends Factory
     {
         return config('access_log.model');
     }
+
+    protected static function fakeUri(
+        int $maxSegments = 2,
+        int $maxWords = 3,
+    ): string
+    {
+        $segsAmount = fake()->numberBetween(0, $maxSegments);
+
+        $segs = [];
+        for ($i = 0; $i < $segsAmount; $i++) {
+            $wordsAmount = fake()->numberBetween(1, $maxWords);
+            $segs[] = fake()->slug($wordsAmount, false);
+        }
+
+        return '/' . implode('/', $segs);
+    }
+
+    public function definition(): array
+    {
+        return [
+            'method' => 'GET',
+            'path' => static::fakeUri(),
+            'status' => 200,
+        ];
+    }
+
+    public function fakePath(
+        int $maxSegments = 2,
+        int $maxWords = 3
+    ): static
+    {
+        return $this->state(fn() => [
+            'path' => static::fakeUri($maxSegments, $maxWords)
+        ]);
+    }
+
+    protected static function makeRoute(string $name, mixed $params = []): string
+    {
+        return route($name, $params, false);
+    }
+
+    public function path(callable|string $path): static
+    {
+        return $this->state(fn($state) => [
+            'path' => is_callable($path) ? $path($state) : $path
+        ]);
+    }
+
+    public function route(callable|string $name, mixed $params = []): static
+    {
+        return $this->path(
+            is_callable($name) ?
+            fn($state) => static::makeRoute(...$name($state)) :
+            static::makeRoute($name, $params)
+        );
+    }
+
+    public function paths(array $paths, bool $random = false): static
+    {
+        return $this->sequence(
+            $random ?
+            fn() => ['path' => $paths[array_rand($paths)]] :
+            array_map(fn($p) => ['path' => $p], $paths)
+        );
+    }
+
+    public function routes(array $routes, bool $random = false): static
+    {
+        return $this->paths(
+            array_map(fn($r) => static::makeRoute($r[0], $r[1]), $routes),
+            $random
+        );
+    }
+
+    public function properties(callable|array $props): static
+    {
+        return $this->state(fn($state) => [
+            'properties' => is_callable($props) ? $props($state) : $props
+        ]);
+    }
+
 }
-- 
cgit v1.2.3