diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 7f4b2efb8d..0c0a3fc254 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -43,6 +43,7 @@ class ProgressBar private $formatLineCount; private $messages; private $overwrite = true; + private $formatSetByUser = false; private static $formatters; private static $formats; @@ -72,7 +73,7 @@ class ProgressBar } } - $this->setFormat($this->determineBestFormat()); + $this->setFormatInternal($this->determineBestFormat()); $this->startTime = time(); } @@ -310,16 +311,8 @@ class ProgressBar */ public function setFormat($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"); + $this->formatSetByUser = true; + $this->setFormatInternal($format); } /** @@ -345,6 +338,10 @@ class ProgressBar if (null !== $max) { $this->setMaxSteps($max); + + if (!$this->formatSetByUser) { + $this->setFormatInternal($this->determineBestFormat()); + } } $this->display(); @@ -478,6 +475,25 @@ class ProgressBar $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. * diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index 78f537543e..dbcc6e2a8a 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -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() { $bar = new ProgressBar($output = $this->getOutputStream(), 10);