- Introduction
- Testing Commands
- Prompt Answers
- with() Methods
- Response Assertions
- isError()
- isFatal()
- isOK()
- isWarning()
- outputEquals()
- statusCodeEquals()
There are two ways to test a command:Note: If you need to define a
setUp()ortearDown()method in your test, make sure to callparent::setUp()orparent::tearDown().
- 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 aCommandBuilderto 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>");
}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("");
}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
public function testStatusCode()
{
$this->execute("errorcommand")
->assertResponse
->isError();
}public function testStatusCode()
{
$this->execute("badcommand")
->assertResponse
->isFatal();
}public function testStatusCode()
{
$this->execute("goodcommand")
->assertResponse
->isOK();
}public function testStatusCode()
{
$this->execute("warningcommand")
->assertResponse
->isWarning();
}public function testOutputIsCorrect()
{
$this->execute("hello")
->assertResponse
->outputEquals("Hello, world");
}public function testStatusCode()
{
$this->execute("hello")
->assertResponse
->statusCodeEquals(StatusCodes::WARNING);
}