diff options
| author | Sam Light <sam@lightscale.co.uk> | 2025-04-19 11:54:58 +0100 | 
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2025-04-19 11:54:58 +0100 | 
| commit | 826d4017fa8d43f3babf3c54e1ab5e5275351ae9 (patch) | |
| tree | 1274ba229f978664f0057a20209997011f7ee470 /database/factories | |
| parent | fabeedbd092ba58d14af05ced1e23a3790449363 (diff) | |
Setup a load of factory helper methodsv1.1.0
Diffstat (limited to 'database/factories')
| -rw-r--r-- | database/factories/AccessLogFactory.php | 81 | 
1 files changed, 81 insertions, 0 deletions
| 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 +        ]); +    } +  } | 
