Commit 98b8e04d authored by Laravel Shift's avatar Laravel Shift
Browse files

Shift to class based factories

parent 78fc19cc
......@@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Casts\EncryptedString;
use DateInterval;
use DateTime;
......@@ -10,6 +11,8 @@ use Illuminate\Database\Eloquent\Model;
class Draw extends Model
{
use HasFactory;
// Remove everything N weeks after the expiration_date
const WEEKS_BEFORE_DELETION = 3;
......
......@@ -2,12 +2,15 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bus\DispatchesJobs;
class Mail extends Model
{
use HasFactory;
use HashId, DispatchesJobs;
protected static $hashConnection = 'bounce';
......
......@@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Casts\EncryptedString;
use App\Collections\ParticipantsCollection;
use Illuminate\Database\Eloquent\Model;
......@@ -9,6 +10,8 @@ use Illuminate\Notifications\Notifiable;
class Participant extends Model
{
use HasFactory;
use Notifiable;
use HashId {
resolveRouteBinding as public baseResolver;
......
use Illuminate\Database\Eloquent\Factories\Factory;
namespace Database\Factories;
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
$factory->define(App\Models\Draw::class, function (Faker\Generator $faker) {
return [
'mail_title' => $faker->sentence,
'mail_body' => $faker->text,
'expires_at' => $faker->dateTimeBetween('+1 day', '+1 month'),
class DrawFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Draw::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'mail_title' => $this->faker->sentence,
'mail_body' => $this->faker->text,
'expires_at' => $this->faker->dateTimeBetween('+1 day', '+1 month'),
];
});
}
$factory->state(App\Models\Draw::class, 'expired', function ($faker) {
return [
'expires_at' => $faker->dateTime('-1 hour'),
public function expired()
{
return $this->state(function () {
return [
'expires_at' => $this->faker->dateTime('-1 hour'),
];
});
});
}
}
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
$factory->define(App\Models\Mail::class, function (Faker\Generator $faker) {
return [
class MailFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Mail::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'delivery_status' => App\Models\Mail::CREATED,
'draw_id' => factory(App\Models\Draw::class),
'version' => $faker->numberBetween(0, 255),
'draw_id' => \App\Models\Draw::factory(),
'version' => $this->faker->numberBetween(0, 255),
];
});
}
}
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/*
|--------------------------------------------------------------------------
......
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
$factory->define(App\Models\Participant::class, function (Faker\Generator $faker) {
return [
'draw_id' => factory(App\Models\Draw::class),
'name' => $faker->name,
'email' => $faker->email,
class ParticipantFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \App\Models\Participant::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'draw_id' => \App\Models\Draw::factory(),
'name' => $this->faker->name,
'email' => $this->faker->email,
'target_id' => null,
'mail_id' => factory(App\Models\Mail::class),
'mail_id' => \App\Models\Mail::factory(),
];
});
}
}
......@@ -12,7 +12,7 @@ class CleanupTest extends RequestCase
public function testDrawNotExpired(): void
{
$this->assertEquals(0, Draw::count());
$draw = factory(Draw::class)->create();
$draw = Draw::factory()->create();
$this->assertEquals(1, Draw::count());
$this->assertDatabaseHas('draws', ['id' => $draw->id]);
......@@ -25,7 +25,7 @@ class CleanupTest extends RequestCase
public function testDrawExpired(): void
{
$this->assertEquals(0, Draw::count());
$draw = factory(Draw::class)->states('expired')->create();
$draw = Draw::factory()->expired()->create();
$this->assertEquals(1, Draw::count());
$this->assertDatabaseHas('draws', ['id' => $draw->id]);
......
......@@ -15,9 +15,9 @@ class RequestOrganizerTest extends RequestCase
{
$this->assertGreaterThan(1, $participants);
$draw = factory(Draw::class)->create();
$draw = Draw::factory()->create();
$draw->participants()->createMany(
factory(Participant::class, $participants)->make()->toArray()
Participant::factory()->count($participants)->make()->toArray()
);
foreach ($draw->participants as $idx => $participant) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment