Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitceecd00

Browse files
author
James Halsall
committed
[Console] Add ability to regress the ProgressBar
1 parent3521105 commitceecd00

File tree

2 files changed

+83
-19
lines changed

2 files changed

+83
-19
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,22 @@ public function start($max = null)
339339
* Advances the progress output X steps.
340340
*
341341
* @param int $step Number of steps to advance
342-
*
343-
* @throws LogicException
344342
*/
345343
publicfunctionadvance($step =1)
346344
{
347345
$this->setProgress($this->step +$step);
348346
}
349347

348+
/**
349+
* Regresses the progress output X steps.
350+
*
351+
* @param int $step Number of steps to regress
352+
*/
353+
publicfunctionregress($step =1)
354+
{
355+
$this->setProgress($this->step -$step);
356+
}
357+
350358
/**
351359
* Sets whether to overwrite the progressbar, false for new line.
352360
*
@@ -361,18 +369,15 @@ public function setOverwrite($overwrite)
361369
* Sets the current progress.
362370
*
363371
* @param int $step The current progress
364-
*
365-
* @throws LogicException
366372
*/
367373
publicfunctionsetProgress($step)
368374
{
369375
$step = (int)$step;
370-
if ($step <$this->step) {
371-
thrownewLogicException('You can\'t regress the progress bar.');
372-
}
373376

374377
if ($this->max &&$step >$this->max) {
375378
$this->max =$step;
379+
}elseif ($step <0) {
380+
$step =0;
376381
}
377382

378383
$prevPeriod = (int) ($this->step /$this->redrawFreq);

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

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,77 @@ public function testAdvanceOverMax()
9696
);
9797
}
9898

99+
publicfunctiontestRegress()
100+
{
101+
$bar =newProgressBar($output =$this->getOutputStream());
102+
$bar->start();
103+
$bar->advance();
104+
$bar->advance();
105+
$bar->regress();
106+
107+
rewind($output->getStream());
108+
$this->assertEquals(
109+
' 0 [>---------------------------]'.
110+
$this->generateOutput(' 1 [->--------------------------]').
111+
$this->generateOutput(' 2 [-->-------------------------]').
112+
$this->generateOutput(' 1 [->--------------------------]'),
113+
stream_get_contents($output->getStream())
114+
);
115+
}
116+
117+
publicfunctiontestRegressWithStep()
118+
{
119+
$bar =newProgressBar($output =$this->getOutputStream());
120+
$bar->start();
121+
$bar->advance(4);
122+
$bar->advance(4);
123+
$bar->regress(2);
124+
125+
rewind($output->getStream());
126+
$this->assertEquals(
127+
' 0 [>---------------------------]'.
128+
$this->generateOutput(' 4 [---->-----------------------]').
129+
$this->generateOutput(' 8 [-------->-------------------]').
130+
$this->generateOutput(' 6 [------>---------------------]'),
131+
stream_get_contents($output->getStream())
132+
);
133+
}
134+
135+
publicfunctiontestRegressMultipleTimes()
136+
{
137+
$bar =newProgressBar($output =$this->getOutputStream());
138+
$bar->start();
139+
$bar->advance(3);
140+
$bar->advance(3);
141+
$bar->regress(1);
142+
$bar->regress(2);
143+
144+
rewind($output->getStream());
145+
$this->assertEquals(
146+
' 0 [>---------------------------]'.
147+
$this->generateOutput(' 3 [--->------------------------]').
148+
$this->generateOutput(' 6 [------>---------------------]').
149+
$this->generateOutput(' 5 [----->----------------------]').
150+
$this->generateOutput(' 3 [--->------------------------]'),
151+
stream_get_contents($output->getStream())
152+
);
153+
}
154+
155+
publicfunctiontestRegressBelowMin()
156+
{
157+
$bar =newProgressBar($output =$this->getOutputStream(),10);
158+
$bar->setProgress(1);
159+
$bar->regress();
160+
$bar->regress();
161+
162+
rewind($output->getStream());
163+
$this->assertEquals(
164+
' 1/10 [==>-------------------------] 10%'.
165+
$this->generateOutput(' 0/10 [>---------------------------] 0%'),
166+
stream_get_contents($output->getStream())
167+
);
168+
}
169+
99170
publicfunctiontestFormat()
100171
{
101172
$expected =
@@ -282,18 +353,6 @@ public function testSetCurrentBeforeStarting()
282353
$this->assertNotNull($bar->getStartTime());
283354
}
284355

285-
/**
286-
* @expectedException \LogicException
287-
* @expectedExceptionMessage You can't regress the progress bar
288-
*/
289-
publicfunctiontestRegressProgress()
290-
{
291-
$bar =newProgressBar($output =$this->getOutputStream(),50);
292-
$bar->start();
293-
$bar->setProgress(15);
294-
$bar->setProgress(10);
295-
}
296-
297356
publicfunctiontestRedrawFrequency()
298357
{
299358
$bar =$this->getMock('Symfony\Component\Console\Helper\ProgressBar',array('display'),array($this->getOutputStream(),6));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp