8000 [Console] added some basic tests for the ProgressHelper class · markross/symfony@7b32976 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b32976

Browse files
committed
[Console] added some basic tests for the ProgressHelper class
1 parent 729e3bf commit 7b32976

File tree

2 files changed

+99
-22
lines changed

2 files changed

+99
-22
lines changed

src/Symfony/Component/Console/Helper/ProgressHelper.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ProgressHelper extends Helper
3333
private $barChar = '=';
3434
private $emptyBarChar = '-';
3535
private $progressChar = '>';
36-
private $format = self::FORMAT_NORMAL_NOMAX;
36+
private $format = null;
3737
private $redrawFreq = 1;
3838

3939
private $barCharOriginal;
@@ -148,7 +148,7 @@ public function setEmptyBarCharacter($char)
148148
*
149149
* @param string $char A character
150150
*/
151-
public function setProgressChar($char)
151+
public function setProgressCharacter($char)
152152
{
153153
$this->progressChar = $char;
154154
}
@@ -186,27 +186,29 @@ public function start(OutputInterface $output, $max = null)
186186
$this->max = (int) $max;
187187
$this->output = $output;
188188

189-
switch ($output->getVerbosity()) {
190-
case OutputInterface::VERBOSITY_QUIET:
191-
$this->format = self::FORMAT_QUIET_NOMAX;
192-
if ($this->max > 0) {
193-
$this->format = self::FORMAT_QUIET;
194-
}
195-
break;
196-
case OutputInterface::VERBOSITY_VERBOSE:
197-
$this->format = self::FORMAT_VERBOSE_NOMAX;
198-
if ($this->max > 0) {
199-
$this->format = self::FORMAT_VERBOSE;
200-
}
201-
break;
202-
default:
203-
if ($this->max > 0) {
204-
$this->format = self::FORMAT_NORMAL;
205-
}
206-
break;
189+
if (null === $this->format) {
190+
switch ($output->getVerbosity()) {
191+
case OutputInterface::VERBOSITY_QUIET:
192+
$this->format = self::FORMAT_QUIET_NOMAX;
193+
if ($this->max > 0) {
194+
$this->format = self::FORMAT_QUIET;
195+
}
196+
break;
197+
case OutputInterface::VERBOSITY_VERBOSE:
198+
$this->format = self::FORMAT_VERBOSE_NOMAX;
199+
if ($this->max > 0) {
200+
$this->format = self::FORMAT_VERBOSE;
201+
}
202+
break;
203+
default:
204+
if ($this->max > 0) {
205+
$this->format = self::FORMAT_NORMAL;
206+
}
207+
break;
208+
}
207209
}
208210

209-
$this->inititalize();
211+
$this->initialize();
210212
}
211213

212214
/**
@@ -271,7 +273,7 @@ public function finish()
271273
/**
272274
* Initializes the progress helper.
273275
*/
274-
private function inititalize()
276+
private function initialize()
275277
{
276278
$this->formatVars = array();
277279
foreach ($this->defaultFormatVars as $var) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Helper;
13+
14+
use Symfony\Component\Console\Helper\ProgressHelper;
15+
use Symfony\Component\Console\Helper\HelperSet;
16+
use Symfony\Component\Console\Output\StreamOutput;
17+
18+
class ProgressHelperTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testAdvance()
21+
{
22+
$progress = new ProgressHelper();
23+
$progress->start($output = $this->getOutputStream());
24+
$progress->advance();
25+
26+
rewind($output->getStream());
27+
$this->assertEquals($this->generateOutput(' 1 [->--------------------------]'), stream_get_contents($output->getStream()));
28+
}
29+
30+
public function testAdvanceWithStep()
31+
{
32+
$progress = new ProgressHelper();
33+
$progress->start($output = $this->getOutputStream());
34+
$progress->advance(5);
35+
36+
rewind($output->getStream());
37+
$this->assertEquals($this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output 9E12 ->getStream()));
38+
}
39+
40+
public function testAdvanceMultipleTimes()
41+
{
42+
$progress = new ProgressHelper();
43+
$progress->start($output = $this->getOutputStream());
44+
$progress->advance(3);
45+
$progress->advance(2);
46+
47+
rewind($output->getStream());
48+
$this->assertEquals($this->generateOutput(' 3 [--->------------------------]').$this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output->getStream()));
49+
}
50+
51+
public function testCustomizations()
52+
{
53+
$progress = new ProgressHelper();
54+
$progress->setBarWidth(10);
55+
$progress->setBarCharacter('_');
56+
$progress->setEmptyBarCharacter(' ');
57+
$progress->setProgressCharacter('/');
58+
$progress->setFormat(' %current%/%max% [%bar%] %percent%%');
59+
$progress->start($output = $this->getOutputStream(), 10);
60+
$progress->advance();
61+
62+
rewind($output->getStream());
63+
$this->assertEquals($this->generateOutput(' 1/10 [_/ ] 10%'), stream_get_contents($output->getStream()));
64+
}
65+
66+
protected function getOutputStream()
67+
{
68+
return new StreamOutput(fopen('php://memory', 'r+', false));
69+
}
70+
71+
protected function generateOutput($expected)
72+
{
73+
return str_repeat("\x08", 80).$expected.str_repeat(' ', 80 - strlen($expected)).str_repeat("\x08", 80 - strlen($expected));
74+
}
75+
}

0 commit comments

Comments
 (0)
0