'Testing code running in ReactPHP event loop - Laravel

My app listens for incoming emails for all of my users' email addresses. I am using a periodicTimer (from ReactPHP event-loop) 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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source