lukeraymonddowning / poser

Create class based model factories in Laravel applications in seconds.
MIT License
277 stars 10 forks source link

Poser defaults #52

Closed lukeraymonddowning closed 4 years ago

lukeraymonddowning commented 4 years ago

After using Poser for a while, you may wish to provide default relationships that should be applied every time we create a model. For example, in our examples, a Customer requires a related User model. To facilitate this, Poser understands a default[RelationshipType][RelationshipMethodName] syntax inside Poser Factories. To illustrate:

class CustomerFactory extends Factory
{

    public function defaultForUser()
    {
        return UserFactory::new()->withAttributes(["name" => "Joe Bloggs"]);
    }

}

Now, every time we call...

CustomerFactory::new()->create();

...a User called Joe Bloggs will be automatically assigned to the customer. However, if we call...

CustomerFactory::new()->withUser(UserFactory::new()->withAttributes(["name" => "John Doe"]))->create();

...the default will be ignored and instead a User called "John Doe" will be assigned to the Customer.

For with/has relationship types, the case is much the same:

class UserFactory extends Factory
{

    public function defaultWithAddress() 
    {
        return AddressFactory::new();
    }

    public function defaultHasCustomers()
    {
        return CustomerFactory::times(10);
    }

}

In this case, when we call UserFactory::new()->create(), it will be given an Address and 10 Customers.

If we would like to ignore your defaults for a given test, simply chain the withoutDefaults method to your Factory. call.

UserFactory::new()->withoutDefaults()->create();

This will create a User with no Address or Customers. If you would only like to ignore certain defaults, you may pass them as properties to the withoutDefaults method.

UserFactory::new()->withoutDefaults('customers')->create();

Now, our created User will have no Customers but will have a default Address.

lukeraymonddowning commented 4 years ago

@bradroberts @AlanHolmes @andreich1980

Here is my work on defaults for issue #50

A lot of changes here so I would appreciate feedback and suggestions 👍

andreich1980 commented 4 years ago

Read through the code. Everything seems fine. Created a test for that case.

This PR should also fix a problem described in #42 Do I have rights to commit to this PR (I'd like to add a test for that)? Or I should create my own new PR for that? @lukeraymonddowning ?

lukeraymonddowning commented 4 years ago

Do I have rights to commit to this PR (I'd like to add a test for that)? Or I should create my own new PR for that? @lukeraymonddowning ?

I think you should have rights here @andreich1980 👍

AlanHolmes commented 4 years ago

Code looks good to me 👍 (not had chance to test it)

andreich1980 commented 4 years ago

Conflicting files tests/storage/composer.json @lukeraymonddowning you can leave only "autoload" and throw out "autoload-dev" from the tests/storage/composer.json My bad, I commited two versions via two my PRs.

lukeraymonddowning commented 4 years ago

And we have liftoff! Thanks all.