Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/zendframework/zendframework#6012-progressbar-mul…
Browse files Browse the repository at this point in the history
…tibyte-support'

Close zendframework/zendframework#6012
  • Loading branch information
Ocramius committed Apr 3, 2014
2 parents 13a38e9 + 4c48e4d commit e32ef93
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 35 deletions.
5 changes: 3 additions & 2 deletions src/Adapter/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,9 @@ public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $te
break;

case self::ELEMENT_TEXT:
$renderedElements[] = StringUtils::getWrapper($this->charset)->strPad(
substr($text, 0, $this->textWidth),
$wrapper = StringUtils::getWrapper($this->charset);
$renderedElements[] = $wrapper->strPad(
$wrapper->substr($text, 0, $this->textWidth),
$this->textWidth,
' ',
STR_PAD_RIGHT
Expand Down
32 changes: 32 additions & 0 deletions test/Adapter/ConsoleStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\ProgressBar\Adapter;

use Zend\ProgressBar\Adapter;

class ConsoleStub extends Adapter\Console
{
protected $lastOutput = null;

public function getLastOutput()
{
return $this->lastOutput;
}

protected function _outputData($data)
{
$this->lastOutput = $data;
}

public function getCharset()
{
return $this->charset;
}
}
86 changes: 53 additions & 33 deletions test/Adapter/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace ZendTest\ProgressBar\Adapter;

use Zend\ProgressBar\Adapter;
use ZendTest\ProgressBar\Adapter\ConsoleStub;
use Zend\Stdlib\StringUtils;

require_once 'MockupStream.php';

Expand All @@ -31,7 +33,7 @@ protected function tearDown()
public function testWindowsWidth()
{
if (substr(PHP_OS, 0, 3) === 'WIN') {
$adapter = new Stub();
$adapter = new ConsoleStub();
$adapter->notify(0, 100, 0, 0, null, null);
$this->assertEquals(79, strlen($adapter->getLastOutput()));
} else {
Expand All @@ -41,7 +43,7 @@ public function testWindowsWidth()

public function testStandardOutputStream()
{
$adapter = new Stub();
$adapter = new ConsoleStub();

$this->assertTrue(is_resource($adapter->getOutputStream()));

Expand All @@ -51,7 +53,7 @@ public function testStandardOutputStream()

public function testManualStandardOutputStream()
{
$adapter = new Stub(array('outputStream' => 'php://stdout'));
$adapter = new ConsoleStub(array('outputStream' => 'php://stdout'));

$this->assertTrue(is_resource($adapter->getOutputStream()));

Expand All @@ -61,7 +63,7 @@ public function testManualStandardOutputStream()

public function testManualErrorOutputStream()
{
$adapter = new Stub(array('outputStream' => 'php://stderr'));
$adapter = new ConsoleStub(array('outputStream' => 'php://stderr'));

$this->assertTrue(is_resource($adapter->getOutputStream()));

Expand All @@ -71,7 +73,7 @@ public function testManualErrorOutputStream()

public function testFixedWidth()
{
$adapter = new Stub(array('width' => 30));
$adapter = new ConsoleStub(array('width' => 30));
$adapter->notify(0, 100, 0, 0, null, null);

$this->assertEquals(' 0% [----------] ', $adapter->getLastOutput());
Expand All @@ -80,12 +82,12 @@ public function testFixedWidth()
public function testInvalidElement()
{
$this->setExpectedException('Zend\ProgressBar\Adapter\Exception\InvalidArgumentException', 'Invalid element found');
$adapter = new Stub(array('width' => 30, 'elements' => array('foo')));
$adapter = new ConsoleStub(array('width' => 30, 'elements' => array('foo')));
}

public function testCariageReturn()
{
$adapter = new Stub(array('width' => 30));
$adapter = new ConsoleStub(array('width' => 30));
$adapter->notify(0, 100, 0, 0, null, null);
$adapter->notify(0, 100, 0, 0, null, null);

Expand All @@ -94,89 +96,89 @@ public function testCariageReturn()

public function testBarLayout()
{
$adapter = new Stub(array('width' => 30));
$adapter = new ConsoleStub(array('width' => 30));
$adapter->notify(50, 100, .5, 0, null, null);

$this->assertContains(' 50% [#####-----]', $adapter->getLastOutput());
}

public function testBarOnly()
{
$adapter = new Stub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR)));
$adapter = new ConsoleStub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR)));
$adapter->notify(0, 100, 0, 0, null, null);

$this->assertEquals('[------------------]', $adapter->getLastOutput());
}

public function testPercentageOnly()
{
$adapter = new Stub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_PERCENT)));
$adapter = new ConsoleStub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_PERCENT)));
$adapter->notify(0, 100, 0, 0, null, null);

