fix bug with set max count, by start method in progress bar

This commit is contained in:
Viacheslav Sychov 2015-10-08 10:06:00 +01:00 committed by Fabien Potencier
parent c98ce2a6e3
commit 3cbfa63d05
2 changed files with 48 additions and 11 deletions

View File

@ -43,6 +43,7 @@ class ProgressBar
private $formatLineCount; private $formatLineCount;
private $messages; private $messages;
private $overwrite = true; private $overwrite = true;
private $formatSetByUser = false;
private static $formatters; private static $formatters;
private static $formats; private static $formats;
@ -72,7 +73,7 @@ class ProgressBar
} }
} }
$this->setFormat($this->determineBestFormat()); $this->setFormatInternal($this->determineBestFormat());
$this->startTime = time(); $this->startTime = time();
} }
@ -310,16 +311,8 @@ class ProgressBar
*/ */
public function setFormat($format) public function setFormat($format)
{ {
// try to use the _nomax variant if available $this->formatSetByUser = true;
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) { $this->setFormatInternal($format);
$this->format = self::getFormatDefinition($format.'_nomax');
} elseif (null !== self::getFormatDefinition($format)) {
$this->format = self::getFormatDefinition($format);
} else {
$this->format = $format;
}
$this->formatLineCount = substr_count($this->format, "\n");
} }
/** /**
@ -345,6 +338,10 @@ class ProgressBar
if (null !== $max) { if (null !== $max) {
$this->setMaxSteps($max); $this->setMaxSteps($max);
if (!$this->formatSetByUser) {
$this->setFormatInternal($this->determineBestFormat());
}
} }
$this->display(); $this->display();
@ -478,6 +475,25 @@ class ProgressBar
$this->overwrite(str_repeat("\n", $this->formatLineCount)); $this->overwrite(str_repeat("\n", $this->formatLineCount));
} }
/**
* Sets the progress bar format.
*
* @param string $format The format
*/
private function setFormatInternal($format)
{
// try to use the _nomax variant if available
if (!$this->max && null !== self::getFormatDefinition($format.'_nomax')) {
$this->format = self::getFormatDefinition($format.'_nomax');
} elseif (null !== self::getFormatDefinition($format)) {
$this->format = self::getFormatDefinition($format);
} else {
$this->format = $format;
}
$this->formatLineCount = substr_count($this->format, "\n");
}
/** /**
* Sets the progress bar maximal steps. * Sets the progress bar maximal steps.
* *

View File

@ -106,6 +106,27 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testFormatWhenMaxInConstructAndInStart()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->start();
$bar->advance(10);
$bar->finish();
rewind($output->getStream());
$maxInConstruct = stream_get_contents($output->getStream());
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->start(10);
$bar->advance(10);
$bar->finish();
rewind($output->getStream());
$maxInStart = stream_get_contents($output->getStream());
$this->assertEquals($maxInStart, $maxInConstruct);
}
public function testCustomizations() public function testCustomizations()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 10); $bar = new ProgressBar($output = $this->getOutputStream(), 10);