Skip to content

Latest commit

 

History

History
165 lines (129 loc) · 4.73 KB

console-testing.md

File metadata and controls

165 lines (129 loc) · 4.73 KB

Testing Console Applications

Table of Contents

  1. Introduction
  2. Testing Commands
  3. Prompt Answers
  4. with() Methods
  5. Response Assertions
  6. isError()
  7. isFatal()
  8. isOK()
  9. isWarning()
  10. outputEquals()
  11. 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:

  • withArguments($arguments)
    • Passes the argument or array of arguments to the command
  • withAnswers($answers)
    • Passes the prompt answers or array of prompt answers to the command
  • withOptions($options)
    • Passes the options or array of options to the command
  • withStyle($isStyled)
    • Sets whether or not the response should be styled

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);
}