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

[8.x] Adds an afterRefreshingDatabase test method #39978

Merged
merged 2 commits into from
Dec 11, 2021
Merged

[8.x] Adds an afterRefreshingDatabase test method #39978

merged 2 commits into from
Dec 11, 2021

Conversation

lukeraymonddowning
Copy link
Contributor

Howdy!

This PR adds a new method that can be used in a TestCase that uses RefreshDatabase or LazilyRefreshDatabase that allows a user to perform work after the database has been refreshed. This can be used for seeding, for example, without worry that such work will negate the usefulness of LazilyRefreshDatabase.

Background

I've been working today with an app that uses LazilyRefreshDatabase and seeds a bunch of roles and permissions into the database before tests. In order to get around the fact that seeding would negate the benefits of LazilyRefreshDatabase, I wrapped the seeding logic in the MigrationsEnded event.

However, this has a couple of caveats:

  1. Most developers aren't aware of how to accomplish this, so would probably just leave it as is and suffer the performance consequences 🤷‍♂️
  2. If a package has it's own migrations (such as themsaid/wink), then this event will fire twice and you'll end up in a heap on the floor crying 😪

Implementation

So, instead, why not provide devs with a method that allows them to perform these setup operations at just the right time? This very simply adds an afterRefreshingDatabase method to the RefreshDatabase trait that can be implemented if desired to put all seeding/after database steps in one place controlled by the framework.

abstract class TestCase extends BaseTestCase
{
   
    use CreatesApplication;
    use LazilyRefreshDatabase;

    protected function afterRefreshingDatabase()
    {
        $this->artisan('db:seed', ['--class' => RoleAndPermissionSeeder::class]);
    }

}

The nice thing about this is the fact that it works regardless of which refresh database trait you use, which makes it easier to switch and swap between the two traits depending on use-case.

As always, thanks for all your hard work.

Kind Regards,
Luke

@lukeraymonddowning lukeraymonddowning changed the title Adds an afterRefreshingDatabase test method [8.x] Adds an afterRefreshingDatabase test method Dec 10, 2021
@taylorotwell taylorotwell merged commit 742b4f8 into laravel:8.x Dec 11, 2021
@lukeraymonddowning lukeraymonddowning deleted the after_refreshing_database branch April 17, 2023 11:26
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