For a new package I’m working on I had to test some Artisan commands. The commands I want to test contain calls to
confirm to interactively get some input by the user. I had a little trouble finding a way to tests such commands, but luckily a blogpost by Mohammed Said pointed me in the right direction, which was to leverage partial mocks.
Here’s the most interesting part, Artisan Commands can ask the user to provided specific pieces of information using a predefined methods that cover all the use cases an application might need.
So we mock the command, register the mocked version in Kernel, add our expectations for method calls, and pretend the user response in the form of return values.