laracasts / TestDummy

Easy factories for PHP integration testing.
https://laracasts.com/lessons/whats-new-in-testdummy
MIT License
457 stars 80 forks source link

[2.0] Recursion error when factory creates relationship on itself #67

Open amochohan opened 9 years ago

amochohan commented 9 years ago

TestDummy is unable to handle creating a dummy record when the record requires a mock of itself for a relationship.

An example:

A User model can must be created by a user (think of a customer relationship management system, a new customer contact record is created, but the business wants to log who has created, or owns that user account).

The following code can be used to replicate the issue:

$factory('App\User', [
    'uuid'      => $faker->uuid,
    'name'      => $faker->name,
    'email'     => $faker->email,
    'created_by_id' => 'factory:App\User'
]);

Ideally, if there is only one User being created that has a self relationship, it should set the id the the id that has been inserted.

JeffreyWay commented 9 years ago

What happens when you run this? I would think it would allow that...

JeffreyWay commented 9 years ago

Ahh, yeah didn't see the title of this issue. Yeah, there would be a recursion issue, wouldn't there...

Hmm - I'll take a look.

devinfd commented 9 years ago

Perhaps I'm wrong but couldn't the created_by_id just reference a different App\User factory that has been name-spaced to something like UserCreator? In the UserCreator factory just leave the created_by_id field blank?