thephpleague / factory-muffin

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

Closure generator doesn't pass model with previous attributes set #329

Closed eblanshey closed 10 years ago

eblanshey commented 10 years ago

You have the following example in the docs:

League\FactoryMuffin\Facade::define('MyModel', array(
    'title' => 'sentence|5',
    'slug' => function ($object, $saved) {
        $slug = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $object->title);
        $slug = strtolower(trim($slug, '-'));
        $slug = preg_replace("/[\/_|+ -]+/", '-', $slug);

        return $slug;
    },
));

It's assuming that, in the closure, $object->title will refer to the title which was just previously generated, but that is not the case, as the attributes have not been set on the model at this point.

In the attributesFor() method, you are assigning the attributes to an array, not the object itself.

GrahamCampbell commented 10 years ago

This is a known issue, and is fixed in the upcoming 3.0 release.

eblanshey commented 10 years ago

Ah, I wasn't aware. Any rough timeframe available?

GrahamCampbell commented 10 years ago

Yeh: https://github.com/thephpleague/factory-muffin/milestones.

GrahamCampbell commented 10 years ago

You can use the callback functionality in 2.1 if you need access to the previously set attributes if you really need them btw. Otherwise you could try out 3.0 using the version constraint 3.0.*@dev.