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

[9.x] Adds bootable traits to TestCase #42394

Merged
merged 3 commits into from
May 19, 2022
Merged

[9.x] Adds bootable traits to TestCase #42394

merged 3 commits into from
May 19, 2022

Conversation

pascalbaljet
Copy link
Contributor

This PR brings the bootable trait feature from Eloquent over to the Foundation TestCase.

Currently, the setUpTraits method checks several traits like RefreshDatabase and DatabaseMigrations, and then calls a method if the TestCase uses that trait. If you want to do this for custom traits, you must override the setUp or setUpTraits method. With this PR, it checks whether the trait has a bootTraitName method, and then calls that method.

trait RefreshSomeService
{
    public function bootRefreshSomeService()
    {
        SomeService::refresh();
    }
}

class SomeServiceTest extends TestCase
{
    use RefreshDatabase;
    use RefreshSomeService;
}

@taylorotwell
Copy link
Member

There was a similar PR by @DarkGhostHunter: https://github.com/laravel/framework/pull/39883/files

His supported setUp and tearDown in the traits, which may be useful? What are the use cases for this type of thing? What kind of traits are you writing?

@pascalbaljet
Copy link
Contributor Author

I have Dusk tests that upload a file directly to an S3 bucket (similar to Vapor with Vapor.store). I can't swap the filesystem disk to a local one as I'm using signed multipart uploads requiring specific S3 API requests. I'm using a local S3-compatible MinIO server to prevent uploading to a live service.

MinIO is fast and lightweight, so I've created a trait that boots up a new server for each test and destroys it when the test has finished. I don't have to think about starting the server manually before running the test suite, and it only boots on tests that use the trait.

I haven't thought about a tearDown method, as I register a callback using the beforeApplicationDestroyed method, just like most of the Laravel traits that interact with the database. If you want support for a tearDown method, I will gladly update the PR :)

@taylorotwell taylorotwell merged commit c73253e into laravel:9.x May 19, 2022
@taylorotwell
Copy link
Member

Renamed to setUpFoo. Thanks.

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.

3 participants