Skip to content

addition: Generic instance factory#17

Merged
remipelhate merged 2 commits into
mainfrom
addition/generic-instance-factory
Aug 29, 2025
Merged

addition: Generic instance factory#17
remipelhate merged 2 commits into
mainfrom
addition/generic-instance-factory

Conversation

@remipelhate

Copy link
Copy Markdown
Member

When setting up factories for classes with a non-public default constructor, we have to pick one of the named constructors and add some additional logic to ensure all attributes defined by the factory are properly applied to the created instance. The whole point of testing factories is to eliminate "complex" logic while creating instances, so this felt a bit off.

This PR introduces Craftzing\TestBench\Factories\InstanceFactory which uses reflection to completely bypass constructor limitations while creating instances:

final class UserFactory extends ImmutableFactory
{
    protected function instance(array $attributes): User
    {
        return new InstanceFactory(User::class)->make($attributes);
    }
}

@remipelhate remipelhate self-assigned this Aug 29, 2025
@remipelhate remipelhate requested a review from fowbi August 29, 2025 09:01
@remipelhate remipelhate merged commit c37fcd3 into main Aug 29, 2025
4 checks passed
@remipelhate remipelhate deleted the addition/generic-instance-factory branch August 29, 2025 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants