Set the redraw frequency at least to 1. Setting it to 0 would otherwise produce an error.

This commit is contained in:
Dominik Ritter 2015-11-27 19:24:25 +01:00 committed by Christophe Coevoet
parent 42a9da9588
commit a1c207c7d1
2 changed files with 25 additions and 7 deletions

View File

@ -67,10 +67,8 @@ class ProgressBar
// disable overwrite when output does not support ANSI codes. // disable overwrite when output does not support ANSI codes.
$this->overwrite = false; $this->overwrite = false;
if ($this->max > 10) { // set a reasonable redraw frequency so output isn't flooded
// set a reasonable redraw frequency so output isn't flooded $this->setRedrawFrequency($max / 10);
$this->setRedrawFrequency($max / 10);
}
} }
$this->startTime = time(); $this->startTime = time();
@ -316,11 +314,11 @@ class ProgressBar
/** /**
* Sets the redraw frequency. * Sets the redraw frequency.
* *
* @param int $freq The frequency in steps * @param int|float $freq The frequency in steps
*/ */
public function setRedrawFrequency($freq) public function setRedrawFrequency($freq)
{ {
$this->redrawFreq = (int) $freq; $this->redrawFreq = max((int) $freq, 1);
} }
/** /**

View File

@ -296,7 +296,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testRedrawFrequency() public function testRedrawFrequency()
{ {
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($output = $this->getOutputStream(), 6)); $bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream(), 6));
$bar->expects($this->exactly(4))->method('display'); $bar->expects($this->exactly(4))->method('display');
$bar->setRedrawFrequency(2); $bar->setRedrawFrequency(2);
@ -307,6 +307,26 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
$bar->advance(1); $bar->advance(1);
} }
public function testRedrawFrequencyIsAtLeastOneIfZeroGiven()
{
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream()));
$bar->expects($this->exactly(2))->method('display');
$bar->setRedrawFrequency(0);
$bar->start();
$bar->advance();
}
public function testRedrawFrequencyIsAtLeastOneIfSmallerOneGiven()
{
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream()));
$bar->expects($this->exactly(2))->method('display');
$bar->setRedrawFrequency(0.9);
$bar->start();
$bar->advance();
}
/** /**
* @requires extension mbstring * @requires extension mbstring
*/ */