- Introduction
- Testing Commands
- Prompt Answers
- with() Methods
- Response Assertions
- isError()
- isFatal()
- isOK()
- isWarning()
- outputEquals()
- statusCodeEquals()
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()
ortearDown()
method in your test, make sure to callparent::setUp()
orparent::tearDown()
.
There are two ways to test a command:
- Call
$this->execute()
and pass in the command name, argument values, option values, prompt answers, and whether or not to style the output - Call
$this->command()
, which returns aCommandBuilder
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>');
}
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('');
}
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
You can run various assertions on the response returned by the Kernel
. To do so, simply use $this->assertResponse
.
Asserts that the status code of the last command is an error:
public function testStatusCode()
{
$this->execute('errorcommand')
->assertResponse
->isError();
}
Asserts that the status code of the last command is fatal:
public function testStatusCode()
{
$this->execute('badcommand')
->assertResponse
->isFatal();
}
Asserts that the status code of the last command is OK:
public function testStatusCode()
{
$this->execute('goodcommand')
->assertResponse
->isOK();
}
Asserts that the status code of the last command is a warning:
public function testStatusCode()
{
$this->execute('warningcommand')
->assertResponse
->isWarning();
}
Asserts that the output of the last command equals an expected value:
public function testOutputIsCorrect()
{
$this->execute('hello')
->assertResponse
->outputEquals('Hello, world');
}
Asserts that the status code of the last command equals an expected value:
public function testStatusCode()
{
$this->execute('hello')
->assertResponse
->statusCodeEquals(StatusCodes::WARNING);
}