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

Explicitly call loadMissing('permissions') when the relation is needed, and test with Model::preventLazyLoading() #2776

Merged
merged 5 commits into from
Jan 31, 2025

Conversation

erikn69
Copy link
Contributor

@erikn69 erikn69 commented Dec 11, 2024

@drbyte hi,
I did add Model::preventLazyLoading() to the tests, but it doesn't give me any errors, did I put it in the wrong place(getEnvironmentSetUp)?
It would be a good idea to leave it, so that mistake is not made unintentionally.

The other changes are PHPSTAN bugs, they came out when updating

@erikn69 erikn69 mentioned this pull request Dec 13, 2024
@drbyte
Copy link
Collaborator

drbyte commented Jan 31, 2025

did I put it in the wrong place(getEnvironmentSetUp)?

Looks like that's a suitable place. So is earlier, like SetUp(). But I like it in getEnvironmentSetUp(), as you've done.

Confirmation example test, run in someplace like PermissionTest.php:

    /** @test */
    public function global_prevents_lazy_loading_flag_is_enabled()
    {
        $this->assertTrue(Model::preventsLazyLoading());
    }

@drbyte
Copy link
Collaborator

drbyte commented Jan 31, 2025

I think there's value in increasing the size of the testing dataset to be sure we're not missing something by virtue of the fact that Lazy Loading isn't even being required.
This is probably why some of the past issues with loading relations have caused unexpected outcomes. And BC frustrations.

@drbyte drbyte changed the title Test with Model::preventLazyLoading() Explicitly call loadMissing('permissions') when the relation is needed, and test with Model::preventLazyLoading() Jan 31, 2025
@drbyte drbyte merged commit dd3ccf5 into spatie:main Jan 31, 2025
39 checks passed
@drbyte
Copy link
Collaborator

drbyte commented Jan 31, 2025

Thanks.

Expanding the tests dataset can be another task/project/PR. The idea being to ensure that the lazy-loading criteria isn't skipped/bypassed due to Laravel determining that N+1 wouldn't happen since too few records are present.

@erikn69 erikn69 deleted the patch-55 branch January 31, 2025 20:08
@drbyte drbyte linked an issue Jan 31, 2025 that may be closed by this pull request
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.

Lazy Loading
2 participants