Skip to content

Commit

Permalink
Tweak console helpers.
Browse files Browse the repository at this point in the history
* Add clearing line to progress::output(). This makes usage easier.
* Move argument shifting to __call(). I feel this would end up in each
  of the helpers. Better to have the argument shifting in the wrapper
  method.
* Fix mistakes in ProgressHelper.
  • Loading branch information
markstory committed May 23, 2015
1 parent 352136c commit 3d27a4c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 30 deletions.
5 changes: 4 additions & 1 deletion src/Console/ConsoleIo.php
Expand Up @@ -399,7 +399,10 @@ public function helper($name, array $settings = [])
*/
public function __call($method, $args)
{
$helper = $this->helper($method, $args);
$helper = $this->helper($method);
if (count($args) === 1 && isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
return $helper->output($args);
}
}
5 changes: 3 additions & 2 deletions src/Shell/Helper/ProgressHelper.php
Expand Up @@ -62,7 +62,7 @@ public function output($args)
if (isset($args[0])) {
$args['callback'] = $args[0];
}
if (!$args['callback'] || !is_callable($args[0])) {
if (!$args['callback'] || !is_callable($args['callback'])) {
throw new RuntimeException('Callback option must be a callable.');
}
$this->init($args);
Expand All @@ -72,6 +72,7 @@ public function output($args)
$callback($this);
$this->draw();
}
$this->_io->out('');
}

/**
Expand Down Expand Up @@ -100,7 +101,7 @@ public function init(array $args = [])
*/
public function increment($num = 1)
{
$this->_progress = max(0, $this->_progress + $num);
$this->_progress = min(max(0, $this->_progress + $num), $this->_total);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Shell/Helper/TableHelper.php
Expand Up @@ -83,9 +83,6 @@ protected function _render($row, $widths)
*/
public function output($rows)
{
if (count($rows) === 1) {
$rows = $rows[0];
}
$widths = $this->_calculateWidths($rows);

$this->_rowSeparator($widths);
Expand Down
9 changes: 7 additions & 2 deletions tests/TestCase/Console/ConsoleIoTest.php
Expand Up @@ -380,9 +380,14 @@ public function testHelper()
*/
public function testHelperCall()
{
$this->out->expects($this->once())
$this->out->expects($this->at(0))
->method('write')
->with('It works!well ish');
$this->out->expects($this->at(1))
->method('write')
->with('It works!well ish');
$helper = $this->io->simple('well', 'ish');

$this->io->simple('well', 'ish');
$this->io->simple(['well', 'ish']);
}
}
1 change: 1 addition & 0 deletions tests/TestCase/Shell/Helper/ProgressHelperTest.php
Expand Up @@ -70,6 +70,7 @@ public function testOutputSuccess()
'===========================================================> 80%',
'',
'==========================================================================> 100%',
'',
];
$this->assertEquals($expected, $this->stub->messages());
}
Expand Down
22 changes: 0 additions & 22 deletions tests/TestCase/Shell/Helper/TableHelperTest.php
Expand Up @@ -63,28 +63,6 @@ public function testOutput()
$this->assertEquals($expected, $this->stub->messages());
}

/**
* Test output array shifting
*
* @return voi
*/
public function testOutputShifting()
{
$data = [
['Header 1', 'Header', 'Long Header'],
['short', 'Longish thing', 'short'],
];
$this->helper->output([$data]);
$expected = [
'+----------+---------------+-------------+',
'| Header 1 | Header | Long Header |',
'+----------+---------------+-------------+',
'| short | Longish thing | short |',
'+----------+---------------+-------------+',
];
$this->assertEquals($expected, $this->stub->messages());
}

/**
* Test output with multibyte characters
*
Expand Down

0 comments on commit 3d27a4c

Please sign in to comment.