This gist is a solution to Laravel Dusk issue 152. I have been using this approach for some time and it has been working without any problems. It specifically solves mocking Mail services in Dusk tests, but the same approach could be used to fake Queue and any other services.
This implementation is based on how Dusk already handles login sessions, this can be seen in DuskServiceProvider.php:18..31. Some special routes (starting with _) are used to comunicate with the browser and set some cookies, this cookies are then read every time a test is executed in the browser and the mocking data is serialized/deserialized within the cookie. These test providers should only be enabled on testing environment, or else all this operations would be an unecessary overhead in each request.
The reason why this is a gist and not a pull request for dusk is that this is the approach I have been using in my projects and, although I think it's a good approach there are some things to do before this can be added to dusk:
- Have a generic interface / pattern for doing this (as I said, not only Mail could be mocked like this, but any service). This should be extendible for any custom services or any other Laravel services.
- Settle on a nomenclature. I am aware the naming in some classes is not ideal (for example having the service "enabled" and "active" is not understandable in my opinion).
- Complete the implementation of Mail mocking. I have only implemented what is necessary for my tests, but a pull request should be a complete implementation of Laravel mailing.
- Have some kind of feedback from Dusk maintainers. I'd like to know that this is an acceptable approach before proceeding, since it would be a waste to implement all this for it to be rejected right away.
I want to do this when I have the time and provide a pull request (no ETA at the moment). In the meantime, any comments or implementation proposals are welcome.