thephpleague / factory-muffin

Enables the rapid creation of objects for testing
https://factory-muffin.thephpleague.com/
MIT License
531 stars 72 forks source link

Share model object between definitions #393

Closed dutchiexl closed 8 years ago

dutchiexl commented 9 years ago

I am building an application with 3 models:

Because a program is not needed to create a certification, both the Certification as the Program have a relation with participant. This is the same participant.

I created my definitions:

$fm->define('App\Models\Certification')->setDefinitions(
    [
        'type'           => 'animator',
        'number'         => Faker::randomNumber(8),
        'participant_id' => 'factory|App\Models\Participant',
        'issuer_id'      => 'factory|App\Models\User',
        'program_id'     => 'factory|App\Models\Program',
        'issued_by_tool' => false
    ]
);

$fm->define('App\Models\Participant')->setDefinitions(
    [
        'user_id'                       => 'factory|App\Models\User',
        'kavo_id'                       => Faker::randomNumber(8),
        'birth_date'                    => Faker::dateTimeThisCentury(),
        'email_secondary'               => Faker::email(),
        'requires_national_registry_nr' => false,
        'national_registry_nr'          => null,
        'portfolio'                     => null,

    ]
);

$fm->define('App\Models\Program')->setDefinitions(
    [
        'type'                  => 'animator',
        'participant_id'        => 'factory|App\Models\Participant',
        'finished_successfully' => false,
        'active'                => true
    ]
);

for both the program as the certification, a participant will be created. I would like to pass them the same participant. Is this possible?

saada commented 9 years ago

The way I work around this is by writing

$participant = $fm->create('App\Models\Particiant');
$program = $fm->create('App\Models\Program', 
     ['participant_id' => $participant->id]);
$certification = $fm->create('App\Models\Certification', 
     ['participant_id' => $participant->id, 'program_id' => $program->id]);
newradius commented 4 years ago

Is there any way to share models between definitions in v3.0.1? I can't find any example.