-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Add deferred resolution to model factory definitions #7626
base: epic/campaigns
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, @kjohnson! A couple little suggestions! 😄
return new $this->model(array_merge($this->definition(), $attributes)); | ||
return new $this->model(array_map(function($attribute) { | ||
return is_callable($attribute) ? $attribute() : $attribute; | ||
}, array_merge($this->definition(), $attributes))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the is_callable()
is too generic? For example, model properties can be a class (say a VO), and if that class instance is invokable, then that would pass as is_callable()
in which case it would be called here unintentionally.
I think it's safer to use $attribute instanceof Closure
instead. I can't think of a time wherein a model property would be a closure, as that's not something persistable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I added a test to confirm and the test failed. Updating the resolver to check for a Closure
type passed the test.
public function testDoesNotResolveInvokableClasses()
{
$factory = new class(MockModelWithInvokableProperty::class) extends ModelFactory {
public function definition(): array {
return [
'invokable' => new class extends MockInvokableClass {
public function __invoke() {
throw new \Exception('Invokable classes should not be resolved by factories.');
}
},
];
}
};
$object = $factory->make();
$this->assertInstanceOf(MockInvokableClass::class, $object->invokable);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved, see dd89630.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work, @kjohnson! 👍
Would you be willing, on your next Fun Friday, to add this to the StellarWP library? We really need to move over to that library as to avoid drifting apart.
Yeah, that sounds like a good next step. |
Description
This PR adds deferred resolution to model factory definitions, such as nested model factories, so that defaults can be overridden without instantiating unused model instances.
Affects
Updates
Give\Framework\Models\Factories\ModelFactory::makeInstance()
to support resolving attributes that arecallable
.Visuals
Testing Instructions
This feature is covered by unit tests and does not have a customer facing application.
Pre-review Checklist
@unreleased
tags included in DocBlocks