Skip to content

Commit

Permalink
added factories and seeders
Browse files Browse the repository at this point in the history
  • Loading branch information
romo4ko committed Oct 26, 2024
1 parent e9fd436 commit e3db8dd
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 13 deletions.
14 changes: 14 additions & 0 deletions app/Enums/ChallengeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Enums;

enum ChallengeType: string
{
case PERSONAL = 'personal';
case TEAM = 'team';

public static function values(): array
{
return array_map(fn (self $case) => $case->value, self::cases());
}
}
2 changes: 2 additions & 0 deletions app/Models/Achievement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Achievement extends Model
{
use HasFactory;

public $timestamps = true;

protected $fillable = ['name', 'description', 'image_id'];

public function challenges(): BelongsToMany
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Challenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class Challenge extends Model
{
use HasFactory;

public $timestamps = true;

protected $fillable = [
'name',
'description',
'start_date',
'end_date',
'result',
'created_at',
'achievement_id',
'image_id',
'type'
Expand Down
1 change: 1 addition & 0 deletions app/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Image extends Model
{
use HasFactory;

public $timestamps = true;
protected $fillable = ['image_path'];

public function users(): hasMany
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Team extends Model
{
use HasFactory;

public $timestamps = true;

protected $fillable = [
'name',
'description',
Expand Down
2 changes: 2 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class User extends Authenticatable
* @var array<int, string>
*/

public $timestamps = true;

protected $fillable = [
'name',
'surname',
Expand Down
39 changes: 39 additions & 0 deletions config/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

return [
'achievements' => [
[
'name' => 'Достижение 1',
'description' => 'Достижение 2',
'image_id' => null
],
[
'name' => 'Достижение 1',
'description' => 'Достижение 2',
'image_id' => null
],
[
'name' => 'Достижение 1',
'description' => 'Достижение 2',
'image_id' => null
],
],

'challenges' => [
[
'name' => 'Задание 1',
'description' => 'Задание 2',
'image_id' => null
],
[
'name' => 'Задание 1',
'description' => 'Задание 2',
'image_id' => null
],
[
'name' => 'Задание 1',
'description' => 'Задание 2',
'image_id' => null
],
],
];
31 changes: 31 additions & 0 deletions database/factories/ChallengeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Enums\ChallengeType;
use App\Models\Achievement;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;

class ChallengeFactory extends Factory
{
public function definition(): array
{
$now = Carbon::now();

return [
'name' => fake()->text(20),
'description' => fake()->text(),
'start_date' => $now,
'end_date' => $now->addDays(fake()->numberBetween(1, 30)),
'achievement_id' => Achievement::query()->inRandomOrder()->first(),
'image_id' => null,
'type' => fake()->randomElement([
ChallengeType::PERSONAL,
ChallengeType::TEAM
])
];
}
}
5 changes: 4 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ public function definition(): array
{
return [
'name' => fake()->name(),
'surname' => fake()->lastName(),
'about' => fake()->text(),
'is_confirmed' => true,
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'password' => Hash::make('password'),
'remember_token' => Str::random(10),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up(): void
$table->id();
$table->string('name');
$table->string('surname');
$table->string('patronymic');
$table->string('patronymic')->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
Expand Down
20 changes: 20 additions & 0 deletions database/seeders/AchievementsSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\Achievement;
use Illuminate\Database\Seeder;

class AchievementsSeeder extends Seeder
{
public function run(): void
{
$achievements = config('base.achievements');

foreach ($achievements as $achievement) {
Achievement::query()->create($achievement);
}
}
}
20 changes: 20 additions & 0 deletions database/seeders/ChallengesSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\Challenge;
use Illuminate\Database\Seeder;

class ChallengesSeeder extends Seeder
{
public function run(): void
{
$challenges = config('base.challenges');

foreach ($challenges as $challenge) {
Challenge::factory()->create($challenge);
}
}
}
16 changes: 6 additions & 10 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@

namespace Database\Seeders;

use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call(
[
TeamsSeeder::class
]
);
$this->call([
UsersSeeder::class,
AchievementsSeeder::class,
ChallengesSeeder::class,
TeamsSeeder::class
]);
}
}
22 changes: 22 additions & 0 deletions database/seeders/UsersSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;

class UsersSeeder extends Seeder
{
public function run(): void
{
User::factory(10)->create();

User::factory()->create([
'name' => 'Admin',
'email' => '[email protected]',
'is_admin' => true,
]);
}
}

0 comments on commit e3db8dd

Please sign in to comment.