$this->assertEquals(' 0%', $adapter->getLastOutput());
}

public function testEtaOnly()
{
$adapter = new Stub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_ETA)));
$adapter = new ConsoleStub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_ETA)));
$adapter->notify(0, 100, 0, 0, null, null);

$this->assertEquals(' ', $adapter->getLastOutput());
}

public function testCustomOrder()
{
$adapter = new Stub(array('width' => 25, 'elements' => array(Adapter\Console::ELEMENT_ETA,
Adapter\Console::ELEMENT_PERCENT,
Adapter\Console::ELEMENT_BAR)));
$adapter = new ConsoleStub(array('width' => 25, 'elements' => array(Adapter\Console::ELEMENT_ETA,
Adapter\Console::ELEMENT_PERCENT,
Adapter\Console::ELEMENT_BAR)));
$adapter->notify(0, 100, 0, 0, null, null);

$this->assertEquals(' 0% [-----]', $adapter->getLastOutput());
}

public function testBarStyleIndicator()
{
$adapter = new Stub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barIndicatorChar' => '>'));
$adapter = new ConsoleStub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barIndicatorChar' => '>'));
$adapter->notify(10, 100, .1, 0, null, null);

$this->assertContains('[##>---------------]', $adapter->getLastOutput());
}

public function testBarStyleIndicatorWide()
{
$adapter = new Stub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barIndicatorChar' => '[]'));
$adapter = new ConsoleStub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barIndicatorChar' => '[]'));
$adapter->notify(10, 100, .1, 0, null, null);

$this->assertContains('[##[]--------------]', $adapter->getLastOutput());
}

public function testBarStyleLeftRightNormal()
{
$adapter = new Stub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barLeftChar' => '+', 'barRightChar' => ' '));
$adapter = new ConsoleStub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barLeftChar' => '+', 'barRightChar' => ' '));
$adapter->notify(10, 100, .1, 0, null, null);

$this->assertContains('[++ ]', $adapter->getLastOutput());
}

public function testBarStyleLeftRightWide()
{
$adapter = new Stub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barLeftChar' => '+-', 'barRightChar' => '=-'));
$adapter = new ConsoleStub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barLeftChar' => '+-', 'barRightChar' => '=-'));
$adapter->notify(10, 100, .1, 0, null, null);

$this->assertContains('[+-=-=-=-=-=-=-=-=-]', $adapter->getLastOutput());
}

public function testBarStyleLeftIndicatorRightWide()
{
$adapter = new Stub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barLeftChar' => '+-', 'barIndicatorChar' => '[]', 'barRightChar' => '=-'));
$adapter = new ConsoleStub(array('width' => 20, 'elements' => array(Adapter\Console::ELEMENT_BAR), 'barLeftChar' => '+-', 'barIndicatorChar' => '[]', 'barRightChar' => '=-'));
$adapter->notify(10, 100, .1, 0, null, null);

$this->assertContains('[+-[]=-=-=-=-=-=-=-]', $adapter->getLastOutput());
}

