Skip to content

[12.x] Allow globally disabling Factory parent relationships #56154

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

Open
wants to merge 3 commits into
base: 12.x
Choose a base branch
from

Conversation

cosmastech
Copy link
Contributor

A continuation of #53450. This feature was added by @browner12 and it's been incredibly helpful for our team in speeding up tests.

That said, it requires us to write ->withoutParents() on each factory make, which has led to bugs. Namely, we don't add the trait to refresh the database and then data gets left over between tests, which breaks expectations in other tests... but we don't discover it for a month 😆 😭 and then it's debug city.

It would be great if in the test's setup, we can just indicate no factory should create a parent relationship.

public function test_has_one_editor_permission_returns_true(): void
{
+    UserPermissionFactory::$defaultExpandRelationships = false;
+
    $collection = new UserPermissionCollection([
        UserPermission::factory()
-            ->withoutParents()
            ->make([
                'type' => 'viewer',
                'company_id' => 2,
                'product_id' => 789,
            ]),
        UserPermission::factory()
-            ->withoutParents()
            ->make([
                'type' => 'editor',
                'company_id' => 2,
                'product_id' => 432,
            ]),
    ]);

    $result = $collection->hasEditorForCompany(2);

    $this->assertTrue($result);
}

Even better, I would just add this to the class's setUp() method.

@NickSdot
Copy link
Contributor

Would a descriptive method be more Laravel-y?

UserPermissionFactory::disableExpandedRelationships()

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