Testing Console Applications

Table of Contents

  1. Introduction
  2. Testing Commands
    1. Prompt Answers
    2. with() Methods
  3. Response Assertions
    1. isError()
    2. isFatal()
    3. isOK()
    4. isWarning()
    5. outputEquals()
    6. statusCodeEquals()

Introduction

Opulence comes with the ability to integration test your console commands using Opulence\Framework\Console\Testing\PhpUnit\IntegrationTestCase. With it, you can test the output of your commands, simulate responses to prompt questions, and check the status code of the kernel. Simply extend IntegrationTestCase in your test classes, and you'll inherit the following methods to help test your console application:

Note: If you need to define a setUp() or tearDown() method in your test, make sure to call parent::setUp() or parent::tearDown().

Testing Commands

There are two ways to test a command:

  1. Call $this->execute() and pass in the command name, argument values, option values, prompt answers, and whether or not to style the output
  2. Call $this->command(), which returns a CommandBuilder to build up your command

For example, the following two tests will test identical things:

public function testUsingExecute()
{
    $this->execute('app:rename', ['Project', 'MyApp'], [], true)
        ->assertResponse
        ->outputEquals('<success>Updated name successfully</success>');
}

public function testUsingCommand()
{
    $this->command('app:rename')
        ->withArguments(['Project', 'MyApp'])
        ->withAnswers(true)
        ->execute()
        ->assertResponse
        ->outputEquals('<success>Updated name successfully</success>');
}

Prompt Answers

Let's say that our command has a Confirmation question that we want to try testing with true and false answers. Simply pass the answers and test the output of each:

public function testConfirmation()
{
    $this->command('app:rename')
        ->withArguments(['Project', 'MyApp'])
        ->withAnswers(true)
        ->execute()
        ->assertResponse
        ->outputEquals('<success>Updated name successfully</success>');
    $this->command('app:rename')
        ->withArguments(['Project', 'MyApp'])
        ->withAnswers(false)
        ->execute()
        ->assertResponse
        ->outputEquals('');
}

with() Methods

The following methods can be used to pass data to your command:

Response Assertions

You can run various assertions on the response returned by the Kernel. To do so, simply use $this->assertResponse.

isError()

Asserts that the status code of the last command is an error:

public function testStatusCode()
{
    $this->execute('errorcommand')
        ->assertResponse
        ->isError();
}

isFatal()

Asserts that the status code of the last command is fatal:

public function testStatusCode()
{
    $this->execute('badcommand')
        ->assertResponse
        ->isFatal();
}

isOK()

Asserts that the status code of the last command is OK:

public function testStatusCode()
{
    $this->execute('goodcommand')
        ->assertResponse
        ->isOK();
}

isWarning()

Asserts that the status code of the last command is a warning:

public function testStatusCode()
{
    $this->execute('warningcommand')
        ->assertResponse
        ->isWarning();
}

outputEquals()

Asserts that the output of the last command equals an expected value:

public function testOutputIsCorrect()
{
    $this->execute('hello')
        ->assertResponse
        ->outputEquals('Hello, world');
}

statusCodeEquals()

Asserts that the status code of the last command equals an expected value:

public function testStatusCode()
{
    $this->execute('hello')
        ->assertResponse
        ->statusCodeEquals(StatusCodes::WARNING);
}