Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Korko
SecretSanta.fr
Commits
98b8e04d
Commit
98b8e04d
authored
Jan 03, 2021
by
Laravel Shift
Browse files
Shift to class based factories
parent
78fc19cc
Changes
10
Hide whitespace changes
Inline
Side-by-side
app/Models/Draw.php
View file @
98b8e04d
...
...
@@ -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
;
...
...
app/Models/Mail.php
View file @
98b8e04d
...
...
@@ -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'
;
...
...
app/Models/Participant.php
View file @
98b8e04d
...
...
@@ -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
;
...
...
database/factories/DearSantaFactory.php
View file @
98b8e04d
use
Illuminate\Database\Eloquent\Factories\Factory
;
namespace
Database\Factories
;
database/factories/DrawFactory.php
View file @
98b8e04d
<?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'
),
];
});
});
}
}
database/factories/MailFactory.php
View file @
98b8e04d
<?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
),
];
});
}
}
database/factories/ModelFactory.php
View file @
98b8e04d
<?php
namespace
Database\Factories
;
use
Illuminate\Database\Eloquent\Factories\Factory
;
/*
|--------------------------------------------------------------------------
...
...
database/factories/ParticipantFactory.php
View file @
98b8e04d
<?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
(
),
];
});
}
}
tests/Feature/CleanupTest.php
View file @
98b8e04d
...
...
@@ -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
]);
...
...
tests/Feature/RequestOrganizerTest.php
View file @
98b8e04d
...
...
@@ -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
)
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment