Skip to content

Commit

Permalink
Factory time
Browse files Browse the repository at this point in the history
- Added factories for each model
- Added Builder mixin to various models
  • Loading branch information
FloKnapp committed Nov 20, 2024
1 parent 7b3c058 commit b359521
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/Models/Episode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
Expand All @@ -14,6 +15,9 @@
*/
class Episode extends Model
{

use HasFactory;

protected $fillable = [
'user_id',
'public_id',
Expand Down
4 changes: 4 additions & 0 deletions app/Models/EpisodeTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
Expand All @@ -14,6 +15,9 @@
*/
class EpisodeTrigger extends Model
{

use HasFactory;

protected $fillable = [
'name',
];
Expand Down
3 changes: 3 additions & 0 deletions app/Models/EpisodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
Expand All @@ -15,6 +16,8 @@
class EpisodeType extends Model
{

use HasFactory;

protected $fillable = [
'name',
];
Expand Down
12 changes: 12 additions & 0 deletions app/Models/Symptom.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* Class Symptom
* @package App\Models
* @author Florian Knapp <office@florianknapp.de>
*
* @mixin Builder
*/
class Symptom extends Model
{

use HasFactory;

protected $fillable = [
'name',
];
Expand Down
8 changes: 8 additions & 0 deletions app/Models/SymptomTiming.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
* Class SymptomTiming
* @package App\Models
* @author Florian Knapp <office@florianknapp.de>
*
* @mixin Builder
*/
class SymptomTiming extends Model
{

Expand Down
12 changes: 12 additions & 0 deletions app/Models/Timing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* Class Timing
* @package App\Models
* @author Florian Knapp <office@florianknapp.de>
*
* @mixin Builder
*/
class Timing extends Model
{

use HasFactory;

protected $fillable = [
'name'
];
Expand Down
59 changes: 57 additions & 2 deletions database/factories/EpisodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
namespace Database\Factories;

use App\Models\Episode;
use App\Models\EpisodeSymptom;
use App\Models\EpisodeTrigger;
use App\Models\EpisodeType;
use App\Models\Symptom;
use App\Models\SymptomTiming;
use App\Models\Timing;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
Expand All @@ -22,20 +29,68 @@ public function definition(): array
{
return [
'public_id' => $this->faker->uuid(),
'user_id' => $this->faker->numberBetween(1, 255),
'user_id' => User::factory()->create()->id,
'state' => $this->faker->randomElement(['published', 'draft']),
'duration' => $this->faker->numberBetween(0, 600),
'intensity' => $this->faker->numberBetween(0, 10),
'published_at' => $this->faker->dateTimeBetween('-1 years'),
];
}

public function configure()
public function configure(): EpisodeFactory|Factory
{
return $this->afterCreating(function (Episode $episode) {

$this->createBaseData();

// Attach type
$type = EpisodeType::inRandomOrder()->take(1)->get();
$episode->types()->attach($type);

// Attach symptoms
$symptoms = Symptom::take(2)->get();

foreach ($symptoms as $symptom) {

EpisodeSymptom::firstOrCreate([
'episode_id' => $episode->id,
'symptom_id' => $symptom->id
]);

$timing = Timing::inRandomOrder()->take(1)->get()->first();

SymptomTiming::firstOrCreate([
'symptom_id' => $symptom->id,
'timing_id' => $timing->id
]);

}

// Attach trigger
$triggers = EpisodeTrigger::inRandomOrder()->take(2)->get();
$episode->triggers()->attach($triggers);

});
}

/**
* @return void
*/
private function createBaseData(): void
{
EpisodeType::firstOrCreate(['name' => 'absence']);
EpisodeType::firstOrCreate(['name' => 'tonic']);

Symptom::firstOrCreate(['name' => 'aura']);
Symptom::firstOrCreate(['name' => 'unconsciousness']);

Timing::firstOrCreate(['name' => 'pre']);
Timing::firstOrCreate(['name' => 'during']);
Timing::firstOrCreate(['name' => 'post']);

EpisodeTrigger::firstOrCreate(['name' => 'menstruation']);
EpisodeTrigger::firstOrCreate(['name' => 'sleep_deprivation']);
EpisodeTrigger::firstOrCreate(['name' => 'nothing']);

}
}
24 changes: 24 additions & 0 deletions database/factories/EpisodeTypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use App\Models\EpisodeType;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<EpisodeType>
*/
class EpisodeTypeFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->randomElement(['absence', 'tonic'])
];
}
}
24 changes: 24 additions & 0 deletions database/factories/SymptomFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use App\Models\Symptom;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Symptom>
*/
class SymptomFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->randomElement(['aura', 'unconsciousness'])
];
}
}
24 changes: 24 additions & 0 deletions database/factories/TimingFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use App\Models\Timing;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Timing>
*/
class TimingFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->randomElement(['pre', 'during', 'post']),
];
}
}
24 changes: 24 additions & 0 deletions database/factories/TriggerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use App\Models\EpisodeTrigger;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<EpisodeTrigger>
*/
class TriggerFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->randomElement(['menstruation', 'sleep_deprivation']),
];
}
}
2 changes: 1 addition & 1 deletion resources/views/episode/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="p-6 text-gray-900 dark:text-gray-100" x-data="{currentEpisodeId: null}">

@php
$episodeCount = count($episodes);
$episodeCount = count($episodes ?? 0);
@endphp

<x-headline-primary>
Expand Down

0 comments on commit b359521

Please sign in to comment.