diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 7c0c09a97d..c2c6474e4e 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -295,6 +295,13 @@ final class ProgressBar } } + public function setMaxSteps(int $max) + { + $this->format = null; + $this->max = max(0, $max); + $this->stepWidth = $this->max ? Helper::strlen((string) $this->max) : 4; + } + /** * Finishes the progress output. */ @@ -362,12 +369,6 @@ final class ProgressBar $this->formatLineCount = substr_count($this->format, "\n"); } - private function setMaxSteps(int $max) - { - $this->max = max(0, $max); - $this->stepWidth = $this->max ? Helper::strlen((string) $this->max) : 4; - } - /** * Overwrites a previous message to the output. */ diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index aca64914a9..09e7c1a94c 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -592,6 +592,29 @@ class ProgressBarTest extends TestCase ); } + public function testSettingMaxStepsDuringProgressing() + { + $output = $this->getOutputStream(); + $bar = new ProgressBar($output); + $bar->start(); + $bar->setProgress(2); + $bar->setMaxSteps(10); + $bar->setProgress(5); + $bar->setMaxSteps(100); + $bar->setProgress(10); + $bar->finish(); + + rewind($output->getStream()); + $this->assertEquals( + rtrim(' 0 [>---------------------------]'). + rtrim($this->generateOutput(' 2 [-->-------------------------]')). + rtrim($this->generateOutput(' 5/10 [==============>-------------] 50%')). + rtrim($this->generateOutput(' 10/100 [==>-------------------------] 10%')). + rtrim($this->generateOutput(' 100/100 [============================] 100%')), + stream_get_contents($output->getStream()) + ); + } + public function testWithSmallScreen() { $output = $this->getOutputStream();