Fr3nch13/CakePHP Utilities

StubConsoleOutput extends ConsoleOutput
in package

StubOutput makes testing shell commands/shell helpers easier.

You can use this class by injecting it into a ConsoleIo instance that your command/task/helper uses:

use Cake\Console\ConsoleIo;
use Cake\Console\TestSuite\StubConsoleOutput;

$output = new StubConsoleOutput();
$io = new ConsoleIo($output);

Table of Contents

Constants

COLOR  = 2
Color output - Convert known tags in to ANSI color escape codes.
LF  = PHP_EOL
Constant for a newline.
PLAIN  = 1
Plain output - tags will be stripped.
RAW  = 0
Raw output constant - no modification of output text.

Properties

$_backgroundColors  : array<string, int>
background colors used in colored output.
$_foregroundColors  : array<string, int>
text colors used in colored output.
$_options  : array<string, int>
Formatting options for colored output.
$_out  : array<string|int, string>
Buffered messages.
$_output  : resource
File handle for output.
$_outputAs  : int
The current output type.
$_styles  : array<string, array<string|int, mixed>>
Styles that are available as tags in console output.

Methods

__construct()  : mixed
Construct the output object.
__destruct()  : mixed
Clean up and close handles
getOutputAs()  : int
Get the output type on how formatting tags are treated.
getStyle()  : array<string|int, mixed>
Gets the current styles offered
messages()  : array<string|int, string>
Get the buffered output.
output()  : string
Get the output as a string
setOutputAs()  : void
Set the output type on how formatting tags are treated.
setStyle()  : void
Sets style.
styles()  : array<string, mixed>
Gets all the style definitions.
styleText()  : string
Apply styling to text.
write()  : int
Write output to the buffer.
_replaceTags()  : string
Replace tags with color codes.
_write()  : int
Writes a message to the output stream.

Constants

COLOR

Color output - Convert known tags in to ANSI color escape codes.

public int COLOR = 2

PLAIN

Plain output - tags will be stripped.

public int PLAIN = 1

RAW

Raw output constant - no modification of output text.

public int RAW = 0

Properties

$_backgroundColors

background colors used in colored output.

protected static array<string, int> $_backgroundColors = ['black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47]

$_foregroundColors

text colors used in colored output.

protected static array<string, int> $_foregroundColors = ['black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37]

$_options

Formatting options for colored output.

protected static array<string, int> $_options = ['bold' => 1, 'underline' => 4, 'blink' => 5, 'reverse' => 7]

$_output

File handle for output.

protected resource $_output

$_outputAs

The current output type.

protected int $_outputAs = self::COLOR
Tags
see
setOutputAs()

For manipulation.

$_styles

Styles that are available as tags in console output.

protected static array<string, array<string|int, mixed>> $_styles = ['emergency' => ['text' => 'red'], 'alert' => ['text' => 'red'], 'critical' => ['text' => 'red'], 'error' => ['text' => 'red'], 'warning' => ['text' => 'yellow'], 'info' => ['text' => 'cyan'], 'debug' => ['text' => 'yellow'], 'success' => ['text' => 'green'], 'comment' => ['text' => 'blue'], 'question' => ['text' => 'magenta'], 'notice' => ['text' => 'cyan']]

You can modify these styles with ConsoleOutput::styles()

Methods

__construct()

Construct the output object.

public __construct([string|resource $stream = 'php://stdout' ]) : mixed

Checks for a pretty console environment. Ansicon and ConEmu allows pretty consoles on Windows, and is supported.

Parameters
$stream : string|resource = 'php://stdout'

The identifier of the stream to write output to.

Tags
throws
ConsoleException

If the given stream is not a valid resource.

__destruct()

Clean up and close handles

public __destruct() : mixed

getOutputAs()

Get the output type on how formatting tags are treated.

public getOutputAs() : int
Return values
int

getStyle()

Gets the current styles offered

public getStyle(string $style) : array<string|int, mixed>
Parameters
$style : string

The style to get.

Return values
array<string|int, mixed>

The style or empty array.

messages()

Get the buffered output.

public messages() : array<string|int, string>
Return values
array<string|int, string>

output()

Get the output as a string

public output() : string
Return values
string

setOutputAs()

Set the output type on how formatting tags are treated.

public setOutputAs(int $type) : void
Parameters
$type : int

The output type to use. Should be one of the class constants.

Tags
throws
InvalidArgumentException

in case of a not supported output type.

setStyle()

Sets style.

public setStyle(string $style, array<string|int, mixed> $definition) : void

Creates or modifies an existing style.

$output->setStyle('annoy', ['text' => 'purple', 'background' => 'yellow', 'blink' => true]);

Remove a style

$this->output->setStyle('annoy', []);
Parameters
$style : string

The style to set.

$definition : array<string|int, mixed>

The array definition of the style to change or create..

styles()

Gets all the style definitions.

public styles() : array<string, mixed>
Return values
array<string, mixed>

styleText()

Apply styling to text.

public styleText(string $text) : string
Parameters
$text : string

Text with styling tags.

Return values
string

String with color codes added.

write()

Write output to the buffer.

public write(array<string|int, string>|string $message[, int $newlines = 1 ]) : int
Parameters
$message : array<string|int, string>|string

A string or an array of strings to output

$newlines : int = 1

Number of newlines to append

Return values
int

_replaceTags()

Replace tags with color codes.

protected _replaceTags(array<string, string> $matches) : string
Parameters
$matches : array<string, string>

An array of matches to replace.

Return values
string

_write()

Writes a message to the output stream.

protected _write(string $message) : int
Parameters
$message : string

Message to write.

Return values
int

The number of bytes returned from writing to output.


        
On this page

Search results