8000 [Console] Add ability to regress the ProgressBar by jameshalsall · Pull Request #19824 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add ability to regress the ProgressBar #19824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,6 @@ public function start($max = null)
* Advances the progress output X steps.
*
* @param int $step Number of steps to advance
*
* @throws LogicException
*/
public function advance($step = 1)
{
Expand All @@ -361,18 +359,15 @@ public function setOverwrite($overwrite)
* Sets the current progress.
*
* @param int $step The current progress
*
* @throws LogicException
*/
public function setProgress($step)
{
$step = (int) $step;
if ($step < $this->step) {
throw new LogicException('You can\'t regress the progress bar.');
}

if ($this->max && $step > $this->max) {
$this->max = $step;
} elseif ($step < 0) {
$step = 0;
}

$prevPeriod = (int) ($this->step / $this->redrawFreq);
Expand Down
83 changes: 71 additions & 12 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,77 @@ public function testAdvanceOverMax()
);
}

public function testRegress()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->start();
$bar->advance();
$bar->advance();
$bar->advance(-1);

rewind($output->getStream());
$this->assertEquals(
' 0 [>---------------------------]'.
$this->generateOutput(' 1 [->--------------------------]').
$this->generateOutput(' 2 [-->-------------------------]').
$this->generateOutput(' 1 [->--------------------------]'),
stream_get_contents($output->getStream())
);
}

public function testRegressWithStep()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->start();
$bar->advance(4);
$bar->advance(4);
$bar->advance(-2);

rewind($output->getStream());
$this->assertEquals(
' 0 [>---------------------------]'.
$this->generateOutput(' 4 [---->-----------------------]').
$this->generateOutput(' 8 [-------->-------------------]').
$this->generateOutput(' 6 [------>---------------------]'),
stream_get_contents($output->getStream())
);
}

public function testRegressMultipleTimes()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->start();
$bar->advance(3);
$bar->advance(3);
$bar->advance(-1);
$bar->advance(-2);

rewind($output->getStream());
$this->assertEquals(
' 0 [>---------------------------]'.
$this->generateOutput(' 3 [--->------------------------]').
$this->generateOutput(' 6 [------>---------------------]').
$this->generateOutput(' 5 [----->----------------------]').
$this->generateOutput(' 3 [--->------------------------]'),
stream_get_contents($output->getStream())
);
}

public function testRegressBelowMin()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->setProgress(1);
$bar->advance(-1);
$bar->advance(-1);

rewind($output->getStream());
$this->assertEquals(
' 1/10 [==>-------------------------] 10%'.
$this->generateOutput(' 0/10 [>---------------------------] 0%'),
stream_get_contents($output->getStream())
);
}

public function testFormat()
{
$expected =
Expand Down Expand Up @@ -282,18 +353,6 @@ public function testSetCurrentBeforeStarting()
$this->assertNotNull($bar->getStartTime());
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage You can't regress the progress bar
*/
public function testRegressProgress()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar->start();
$bar->setProgress(15);
$bar->setProgress(10);
}

public function testRedrawFrequency()
{
$bar = $this->getMock('Symfony\Component\Console\Helper\ProgressBar', array('display'), array($this->getOutputStream(), 6));
Expand Down
0