Skip to content
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

Open
wants to merge 8 commits into
base: epic/campaigns
Choose a base branch
from

Conversation

kjohnson
Copy link
Member

@kjohnson kjohnson commented Nov 15, 2024

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 are callable.

Visuals

public function definition(): array
{
    return [
        'title' => __('GiveWP Campaign', 'give'),
        'formId' => DonationForm::factory()->createAndResolveTo('id'),
    ];
}

Testing Instructions

This feature is covered by unit tests and does not have a customer facing application.

Pre-review Checklist

  • Relevant @unreleased tags included in DocBlocks
  • Includes unit tests
  • Self Review of code and UX completed

@kjohnson kjohnson marked this pull request as ready for review November 15, 2024 20:41
Copy link
Contributor

@JasonTheAdams JasonTheAdams left a 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! 😄

src/Framework/Models/Factories/ModelFactory.php Outdated Show resolved Hide resolved
Comment on lines 97 to 125
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)));
Copy link
Contributor

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.

Copy link
Member Author

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);
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved, see dd89630.

Copy link
Contributor

@JasonTheAdams JasonTheAdams left a 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.

@kjohnson
Copy link
Member Author

Would you be willing, on your next Fun Friday, to add this to the StellarWP library?

Yeah, that sounds like a good next step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants