Skip to content

Commit 5549336

Browse files
committed
bug symfony#16742 [Console][ProgressBar] redrawFrequency should never be 0 (dritter)
This PR was submitted for the 2.8 branch but it was merged into the 2.7 branch instead (closes symfony#16742). Discussion ---------- [Console][ProgressBar] redrawFrequency should never be 0 Set the redraw frequency at least to 1. Setting it to 0 would otherwise produce a "division by zero" error. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | ### why? I had a piece of code that used a ProgressBar and set the redraw frequency to a calculated value. In some cases this calculated value was 0 (or just smaller than 1, internally a cast to `int` is made). In the `setProgress` the redraw frequency is used for a calculation of the period. There the error happens (and shows that redraw frequency should never be 0). ### Ticket There is no ticket concerning this issue. I could do one, if you want. ### Tests This gets tested implicitly in `ProgressBarTest::testNonDecoratedOutputWithoutMax`. Commits ------- a1c207c Set the redraw frequency at least to 1. Setting it to 0 would otherwise produce an error.
2 parents 42a9da9 + a1c207c commit 5549336

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ public function __construct(OutputInterface $output, $max = 0)
6767
// disable overwrite when output does not support ANSI codes.
6868
$this->overwrite = false;
6969

70-
if ($this->max > 10) {
71-
// set a reasonable redraw frequency so output isn't flooded
72-
$this->setRedrawFrequency($max / 10);
73-
}
70+
// set a reasonable redraw frequency so output isn't flooded
71+
$this->setRedrawFrequency($max / 10);
7472
}
7573

7674
$this->startTime = time();
@@ -316,11 +314,11 @@ public function setFormat($format)
316314
/**
317315
* Sets the redraw frequency.
318316
*
319-
* @param int $freq The frequency in steps
317+
* @param int|float $freq The frequency in steps
320318
*/
321319
public function setRedrawFrequency($freq)
322320
{
323-
$this->redrawFreq = (int) $freq;
321+
$this->redrawFreq = max((int) $freq, 1);
324322
}
325323

326324
/**

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public function testRegressProgress()
296296

297297
public function testRedrawFrequency()
298298
{
299-
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($output = $this->getOutputStream(), 6));
299+
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream(), 6));
300300
$bar->expects($this->exactly(4))->method('display');
301301

302302
$bar->setRedrawFrequency(2);
@@ -307,6 +307,26 @@ public function testRedrawFrequency()
307307
$bar->advance(1);
308308
}
309309

310+
public function testRedrawFrequencyIsAtLeastOneIfZeroGiven()
311+
{
312+
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream()));
313+
314+
$bar->expects($this->exactly(2))->method('display');
315+
$bar->setRedrawFrequency(0);
316+
$bar->start();
317+
$bar->advance();
318+
}
319+
320+
public function testRedrawFrequencyIsAtLeastOneIfSmallerOneGiven()
321+
{
322+
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream()));
323+
324+
$bar->expects($this->exactly(2))->method('display');
325+
$bar->setRedrawFrequency(0.9);
326+
$bar->start();
327+
$bar->advance();
328+
}
329+
310330
/**
311331
* @requires extension mbstring
312332
*/

0 commit comments

Comments
 (0)