Skip to content

[Help] Writing PHPUnit tests - Laravel #247

@catabozan

Description

@catabozan

My app listens for incoming emails for all of my users' email addresses.
I am using a periodicTimer to listen for incoming emails. When a new email is received, an event is dispatched.

I want to test that the event is dispatched when a new email is received.

The problem is that the event dispatches after the test is finished running, I checked using a var_dump() in the event class. And throws this error:

PHP Fatal error:  Uncaught ReflectionException: Class "auth" does not exist in 
/path/to/project/vendor/laravel/framework/src/Illuminate/Container/Container.php:877

Also, even if I remove the test in question(the one that checks if the event is dispatched) and run the test suite, the PHP process keeps running even when the test suite has finished (the periodicTimer is called in the other tests, but no event is dispatched)

I think the periodicTimer keeps running even after the Laravel app shuts down (after the tests) and thus the ReflectionException when trying to dispatch the event.

I tried adding a sleep() in my test to give time to the event to get dispatched, but it doesn't dispatch at all. Then I thought that the sleep function was somehow blocking the event loop so I used a long-running while() but the event still got dispatched after the test ran, and the exception got thrown. (the while ran for 40 seconds).

Has anyone encountered this issue before? Is there a way to test code running inside a periodicTimer, and also stop the event loop at the end of the test suite?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions