I spent some time with Laracasts but the TDD tests were 1) based on Codeception (nice, but not what I wanted) and 2) were for Laravel 4. Directly using the Symfony console test methodology doesn't work with Laravel's IoC container in Laravel 5.1. Even the Laravel foundation tests don't specifically test each of the Artisan console commands. At least I couldn't find anything, although ultimately it was the way that Laravel instantiates Artisan that gave me a clue.
You can run an Artisan command in your tests, but what I really wanted to do was check the output to the terminal, which means capturing stdout while running a command, which the test-native Artisan command doesn't support.
In any event, this works well, and displays both ways of running an Artisan command in a test:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BulkUpdateModelTest extends TestCase | |
{ | |
/** @expectedException ReflectionException */ | |
public function testItthrowsAnException_if_the_requested_model_is_invalid() | |
{ | |
$status = $this->artisan('mynamespace:bulkupdate',['model' => 'foo']); | |
} | |
public function testItDisplaysAnError_if_the_requested_model_is_invalid() | |
{ | |
$kernel = $this->app->make(Illuminate\Contracts\Console\Kernel::class); | |
$status = $kernel->handle( | |
$input = new Symfony\Component\Console\Input\ArrayInput(array( | |
'command' => 'mynamespace:bulkupdate', | |
'model' => 'foo', | |
)), | |
$output = new Symfony\Component\Console\Output\BufferedOutput | |
); | |
$this->assertContains('Class \App\Models\foo does not exist', $output->fetch()); | |
} | |
} |
A blog post every couple of years seems to be about the right frequency. No?