Trait luya\testsuite\traits\CommandStdStreamTrait

Available since version1.0.19
Source Code https://github.com/luyadev/luya-testsuite/blob/master/src/traits/CommandStdStreamTrait.php

Console trait for {{\luya\console\Command}} stubs to replacement STDIN/STDOUT/STDERR with a array stack.

Example

class ControllerStub extend Controller
{
     use CommandStdStreamTrait;

     public function testUserAbort()
     {
         $controller = new ControllerStub();

         // send a user input for the first prompt
         $controller->sendInput('yes');
         // send a user input for the second prompt
         $controller->sendInput('no');

         $controller->actionIndex();

         $this->assertEquals('User answer with yes.', $controller->readOutput());
         $this->assertEquals('User answer with no.', $controller->readOutput());
     }
}

Public Methods

Hide inherited methods

Method Description Defined By
confirm() Replace {{\yii\console\Controller::confirm}} function. luya\testsuite\traits\CommandStdStreamTrait
prompt() Replace {{\yii\console\Controller::prompt}} function. luya\testsuite\traits\CommandStdStreamTrait
readError() Read data from console error outpur stream luya\testsuite\traits\CommandStdStreamTrait
readOutput() Read data from console output stream luya\testsuite\traits\CommandStdStreamTrait
select() Replace {{\yii\console\Controller::select}} function. luya\testsuite\traits\CommandStdStreamTrait
sendInput() Write passed input to the console input stream. luya\testsuite\traits\CommandStdStreamTrait
stderr() Replace the {{\yii\console\Controller::stderr}} function. luya\testsuite\traits\CommandStdStreamTrait
stdin() Replace the {{\yii\console\Controller::stdin}} function. luya\testsuite\traits\CommandStdStreamTrait
stdout() Replace the {{\yii\console\Controller::stdout}} function. luya\testsuite\traits\CommandStdStreamTrait
truncateStreams() Clean the console streams luya\testsuite\traits\CommandStdStreamTrait

Protected Methods

Hide inherited methods

Method Description Defined By
output() Replace the {{\luya\console\Controller::output}} function. luya\testsuite\traits\CommandStdStreamTrait

Property Details

Hide inherited properties

$errorStream public property

Error stream

public array $errorStream = []
$inputStream public property

Input stream

public array $inputStream = []
$outputStream public property

Output stream

public array $outputStream = []

Method Details

Hide inherited methods

confirm() public method

Replace {{\yii\console\Controller::confirm}} function.

public boolean confirm ( $message, $default false )
$message
$default boolean

                public function confirm($message, $default = false)
{
    return $this->stdin() == 'yes';
}

            
output() protected method

Replace the {{\luya\console\Controller::output}} function.

protected void output ( $message, $color null )
$message
$color null

                protected function output($message, $color = null)
{
    $this->outputStream[] = $message;
}

            
prompt() public method

Replace {{\yii\console\Controller::prompt}} function.

public mixed|string prompt ( $text, $options = [] )
$text
$options array

                public function prompt($text, $options = [])
{
    return $this->stdin();
}

            
readError() public method

Read data from console error outpur stream

public string readError ( )

                public function readError()
{
    return array_shift($this->errorStream);
}

            
readOutput() public method

Read data from console output stream

public string readOutput ( )

                public function readOutput()
{
    return array_shift($this->outputStream);
}

            
select() public method

Replace {{\yii\console\Controller::select}} function.

public mixed|string select ( $prompt, $options = [] )
$prompt
$options array

                public function select($prompt, $options = [])
{
    return $this->stdin();
}

            
sendInput() public method

Write passed input to the console input stream.

public void sendInput ( string $input )
$input string

                public function sendInput(string $input)
{
    $this->inputStream[] = $input;
}

            
stderr() public method

Replace the {{\yii\console\Controller::stderr}} function.

public void stderr ( $string )
$string

                public function stderr($string)
{
    $this->errorStream[] = $string;
}

            
stdin() public method

Replace the {{\yii\console\Controller::stdin}} function.

public mixed|string stdin ( $raw false )
$raw boolean

                public function stdin($raw = false)
{
    return $raw ? array_shift($this->inputStream) : rtrim((string) array_shift($this->inputStream), PHP_EOL);
}

            
stdout() public method

Replace the {{\yii\console\Controller::stdout}} function.

public void stdout ( $string )
$string

                public function stdout($string)
{
    $this->outputStream[] = $string;
}

            
truncateStreams() public method

Clean the console streams

public void truncateStreams ( )

                public function truncateStreams()
{
    $this->inputStream = [];
    $this->outputStream = [];
    $this->errorStream = [];
}