public function testEtaDelayDisplay()
{
$adapter = new Stub(array('width' => 100, 'elements' => array(Adapter\Console::ELEMENT_ETA)));
$adapter = new ConsoleStub(array('width' => 100, 'elements' => array(Adapter\Console::ELEMENT_ETA)));

$adapter->notify(33, 100, .33, 3, null, null);
$this->assertContains(' ', $adapter->getLastOutput());
Expand All @@ -189,7 +191,7 @@ public function testEtaDelayDisplay()

public function testEtaHighValue()
{
$adapter = new Stub(array('width' => 100, 'elements' => array(Adapter\Console::ELEMENT_ETA)));
$adapter = new ConsoleStub(array('width' => 100, 'elements' => array(Adapter\Console::ELEMENT_ETA)));

$adapter->notify(1, 100005, .001, 5, 100000, null);

Expand All @@ -198,15 +200,15 @@ public function testEtaHighValue()

public function testTextElementDefaultLength()
{
$adapter = new Stub(array('width' => 100, 'elements' => array(Adapter\Console::ELEMENT_TEXT, Adapter\Console::ELEMENT_BAR)));
$adapter = new ConsoleStub(array('width' => 100, 'elements' => array(Adapter\Console::ELEMENT_TEXT, Adapter\Console::ELEMENT_BAR)));
$adapter->notify(0, 100, 0, 0, null, 'foobar');

$this->assertContains('foobar [', $adapter->getLastOutput());
}

public function testTextElementCustomLength()
{
$adapter = new Stub(array('width' => 100, 'elements' => array(Adapter\Console::ELEMENT_TEXT, Adapter\Console::ELEMENT_BAR), 'textWidth' => 10));
$adapter = new ConsoleStub(array('width' => 100, 'elements' => array(Adapter\Console::ELEMENT_TEXT, Adapter\Console::ELEMENT_BAR), 'textWidth' => 10));
$adapter->notify(0, 100, 0, 0, null, 'foobar');

$this->assertContains('foobar [', $adapter->getLastOutput());
Expand Down Expand Up @@ -294,19 +296,37 @@ public function testSetInvalidFinishAction()
$adapter->setFinishAction('CUSTOM_FINISH_ACTION');
}

}

class Stub extends Adapter\Console
{
protected $_lastOutput = null;

public function getLastOutput()
/**
* @group 6012
*/
public function testMultibyteTruncateFixedWidth()
{
return $this->_lastOutput;
$outputWidth = 50;
$adapter = new ConsoleStub(array('width' => $outputWidth, 'elements' => array(Adapter\Console::ELEMENT_PERCENT,
Adapter\Console::ELEMENT_BAR,
Adapter\Console::ELEMENT_ETA,
Adapter\Console::ELEMENT_TEXT)));
$adapter->notify(21, 100, .21, 60, 60, 'ChineseTest 這是多字節長度裁剪的測試。我們希望能有超過20名中國字符的長字符串');
$this->assertEquals(' 21% [##-------] ETA 00:01:00 ChineseTest 這是多字節長度裁', $adapter->getLastOutput());

$wrapper = StringUtils::getWrapper($adapter->getCharset());
$this->assertEquals($outputWidth, $wrapper->strlen($adapter->getLastOutput()));
}

protected function _outputData($data)
/**
* @group 6012
*/
public function testMultibytePadFixedWidth()
{
$this->_lastOutput = $data;
$outputWidth = 50;
$adapter = new ConsoleStub(array('width' => $outputWidth, 'elements' => array(Adapter\Console::ELEMENT_PERCENT,
Adapter\Console::ELEMENT_BAR,
Adapter\Console::ELEMENT_ETA,
Adapter\Console::ELEMENT_TEXT)));
$adapter->notify(21, 100, .21, 60, 60, '這是');
$this->assertEquals(' 21% [##-------] ETA 00:01:00 這是 ', $adapter->getLastOutput());

$wrapper = StringUtils::getWrapper($adapter->getCharset());
$this->assertEquals($outputWidth, $wrapper->strlen($adapter->getLastOutput()));
}
}

0 comments on commit e32ef93

Please sign in to comment.