Symfony Console Input Testing

If you’re writing a Symfony Console application, you’ll likely be getting user input via the Question helper. The Symfony manual has an entry on expected input. Unfortunately until they update the docs (github issue #5246) with a better example, it creates brittle test cases that fail spectacularly, sitting in the middle of a PHPUnit run expecting user input. Which would ruin your whole test run when running in travis-ci or jenkins.

Thankfully there is a better way, which is to mock the question helper ask method. I’ve provided the ofbeaton/console-tester package on packagist so you can use it in your project.

First require it into your project using composer:

Then edit your test class to use it in your tests:

Read the commends to see what your options are. If you’re interested in how the mockQuestionHelper works behind the scenes, check out the code on github