Question

I'm writing my first Artisan command in Laravel 4.1 and wanted to get some ideas on how to test it. My command does one thing (for now). It basically deletes table entries (Notes) from a DB that are a specified amount of days old.


Command Example:

OneTimeNote:delete --days=25

The command works with flying colors. But I've written the command first for learning purposes and now I desire to follow it with a test. I'm using PHPUnit and Mockery.


Command Code: http://pastebin.com/index/dZrxpt8x

As you can see, I'm injecting my Note implementation (for abstraction) and then I'm executing one of it's methods 'deleteNotesOlderThan(int $days)'. Like I said, this all works fine and dandy. My issue starts when I'm trying to test the thing.


Command Test: http://pastebin.com/6UwxGvcN

If you look at the code, you can see where I'm stuck at. How do I Mock my Note method and generate a Command Test? Also what sort of things should I be testing with this particular command?

Thank you in advance

Was it helpful?

Solution

My suggestion would be to follow the examples given for you in Laravel.

Picking a random artisan test from the framework, it looks like you don't need Symfony's 'CommandTester' class, but instead you just instantiate and go.

From https://github.com/laravel/framework/blob/master/tests/Foundation/FoundationConfigPublishCommandTest.php: (link subject to become stale)

public function testCommandCallsPublisherWithProperPackageName()
{
    $command = new Illuminate\Foundation\Console\ConfigPublishCommand($pub = m::mock('Illuminate\Foundation\ConfigPublisher'));
    $pub->shouldReceive('publishPackage')->once()->with('foo');
    $command->run(new Symfony\Component\Console\Input\ArrayInput(array('package' => 'foo')), new Symfony\Component\Console\Output\NullOutput);
}

So you instantiate your command, passing in your mocked repository interface, add your expectations to the mock, then call run on the command, passing in 'fake' input and output classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top