Skip to content

Commit

Permalink
Merge pull request #79 from musimana/feature/FactoryReturnTypes
Browse files Browse the repository at this point in the history
feat: Factory Return Types
  • Loading branch information
musimana authored May 28, 2024
2 parents 609599f + 9c79add commit d9c75f2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion database/factories/BlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class BlockFactory extends Factory
/**
* Define the model's default state.
*
* @return array<string, mixed>
* @return array<string, int|null|string>
*/
public function definition(): array
{
Expand Down
2 changes: 1 addition & 1 deletion database/factories/NavbarFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class NavbarFactory extends Factory
/**
* Define the model's default state.
*
* @return array<string, mixed>
* @return array<string, string>
*/
public function definition(): array
{
Expand Down
2 changes: 1 addition & 1 deletion database/factories/NavbarItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class NavbarItemFactory extends Factory
/**
* Define the model's default state.
*
* @return array<string, mixed>
* @return array<string, int|string>
*/
public function definition(): array
{
Expand Down
32 changes: 15 additions & 17 deletions database/factories/PageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@ final class PageFactory extends Factory
/**
* Define the model's default state.
*
* @return array<string, mixed>
* @return array<string, string>
*/
public function definition(): array
{
/** @var string $title */
$title = fake()->words(3, true);

/** @var string $subtitle */
$subtitle = fake()->words(5, true);

/** @var string $meta_description */
$meta_description = fake()->words(24, true);

/** @var array<int, string> $content_array */
$content_array = fake()->paragraphs(5);

return [
'slug' => urlencode(str_replace(' ', '-', $title)),
'title' => ucwords($title),
'subtitle' => fake()->words(5, true),
'subtitle' => $subtitle,
'content' => '<p>' . implode('</p><p>', $content_array) . '</p>',
'meta_title' => ucwords($title),
'meta_description' => fake()->words(24, true),
'meta_description' => $meta_description,
'template' => WebpageTemplate::PUBLIC_CONTENT->value,
];
}

/**
* Indicate that the model should match the project's default About page.
*
* @return static
*/
public function aboutPage()
/** Indicate that the model should match the project's default About page. */
public function aboutPage(): static
{
return $this->state(fn (array $attributes) => array_merge([
...$attributes,
Expand All @@ -52,12 +54,8 @@ public function aboutPage()
]));
}

/**
* Indicate that the model should match the project's default homepage.
*
* @return static
*/
public function homePage()
/** Indicate that the model should match the project's default homepage. */
public function homePage(): static
{
return $this->state(fn (array $attributes) => array_merge([
...$attributes,
Expand All @@ -68,8 +66,8 @@ public function homePage()
'meta_title' => config('app.name'),
'meta_description' => config('metadata.description'),
'template' => WebpageTemplate::PUBLIC_INDEX->value,
'in_sitemap' => 0,
'is_homepage' => 1,
'in_sitemap' => false,
'is_homepage' => true,
]));
}
}
12 changes: 7 additions & 5 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class UserFactory extends Factory
public function definition(): array
{
return [
'is_admin' => 0,
'is_admin' => false,
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
Expand All @@ -32,16 +32,18 @@ public function definition(): array
/** Set admin privileges for the model. */
public function isAdmin(): static
{
return $this->state(fn (array $attributes) => [
return $this->state(fn (array $attributes) => array_merge([
...$attributes,
'is_admin' => true,
]);
]));
}

/** Indicate that the model's email address should be unverified. */
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
return $this->state(fn (array $attributes) => array_merge([
...$attributes,
'email_verified_at' => null,
]);
]));
}
}
7 changes: 3 additions & 4 deletions database/seeders/UserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public function run(): void
$primary_user = User::where([['email', config('mail.from.address')]])->first();

if (!$primary_user) {
User::factory()->create([
'is_admin' => 1,
'name' => config('mail.from.name'),
'email' => config('mail.from.address'),
User::factory()->isAdmin()->create([
'name' => config('mail.from.name', 'admin'),
'email' => config('mail.from.address', 'admin@example.com'),
]);
}

Expand Down

0 comments on commit d9c75f2

Please sign in to comment.