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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 50 deletions.
20 changes: 11 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@
"zf2",
"progressbar"
],
"homepage": "https://github.com/zendframework/zend-progress-bar",
"autoload": {
"psr-4": {
"Zend\\ProgressBar\\": "src/"
}
},
"require": {
"php": ">=5.3.3",
"php": ">=5.3.23",
"zendframework/zend-stdlib": "self.version"
},
"require-dev": {
"zendframework/zend-json": "self.version",
"zendframework/zend-session": "self.version",
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
},
"suggest": {
"zendframework/zend-json": "Zend\\Json component",
"zendframework/zend-session": "To support progressbar persistent"
},
"extra": {
"branch-alias": {
"dev-master": "2.2-dev",
"dev-develop": "2.3-dev"
"dev-master": "2.3-dev",
"dev-develop": "2.4-dev"
}
},
"homepage": "https://github.com/zendframework/zend-progress-bar",
"autoload-dev": {
"psr-4": {
"ZendTest\\ProgressBar\\": "test/"
}
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master",
"phpunit/PHPUnit": "~4.0"
}
}
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;
}
}
87 changes: 53 additions & 34 deletions test/Adapter/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @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;
use Zend\Stdlib\StringUtils;

require_once 'MockupStream.php';

Expand All @@ -31,7 +32,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 +42,7 @@ public function testWindowsWidth()

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

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

Expand All @@ -51,7 +52,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 +62,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 +72,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 +81,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 +95,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 +190,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 +199,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 +295,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()));
}
}
2 changes: 1 addition & 1 deletion test/Adapter/JsPullTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
2 changes: 1 addition & 1 deletion test/Adapter/JsPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
2 changes: 1 addition & 1 deletion test/Adapter/MockupStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
2 changes: 1 addition & 1 deletion test/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down
2 changes: 1 addition & 1 deletion test/Upload/AbstractUploadHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down

0 comments on commit 7db806f

Please sign in to comment.