PHPCS fixes

This commit is contained in:
Gareth Ellis 2015-12-01 14:26:22 +00:00
parent 6b6a90e5c1
commit eafee25d13
10 changed files with 806 additions and 745 deletions

View file

@ -85,7 +85,7 @@ class ConsoleOutput {
*
* @var int
*/
protected $_lastWritten = 0;
protected $_lastWritten = 0;
/**
* The current output type. Manipulated with ConsoleOutput::outputAs();
@ -206,21 +206,20 @@ class ConsoleOutput {
* length of the last message output.
* @return void
*/
public function overwrite($message, $newlines = 1, $size = null)
{
$size = $size ?: $this->_lastWritten;
// Output backspaces.
$this->write(str_repeat("\x08", $size), 0);
$newBytes = $this->write($message, 0);
// Fill any remaining bytes with spaces.
$fill = $size - $newBytes;
if ($fill > 0) {
$this->write(str_repeat(' ', $fill), 0);
}
if ($newlines) {
$this->write("", $newlines);
}
}
public function overwrite($message, $newlines = 1, $size = null) {
$size = $size ?: $this->_lastWritten;
// Output backspaces.
$this->write(str_repeat("\x08", $size), 0);
$newBytes = $this->write($message, 0);
// Fill any remaining bytes with spaces.
$fill = $size - $newBytes;
if ($fill > 0) {
$this->write(str_repeat(' ', $fill), 0);
}
if ($newlines) {
$this->write("", $newlines);
}
}
/**
* Apply styling to text.
@ -277,7 +276,7 @@ class ConsoleOutput {
*/
protected function _write($message) {
$this->_lastWritten = fwrite($this->_output, $message);
return $this->_lastWritten;
return $this->_lastWritten;
}
/**

View file

@ -1,113 +1,122 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.8
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses("ShellHelper", "Console/Helper");
/**
* Create a progress bar using a supplied callback.
*/
class ProgressShellHelper extends ShellHelper
{
/**
* The current progress.
*
* @var int
*/
protected $_progress = 0;
class ProgressShellHelper extends ShellHelper {
/**
* The total number of 'items' to progress through.
*
* @var int
*/
protected $_total = 0;
/**
* The current progress.
*
* @var int
*/
protected $_progress = 0;
/**
* The width of the bar.
*
* @var int
*/
protected $_width = 0;
/**
* The total number of 'items' to progress through.
*
* @var int
*/
protected $_total = 0;
/**
* Output a progress bar.
*
* Takes a number of options to customize the behavior:
*
* - `total` The total number of items in the progress bar. Defaults
* to 100.
* - `width` The width of the progress bar. Defaults to 80.
* - `callback` The callback that will be called in a loop to advance the progress bar.
*
* @param array $args The arguments/options to use when outputing the progress bar.
* @return void
*/
public function output($args)
{
$args += ['callback' => null];
if (isset($args[0])) {
$args['callback'] = $args[0];
}
if (!$args['callback'] || !is_callable($args['callback'])) {
throw new RuntimeException('Callback option must be a callable.');
}
$this->init($args);
$callback = $args['callback'];
while ($this->_progress < $this->_total) {
$callback($this);
$this->draw();
}
$this->_consoleOutput->write('');
}
/**
* The width of the bar.
*
* @var int
*/
protected $_width = 0;
/**
* Initialize the progress bar for use.
*
* - `total` The total number of items in the progress bar. Defaults
* to 100.
* - `width` The width of the progress bar. Defaults to 80.
*
* @param array $args The initialization data.
* @return void
*/
public function init(array $args = [])
{
$args += ['total' => 100, 'width' => 80];
$this->_progress = 0;
$this->_width = $args['width'];
$this->_total = $args['total'];
}
/**
* Output a progress bar.
*
* Takes a number of options to customize the behavior:
*
* - `total` The total number of items in the progress bar. Defaults
* to 100.
* - `width` The width of the progress bar. Defaults to 80.
* - `callback` The callback that will be called in a loop to advance the progress bar.
*
* @param array $args The arguments/options to use when outputing the progress bar.
* @return void
* @throws RuntimeException
*/
public function output($args) {
$args += ['callback' => null];
if (isset($args[0])) {
$args['callback'] = $args[0];
}
if (!$args['callback'] || !is_callable($args['callback'])) {
throw new RuntimeException('Callback option must be a callable.');
}
$this->init($args);
$callback = $args['callback'];
while ($this->_progress < $this->_total) {
$callback($this);
$this->draw();
}
$this->_consoleOutput->write('');
}
/**
* Initialize the progress bar for use.
*
* - `total` The total number of items in the progress bar. Defaults
* to 100.
* - `width` The width of the progress bar. Defaults to 80.
*
* @param array $args The initialization data.
* @return void
*/
public function init(array $args = []) {
$args += ['total' => 100, 'width' => 80];
$this->_progress = 0;
$this->_width = $args['width'];
$this->_total = $args['total'];
}
/**
* Increment the progress bar.
*
* @param int $num The amount of progress to advance by.
* @return void
*/
public function increment($num = 1)
{
$this->_progress = min(max(0, $this->_progress + $num), $this->_total);
}
/**
* Increment the progress bar.
*
* @param int $num The amount of progress to advance by.
* @return void
*/
public function increment($num = 1) {
$this->_progress = min(max(0, $this->_progress + $num), $this->_total);
}
/**
* Render the progress bar based on the current state.
*
* @return void
*/
public function draw()
{
$numberLen = strlen(' 100%');
$complete = round($this->_progress / $this->_total, 2);
$barLen = ($this->_width - $numberLen) * ($this->_progress / $this->_total);
$bar = '';
if ($barLen > 1) {
$bar = str_repeat('=', $barLen - 1) . '>';
}
$pad = ceil($this->_width - $numberLen - $barLen);
if ($pad > 0) {
$bar .= str_repeat(' ', $pad);
}
$percent = ($complete * 100) . '%';
$bar .= str_pad($percent, $numberLen, ' ', STR_PAD_LEFT);
$this->_consoleOutput->overwrite($bar, 0);
}
/**
* Render the progress bar based on the current state.
*
* @return void
*/
public function draw() {
$numberLen = strlen(' 100%');
$complete = round($this->_progress / $this->_total, 2);
$barLen = ($this->_width - $numberLen) * ($this->_progress / $this->_total);
$bar = '';
if ($barLen > 1) {
$bar = str_repeat('=', $barLen - 1) . '>';
}
$pad = ceil($this->_width - $numberLen - $barLen);
if ($pad > 0) {
$bar .= str_repeat(' ', $pad);
}
$percent = ($complete * 100) . '%';
$bar .= str_pad($percent, $numberLen, ' ', STR_PAD_LEFT);
$this->_consoleOutput->overwrite($bar, 0);
}
}

View file

@ -1,64 +1,82 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.8
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
abstract class ShellHelper
{
/**
* Default config for this helper.
*
* @var array
*/
protected $_defaultConfig = [];
abstract class ShellHelper {
/**
* ConsoleOutput instance.
*
* @var ConsoleOutput
*/
protected $_consoleOutput;
/**
* Default config for this helper.
*
* @var array
*/
protected $_defaultConfig = [];
/**
* Runtime config
*
* @var array
*/
protected $_config = [];
/**
* Whether the config property has already been configured with defaults
*
* @var bool
*/
protected $_configInitialized = false;
/**
* ConsoleOutput instance.
*
* @var ConsoleOutput
*/
protected $_consoleOutput;
/**
* Constructor.
*
* @param ConsoleOutput $consoleOutput The ConsoleOutput instance to use.
* @param array $config The settings for this helper.
*/
public function __construct(ConsoleOutput $consoleOutput, array $config = array())
{
$this->_consoleOutput = $consoleOutput;
$this->config($config);
}
/**
* Runtime config
*
* @var array
*/
protected $_config = [];
public function config($config = null)
{
if ($config === null) {
return $this->_config;
}
if (!$this->_configInitialized) {
$this->_config = array_merge($this->_defaultConfig, $config);
$this->_configInitialized = true;
} else {
$this->_config = array_merge($this->_config, $config);
}
}
/**
* Whether the config property has already been configured with defaults
*
* @var bool
*/
protected $_configInitialized = false;
/**
* This method should output content using `$this->_consoleOutput`.
*
* @param array $args The arguments for the helper.
* @return void
*/
abstract public function output($args);
/**
* Constructor.
*
* @param ConsoleOutput $consoleOutput The ConsoleOutput instance to use.
* @param array $config The settings for this helper.
*/
public function __construct(ConsoleOutput $consoleOutput, array $config = array()) {
$this->_consoleOutput = $consoleOutput;
$this->config($config);
}
/**
* Initialize config & store config values
*
* @param null $config Config values to set
* @return array|void
*/
public function config($config = null) {
if ($config === null) {
return $this->_config;
}
if (!$this->_configInitialized) {
$this->_config = array_merge($this->_defaultConfig, $config);
$this->_configInitialized = true;
} else {
$this->_config = array_merge($this->_config, $config);
}
}
/**
* This method should output content using `$this->_consoleOutput`.
*
* @param array $args The arguments for the helper.
* @return void
*/
abstract public function output($args);
}

View file

@ -1,112 +1,124 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.8
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses("ShellHelper", "Console/Helper");
class TableShellHelper extends ShellHelper
{
/**
* Default config for this helper.
*
* @var array
*/
protected $_defaultConfig = [
'headers' => true,
'rowSeparator' => false,
'headerStyle' => 'info',
];
/**
* Create a visually pleasing ASCII art table
* from 2 dimensional array data.
*/
class TableShellHelper extends ShellHelper {
/**
* Calculate the column widths
*
* @param array $rows The rows on which the columns width will be calculated on.
* @return array
*/
protected function _calculateWidths($rows)
{
$widths = [];
foreach ($rows as $line) {
for ($i = 0, $len = count($line); $i < $len; $i++) {
$columnLength = mb_strlen($line[$i]);
if ($columnLength > (isset($widths[$i]) ? $widths[$i] : 0)) {
$widths[$i] = $columnLength;
}
}
}
return $widths;
}
/**
* Default config for this helper.
*
* @var array
*/
protected $_defaultConfig = [
'headers' => true,
'rowSeparator' => false,
'headerStyle' => 'info',
];
/**
* Output a row separator.
*
* @param array $widths The widths of each column to output.
* @return void
*/
protected function _rowSeparator($widths)
{
$out = '';
foreach ($widths as $column) {
$out .= '+' . str_repeat('-', $column + 2);
}
$out .= '+';
$this->_consoleOutput->write($out);
}
/**
* Calculate the column widths
*
* @param array $rows The rows on which the columns width will be calculated on.
* @return array
*/
protected function _calculateWidths($rows) {
$widths = [];
foreach ($rows as $line) {
for ($i = 0, $len = count($line); $i < $len; $i++) {
$columnLength = mb_strlen($line[$i]);
if ($columnLength > (isset($widths[$i]) ? $widths[$i] : 0)) {
$widths[$i] = $columnLength;
}
}
}
return $widths;
}
/**
* Output a row.
*
* @param array $row The row to output.
* @param array $widths The widths of each column to output.
* @param array $options Options to be passed.
* @return void
*/
protected function _render($row, $widths, $options = [])
{
$out = '';
foreach ($row as $i => $column) {
$pad = $widths[$i] - mb_strlen($column);
if (!empty($options['style'])) {
$column = $this->_addStyle($column, $options['style']);
}
$out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
}
$out .= '|';
$this->_consoleOutput->write($out);
}
/**
* Output a row separator.
*
* @param array $widths The widths of each column to output.
* @return void
*/
protected function _rowSeparator($widths) {
$out = '';
foreach ($widths as $column) {
$out .= '+' . str_repeat('-', $column + 2);
}
$out .= '+';
$this->_consoleOutput->write($out);
}
/**
* Output a table.
*
* @param array $rows The data to render out.
* @return void
*/
public function output($rows)
{
$config = $this->config();
$widths = $this->_calculateWidths($rows);
$this->_rowSeparator($widths);
if ($config['headers'] === true) {
$this->_render(array_shift($rows), $widths, ['style' => $config['headerStyle']]);
$this->_rowSeparator($widths);
}
foreach ($rows as $line) {
$this->_render($line, $widths);
if ($config['rowSeparator'] === true) {
$this->_rowSeparator($widths);
}
}
if ($config['rowSeparator'] !== true) {
$this->_rowSeparator($widths);
}
}
/**
* Add style tags
*
* @param string $text The text to be surrounded
* @param string $style The style to be applied
* @return string
*/
protected function _addStyle($text, $style)
{
return '<' . $style . '>' . $text . '</' . $style . '>';
}
/**
* Output a row.
*
* @param array $row The row to output.
* @param array $widths The widths of each column to output.
* @param array $options Options to be passed.
* @return void
*/
protected function _render($row, $widths, $options = []) {
$out = '';
foreach ($row as $i => $column) {
$pad = $widths[$i] - mb_strlen($column);
if (!empty($options['style'])) {
$column = $this->_addStyle($column, $options['style']);
}
$out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
}
$out .= '|';
$this->_consoleOutput->write($out);
}
/**
* Output a table.
*
* @param array $rows The data to render out.
* @return void
*/
public function output($rows) {
$config = $this->config();
$widths = $this->_calculateWidths($rows);
$this->_rowSeparator($widths);
if ($config['headers'] === true) {
$this->_render(array_shift($rows), $widths, ['style' => $config['headerStyle']]);
$this->_rowSeparator($widths);
}
foreach ($rows as $line) {
$this->_render($line, $widths);
if ($config['rowSeparator'] === true) {
$this->_rowSeparator($widths);
}
}
if ($config['rowSeparator'] !== true) {
$this->_rowSeparator($widths);
}
}
/**
* Add style tags
*
* @param string $text The text to be surrounded
* @param string $style The style to be applied
* @return string
*/
protected function _addStyle($text, $style) {
return '<' . $style . '>' . $text . '</' . $style . '>';
}
}

View file

@ -179,7 +179,7 @@ class Shell extends Object {
*
* @var array
*/
protected $_helpers = array();
protected $_helpers = array();
/**
* Constructs this Shell instance.
@ -652,8 +652,7 @@ class Shell extends Object {
*
* @param array|string $message The message to output.
* @param int $newlines Number of newlines to append.
* @param int $size The number of bytes to overwrite. Defaults to the
* length of the last message output.
* @param int $size The number of bytes to overwrite. Defaults to the length of the last message output.
* @return int|bool Returns the number of bytes returned from writing to stdout.
*/
public function overwrite($message, $newlines = 1, $size = null) {
@ -791,23 +790,22 @@ class Shell extends Object {
*
* @param string $name Name of the helper class. Supports plugin syntax.
* @return ShellHelper Instance of helper class
* @throws RunTimeException If invalid class name is provided
* @throws RuntimeException If invalid class name is provided
*/
public function helper($name)
{
list($plugin, $helperClassName) = pluginSplit($name, true);
$helperClassName = Inflector::camelize($name) . "ShellHelper";
if (isset($this->_helpers[$name])) {
return $this->_helpers[$name];
}
App::uses($helperClassName, $plugin . "Console/Helper");
if (!class_exists($helperClassName)) {
throw new RuntimeException("Class " . $helperClassName . " not found");
}
$helper = new $helperClassName($this->stdout);
$this->_helpers[$name] = $helper;
return $helper;
}
public function helper($name) {
list($plugin, $helperClassName) = pluginSplit($name, true);
$helperClassName = Inflector::camelize($name) . "ShellHelper";
if (isset($this->_helpers[$name])) {
return $this->_helpers[$name];
}
App::uses($helperClassName, $plugin . "Console/Helper");
if (!class_exists($helperClassName)) {
throw new RuntimeException("Class " . $helperClassName . " not found");
}
$helper = new $helperClassName($this->stdout);
$this->_helpers[$name] = $helper;
return $helper;
}
/**
* Action to create a Unit Test

View file

@ -99,21 +99,21 @@ class ConsoleOutputTest extends CakeTestCase {
*
* @return void
*/
public function testOverwrite() {
$testString = "Text";
public function testOverwrite() {
$testString = "Text";
$this->output->expects($this->at(0))->method('_write')
->with($testString);
$this->output->expects($this->at(0))->method('_write')
->with($testString);
$this->output->expects($this->at(1))->method('_write')
->with("");
$this->output->expects($this->at(1))->method('_write')
->with("");
$this->output->expects($this->at(2))->method('_write')
->with("Overwriting text");
$this->output->expects($this->at(2))->method('_write')
->with("Overwriting text");
$this->output->write($testString, 0);
$this->output->overwrite("Overwriting text");
}
$this->output->write($testString, 0);
$this->output->overwrite("Overwriting text");
}
/**
* test getting a style.

View file

@ -1,4 +1,17 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.8
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses("ProgressShellHelper", "Console/Helper");
App::uses("ConsoleOutputStub", "TestSuite/Stub");
@ -7,211 +20,204 @@ App::uses("ConsoleOutputStub", "TestSuite/Stub");
* @property ConsoleOutputStub $consoleOutput
* @property ProgressShellHelper $helper
*/
class ProgressShellHelperTest extends CakeTestCase
{
class ProgressShellHelperTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->consoleOutput = new ConsoleOutputStub();
$this->helper = new ProgressShellHelper($this->consoleOutput);
}
$this->consoleOutput = new ConsoleOutputStub();
$this->helper = new ProgressShellHelper($this->consoleOutput);
}
/**
* Test that a callback is required.*
* @expectedException \RuntimeException
*/
public function testOutputFailure()
{
$this->helper->output(['not a callback']);
}
/**
* Test that a callback is required.*
*
* @expectedException \RuntimeException
* @return void
*/
public function testOutputFailure() {
$this->helper->output(['not a callback']);
}
/**
* Test that the callback is invoked until 100 is reached.
*
* @return void
*/
public function testOutputSuccess()
{
$this->helper->output([function ($progress) {
$progress->increment(20);
}]);
$expected = [
'',
'==============> 20%',
'',
'=============================> 40%',
'',
'============================================> 60%',
'',
'===========================================================> 80%',
'',
'==========================================================================> 100%',
'',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test that the callback is invoked until 100 is reached.
*
* @return void
*/
public function testOutputSuccess() {
$this->helper->output([function ($progress) {
$progress->increment(20);
}]);
$expected = [
'',
'==============> 20%',
'',
'=============================> 40%',
'',
'============================================> 60%',
'',
'===========================================================> 80%',
'',
'==========================================================================> 100%',
'',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with options
*
* @return void
*/
public function testOutputSuccessOptions()
{
$this->helper->output([
'total' => 10,
'width' => 20,
'callback' => function ($progress) {
$progress->increment(2);
}
]);
$expected = [
'',
'==> 20%',
'',
'=====> 40%',
'',
'========> 60%',
'',
'===========> 80%',
'',
'==============> 100%',
'',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with options
*
* @return void
*/
public function testOutputSuccessOptions() {
$this->helper->output([
'total' => 10,
'width' => 20,
'callback' => function ($progress) {
$progress->increment(2);
}
]);
$expected = [
'',
'==> 20%',
'',
'=====> 40%',
'',
'========> 60%',
'',
'===========> 80%',
'',
'==============> 100%',
'',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test using the helper manually.
*
* @return void
*/
public function testIncrementAndRender()
{
$this->helper->init();
$this->helper->increment(20);
$this->helper->draw();
$this->helper->increment(40);
$this->helper->draw();
$this->helper->increment(40);
$this->helper->draw();
$expected = [
'',
'==============> 20%',
'',
'============================================> 60%',
'',
'==========================================================================> 100%',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test using the helper manually.
*
* @return void
*/
public function testIncrementAndRender() {
$this->helper->init();
$this->helper->increment(20);
$this->helper->draw();
$this->helper->increment(40);
$this->helper->draw();
$this->helper->increment(40);
$this->helper->draw();
$expected = [
'',
'==============> 20%',
'',
'============================================> 60%',
'',
'==========================================================================> 100%',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test negative numbers
*
* @return void
*/
public function testIncrementWithNegatives()
{
$this->helper->init();
$this->helper->increment(40);
$this->helper->draw();
$this->helper->increment(-60);
$this->helper->draw();
$this->helper->increment(80);
$this->helper->draw();
$expected = [
'',
'=============================> 40%',
'',
' 0%',
'',
'===========================================================> 80%',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test negative numbers
*
* @return void
*/
public function testIncrementWithNegatives() {
$this->helper->init();
$this->helper->increment(40);
$this->helper->draw();
$this->helper->increment(-60);
$this->helper->draw();
$this->helper->increment(80);
$this->helper->draw();
$expected = [
'',
'=============================> 40%',
'',
' 0%',
'',
'===========================================================> 80%',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test increment and draw with options
*
* @return void
*/
public function testIncrementWithOptions()
{
$this->helper->init([
'total' => 10,
'width' => 20,
]);
$this->helper->increment(4);
$this->helper->draw();
$this->helper->increment(4);
$this->helper->draw();
$this->helper->increment(4);
$this->helper->draw();
/**
* Test increment and draw with options
*
* @return void
*/
public function testIncrementWithOptions() {
$this->helper->init([
'total' => 10,
'width' => 20,
]);
$this->helper->increment(4);
$this->helper->draw();
$this->helper->increment(4);
$this->helper->draw();
$this->helper->increment(4);
$this->helper->draw();
$expected = [
'',
'=====> 40%',
'',
'===========> 80%',
'',
'==============> 100%',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
$expected = [
'',
'=====> 40%',
'',
'===========> 80%',
'',
'==============> 100%',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test increment and draw with value that makes the pad
* be a float
*
* @return void
*/
public function testIncrementFloatPad()
{
$this->helper->init([
'total' => 50
]);
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(3);
$this->helper->draw();
$this->helper->increment(4);
$this->helper->draw();
$this->helper->increment(8);
$this->helper->draw();
$expected = [
'',
'=========> 14%',
'',
'====================> 28%',
'',
'==============================> 42%',
'',
'=========================================> 56%',
'',
'===================================================> 70%',
'',
'========================================================> 76%',
'',
'==============================================================> 84%',
'',
'==========================================================================> 100%',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test increment and draw with value that makes the pad
* be a float
*
* @return void
*/
public function testIncrementFloatPad() {
$this->helper->init([
'total' => 50
]);
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(7);
$this->helper->draw();
$this->helper->increment(3);
$this->helper->draw();
$this->helper->increment(4);
$this->helper->draw();
$this->helper->increment(8);
$this->helper->draw();
$expected = [
'',
'=========> 14%',
'',
'====================> 28%',
'',
'==============================> 42%',
'',
'=========================================> 56%',
'',
'===================================================> 70%',
'',
'========================================================> 76%',
'',
'==============================================================> 84%',
'',
'==========================================================================> 100%',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
}

View file

@ -1,4 +1,17 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.8
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses("TableShellHelper", "Console/Helper");
App::uses("ConsoleOutputStub", "TestSuite/Stub");
@ -7,184 +20,182 @@ App::uses("ConsoleOutputStub", "TestSuite/Stub");
* @property ConsoleOutputStub $consoleOutput
* @property TableShellHelper $helper
*/
class TableShellHelperTest extends CakeTestCase
{
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
class TableShellHelperTest extends CakeTestCase {
$this->consoleOutput = new ConsoleOutputStub();
$this->helper = new TableShellHelper($this->consoleOutput);
}
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
/**
* Test output
*
* @return void
*/
public function testDefaultOutput()
{
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with multibyte characters
*
* @return void
*/
public function testOutputUtf8()
{
$data = [
['Header 1', 'Head', 'Long Header'],
['short', 'ÄÄÄÜÜÜ', 'short'],
['Longer thing', 'longerish', 'Longest Value'],
];
$this->helper->output($data);
$expected = [
'+--------------+-----------+---------------+',
'| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
'+--------------+-----------+---------------+',
'| short | ÄÄÄÜÜÜ | short |',
'| Longer thing | longerish | Longest Value |',
'+--------------+-----------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output without headers
*
* @return void
*/
public function testOutputWithoutHeaderStyle()
{
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['headerStyle' => false]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| Header 1 | Header | Long Header |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with different header style
*
* @return void
*/
public function testOutputWithDifferentHeaderStyle()
{
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['headerStyle' => 'error']);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output without table headers
*
* @return void
*/
public function testOutputWithoutHeaders()
{
$data = [
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['headers' => false]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with row separator
*
* @return void
*/
public function testOutputWithRowSeparator()
{
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value']
];
$this->helper->config(['rowSeparator' => true]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'+--------------+---------------+---------------+',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with row separator and no headers
*
* @return void
*/
public function testOutputWithRowSeparatorAndHeaders()
{
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['rowSeparator' => true]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'+--------------+---------------+---------------+',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
$this->consoleOutput = new ConsoleOutputStub();
$this->helper = new TableShellHelper($this->consoleOutput);
}
/**
* Test output
*
* @return void
*/
public function testDefaultOutput() {
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with multibyte characters
*
* @return void
*/
public function testOutputUtf8() {
$data = [
['Header 1', 'Head', 'Long Header'],
['short', 'ÄÄÄÜÜÜ', 'short'],
['Longer thing', 'longerish', 'Longest Value'],
];
$this->helper->output($data);
$expected = [
'+--------------+-----------+---------------+',
'| <info>Header 1</info> | <info>Head</info> | <info>Long Header</info> |',
'+--------------+-----------+---------------+',
'| short | ÄÄÄÜÜÜ | short |',
'| Longer thing | longerish | Longest Value |',
'+--------------+-----------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output without headers
*
* @return void
*/
public function testOutputWithoutHeaderStyle() {
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['headerStyle' => false]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| Header 1 | Header | Long Header |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with different header style
*
* @return void
*/
public function testOutputWithDifferentHeaderStyle() {
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['headerStyle' => 'error']);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <error>Header 1</error> | <error>Header</error> | <error>Long Header</error> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output without table headers
*
* @return void
*/
public function testOutputWithoutHeaders() {
$data = [
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['headers' => false]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with row separator
*
* @return void
*/
public function testOutputWithRowSeparator() {
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value']
];
$this->helper->config(['rowSeparator' => true]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'+--------------+---------------+---------------+',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
/**
* Test output with row separator and no headers
*
* @return void
*/
public function testOutputWithRowSeparatorAndHeaders() {
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
['Longer thing', 'short', 'Longest Value'],
];
$this->helper->config(['rowSeparator' => true]);
$this->helper->output($data);
$expected = [
'+--------------+---------------+---------------+',
'| <info>Header 1</info> | <info>Header</info> | <info>Long Header</info> |',
'+--------------+---------------+---------------+',
'| short | Longish thing | short |',
'+--------------+---------------+---------------+',
'| Longer thing | short | Longest Value |',
'+--------------+---------------+---------------+',
];
$this->assertEquals($expected, $this->consoleOutput->messages());
}
}

View file

@ -981,20 +981,18 @@ TEXT;
*
* @return void
*/
public function testGetInstanceOfHelper()
{
$actual = $this->Shell->helper("progress");
$this->assertInstanceOf("ProgressShellHelper", $actual);
}
public function testGetInstanceOfHelper() {
$actual = $this->Shell->helper("progress");
$this->assertInstanceOf("ProgressShellHelper", $actual);
}
/**
* Test getting an invalid helper
*
* @expectedException RunTimeException
* @return void
*/
public function testGetInvalidHelper()
{
$this->Shell->helper("tomato");
}
public function testGetInvalidHelper() {
$this->Shell->helper("tomato");
}
}

View file

@ -1,54 +1,66 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.8
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses("ConsoleOutput", "Console");
/**
* StubOutput makes testing shell commands/shell helpers easier.
*
* You can use this class by injecting it into a Helper instance:
*
* ```
* App::uses("ConsoleOutputStub", "TestSuite/Stub");
*
* $output = new ConsoleOutputStub();
* $helper = new ProgressHelper($output);
* ```
*/
class ConsoleOutputStub extends ConsoleOutput
{
* StubOutput makes testing shell commands/shell helpers easier.
*
* You can use this class by injecting it into a Helper instance:
*
* ```
* App::uses("ConsoleOutputStub", "TestSuite/Stub");
*
* $output = new ConsoleOutputStub();
* $helper = new ProgressHelper($output);
* ```
*/
class ConsoleOutputStub extends ConsoleOutput {
/**
* Buffered messages.
*
* @var array
*/
protected $_out = [];
* Buffered messages.
*
* @var array
*/
protected $_out = [];
/**
* The number of bytes written by last call to write
*
* @var int
*/
protected $_lastWritten = 0;
protected $_lastWritten = 0;
/**
* Write output to the buffer.
*
* @param string|array $message A string or an array of strings to output
* @param int $newlines Number of newlines to append
* @return void
*/
public function write($message, $newlines = 1)
{
foreach ((array)$message as $line) {
$this->_out[] = $line;
$this->_lastWritten = strlen($line);
}
$newlines--;
while ($newlines > 0) {
$this->_out[] = '';
$this->_lastWritten = 0;
$newlines--;
}
}
* Write output to the buffer.
*
* @param string|array $message A string or an array of strings to output
* @param int $newlines Number of newlines to append
* @return void
*/
public function write($message, $newlines = 1) {
foreach ((array)$message as $line) {
$this->_out[] = $line;
$this->_lastWritten = strlen($line);
}
$newlines--;
while ($newlines > 0) {
$this->_out[] = '';
$this->_lastWritten = 0;
$newlines--;
}
}
/**
* Overwrite output already written to the buffer.
@ -59,21 +71,19 @@ class ConsoleOutputStub extends ConsoleOutput
* length of the last message output.
* @return void
*/
public function overwrite($message, $newlines = 1, $size = null)
{
//insert an empty array to mock deletion of existing output
$this->_out[] = "";
//append new message to output
$this->write($message, $newlines);
}
public function overwrite($message, $newlines = 1, $size = null) {
//insert an empty array to mock deletion of existing output
$this->_out[] = "";
//append new message to output
$this->write($message, $newlines);
}
/**
* Get the buffered output.
*
* @return array
*/
public function messages()
{
return $this->_out;
}
/**
* Get the buffered output.
*
* @return array
*/
public function messages() {
return $this->_out;
}
}