title: Laravel 5 console (artisan) command unit tests
body: <p>I am migrating my Laravel 4.2 app to 5.1 (starting with 5.0) and am a lot of trouble with my console command unit tests. I have artisan commands for which I need to test the produced console output, proper question/response handling and interactions with other services (using mocks). For all its merits, the Laravel doc is unfortunately silent with regards to testing console commands.</p>
  
  <p>I finally found a way to create those tests, but it feels like a hack with those <code>setLaravel</code> and <code>setApplication</code> calls.</p>
  
  <p>Is there a better way to do this? I wish I could add my mock instances to the Laravel IoC container and let it create the commands to test with everything properly set. I'm afraid my unit tests will break easily with newer Laravel versions.</p>
  
  <p>Here's my unit test:</p>
  
  <p>Use statements:</p>
  
  <pre><code>use Mockery as m;
  use App\Console\Commands\AddClientCommand;
  use Symfony\Component\Console\Tester\CommandTester;
  </code></pre>
  
  <p>Setup</p>
  
  <pre><code>public function setUp() {
      parent::setUp();
  
      $this-&gt;store = m::mock('App\Services\Store');
  
      $this-&gt;command = new AddClientCommand($this-&gt;store);
  
      // Taken from laravel/framework artisan command unit tests
      // (e.g. tests/Database/DatabaseMigrationRollbackCommandTest.php)
      $this-&gt;command-&gt;setLaravel($this-&gt;app-&gt;make('Illuminate\Contracts\Foundation\Application'));
  
      // Required to provide input to command questions (provides command-&gt;getHelper())
      // Taken from ??? when I first built my command tests in Laravel 4.2
      $this-&gt;command-&gt;setApplication($this-&gt;app-&gt;make('Symfony\Component\Console\Application'));
  }
  </code></pre>
  
  <p>Input provided as command arguments. Checks console output</p>
  
  <pre><code>public function testReadCommandOutput() {
      $commandTester = new CommandTester($this-&gt;command);
  
      $result = $commandTester-&gt;execute([
          '--client-name' =&gt; 'New Client',
      ]);
  
      $this-&gt;assertSame(0, $result);
      $templatePath = $this-&gt;testTemplate;
  
      // Check console output
      $this-&gt;assertEquals(1, preg_match('/^Client \'New Client\' was added./m', $commandTester-&gt;getDisplay()));
  }
  </code></pre>
  
  <p>Input provided by simulated keyboard keys</p>
  
  <pre><code>public function testAnswerQuestions() {
      $commandTester = new CommandTester($this-&gt;command);
  
      // Simulate keyboard input in console for new client
      $inputs = $this-&gt;command-&gt;getHelper('question');
      $inputs-&gt;setInputStream($this-&gt;getInputStream("New Client\n"));
      $result = $commandTester-&gt;execute([]);
  
      $this-&gt;assertSame(0, $result);
      $templatePath = $this-&gt;testTemplate;
  
      // Check console output
      $this-&gt;assertEquals(1, preg_match('/^Client \'New Client\' was added./m', $commandTester-&gt;getDisplay()));
  }
  
  protected function getInputStream($input) {
      $stream = fopen('php://memory', 'r+', false);
      fputs($stream, $input);
      rewind($stream);
      return $stream;
  }
  </code></pre>
  
  <h3>updates</h3>
  
  <ol>
  <li>This doesn't work in Laravel 5.1 <a href="https://github.com/laravel/framework/issues/11946">#11946</a></li>
  </ol>