thephpleague / factory-muffin

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

[2.1] Closure Callback doesn't appear to do anything #381

Closed kennonb closed 9 years ago

kennonb commented 9 years ago

So I have a pivot table I'm trying seed using the method described in this issue (#272).

I'm thinking I have to be doing something wrong, since the callback doesn't appear to ever fire. So I feel like I have to be missing something. I'll try to outline what I've done so far.

Setup all.php definitions

FactoryMuffin::define( 'Office', array(
    'name' => 'country',
    'iso_code' => 'countryCode',
    'created_by' => 'factory|User',
    'updated_by' => 'factory|User'
) );

FactoryMuffin::define( 'User', array(
    'email' => 'email|unique',
    'password' => 'word',
    'first_name' => 'firstName',
    'last_name' => 'lastName',
) );

FactoryMuffin::define( 'Email', array(
    'title' => 'sentence|4',
    'is_valid' => 'boolean',
    'is_tested' => 'boolean',
    'is_approved' => 'boolean',
    'created_by' => 'factory|User',
    'updated_by' => 'factory|User',
),
    function ( $object, $saved ) {
        $office_id = FactoryMuffin::create( 'Office' )->id;
        $object->offices()->attach( $office_id );
    }
);

Then in my test, I'm creating using the Factory facade

League\FactoryMuffin\Facade::create( 'Email' );

This creates the Email and everything defined in that table, but the callback never fires and the office data is never attached to the Email.

Am I doing something stupid? Or do you need a more fleshed out test case?

Any help is appreciated. :)

GrahamCampbell commented 9 years ago

Hmmm, this is odd. Our tests show that the callback stuff is working.

Could you send a pull request to add a failing test to https://github.com/thephpleague/factory-muffin/blob/2.1/tests/EloquentTest.php please so I can investigate.

kennonb commented 9 years ago

Thanks for the suggestion.

I created a few small test cases on top of what you already have in the repo. They pass locally, and data is created from the callback, so I must have an issue somewhere else.