PHP Tutorial: How to Test Message Queues and Event-Driven Systems
Chris recently joined us during Codementor’s Office Hours to answer viewers’ questions.
The text below is a summary done by the Codementor team and may vary from the original video and if you see any issues, please let us know!
How do you test Message Queues (Beanstalkd Queue)?
I test those with integration tests. I do write integration tests from time to time, where I use the real things. To write integration tests for something like Beanstalkd or RabbitMQ or any of these things, I would need to set up an environment where I’d have real versions of those tools available and real versions of the data set from production. Often you’d find yourself needing to create tools to migrate data from one environment to the other, and those are a pain in the ass to write. Sometimes those are good to invest time in so you can move complete data sets from production into staging.
I would also use PHPUnit to write tests as if I was writing a unit test. I use the real things instead of doing mocks. For my OpenCFP project, code coverage is shamefully low because I had to crank out a whole bunch of stuff for a deadline for my own conference. I’m now going back to write some semi-integration tests because the application is in a state where I can use dependency injection to mock a lot of things such as request objects and database calls. That helps me create some effective tests that verifies the code I’ve written in my controllers work correctly.
How do you test “Event driven systems”? Especially the “order”/”sequence” of events.
You don’t. That’s kind of the last frontier for testing. I can recommend testing the individual stuff inside your application that’s event-driven to make sure the individual units of code get called correctly as part of the event loop. Otherwise you’re stuck with functional tests using things like Behat with mink.
When you’re trying to test event-driven stuff and the sequence of events, the problem is it’s extremely difficult to control the sequence of these things. You can’t always control it for reason that are out of your hand with event loops. This is my experience. Every single time I’ve tried to write things to test event-driven systems and try to force things to happen in a particular sequence, it didn’t work well. I ended up trying to focus on testing whatever things are being called as part of the event loop.
If you want to take a page out of some other people’s thoughts about this, people say when you’re building apps, you shouldn’t be writing tests for the controllers in an MVC-style app. If you think of the event loops for an event driven application as the controller, it makes total sense. Test the things that are going to get called in an event loop and do your best to mock the dependencies and simulate the type of responses that are expected. This is a much bigger problem if you’re using something like Node.js. With PHP, I can count on two hands the number of large-scale applications that are using event-driven stuff in PHP.
Other posts in this series with Chris Hartjes:
- PHP Office Hours: How to Get Started on PHP Testing
- PHP Testing Tutorial: Mocks, Doubles, Dummies and Stubs