thephpleague / factory-muffin

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

Allow For More Customisation #286

Closed GrahamCampbell closed 10 years ago

GrahamCampbell commented 10 years ago

This is just a proof of concept for #285. Provided that this solves @dimrsilva's issue, and @scottrobertson is ok with this, I'll add tests and update the documentation.

scottrobertson commented 10 years ago

I like :)

scottrobertson commented 10 years ago

Maybe custom getter too while we are at it?

GrahamCampbell commented 10 years ago

Where do we get the attributes?

scottrobertson commented 10 years ago

Ignore me, I just at a large popcorn at the cinima, I am probably drunk on sugar.

GrahamCampbell commented 10 years ago

Lol, no problem. I'll write up some docs and tests for this then. :)

GrahamCampbell commented 10 years ago

@scottrobertson While you're here, what do you think about #287?

scottrobertson commented 10 years ago

Just replied - I like it.

GrahamCampbell commented 10 years ago

@scottrobertson Final call before I merge this.

GrahamCampbell commented 10 years ago

I'll leave this till tomorrow to give you time to review it.

spencerdeinum commented 10 years ago

I'm not sure if I understand this fully but is the custom maker global?

I'm looking for a way to define a custom maker for certain classes.

I previously added #34 because for some of our models our criteria for setting their attributes is a bit complex, ie some fields can only be entered when others exist.

This means that our factory methods sometimes randomly generate an attribute, and then depending on that attribute might generate more data.

I think it would be useful to be able to say

League\FactoryMuffin\Facade::defineFactory('MyModel', function ($model, $faker) {
  // Fill in model attributes with faker
  return $model;
})

Or maybe even this as a short hand...

League\FactoryMuffin\Facade::defineFactory('MyModel', array $attrs = [], function ($model, $faker) {
 // $attrs already set as normal on model, but this closure can customize some additional attrs
  return $model;
})

We are still using the old package for now but this is something we'd need to be able to upgrade.

I thought I would ask for some feedback before writing a pull request though.

scottrobertson commented 10 years ago

@spencerdeinum that callback already exists on the ::define method, so you can already do what you are asking. (See #277). We are working on making faker available inside that callback.

spencerdeinum commented 10 years ago

Ah, I think Github was defaulting to the 2.0 release.

That is great, I really like the new version of this package guys, thanks for all your hard work :)

scottrobertson commented 10 years ago

@spencerdeinum it is defaulting to 2.0 yeah :)

and thank you very much. More to come hopefully.

GrahamCampbell commented 10 years ago

Feel free to checkout our upgrading guide to our 2.1 dev version over on the master branch. We make sure the docs are always kept up to date. There are no BC breaks anyway - the guide mainly introduces new things and highlights a few querks.

You can install it by requiring:

{
    "require-dev": {
        "league/factory-muffin": "2.1.*@dev"
    }
}
dimrsilva commented 10 years ago

@GrahamCampbell, just a detail, what do you think of using call_user_func($setter) instead $setter(...)? That way, I can pass an closure, an array with object/method or a function name.

GrahamCampbell commented 10 years ago

If you really need it, I can change it. I just chose the other way for performance reasons. You'd notice a slow down if you were seeding 1,000 times for example.

dimrsilva commented 10 years ago

Ok, I do not really need it. And if I did, I could call my functions inside a closure...

GrahamCampbell commented 10 years ago

This is something to consider for 3.0. Maybe we could change the "closure" generator to a "callable" generator too.