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

Appearance settings

[Console] progress bar fix #16490

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ProgressBar: fix tests
  • Loading branch information
TomasVotruba committed Nov 15, 2015
commit cf0e411bc9a5b5d12bd2a7ec42e9f9afd0d3da25
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ public function getTerminalHeight()
}

/**
* Sets terminal dimensions.
*
* Can be useful to force terminal dimensions for functional tests.
*
* @param int $width
* @param int $height
* {@inheritdoc}
*/
public function setTerminalDimensions($width, $height)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public function getTerminalWidth();
* @return int|null
*/
public function getTerminalHeight();

/**
* Sets terminal dimensions.
*
* Can be useful to force terminal dimensions for functional tests.
*
* @param int $width
* @param int $height
*/
public function setTerminalDimensions($width, $height);
}
81 changes: 47 additions & 34 deletions 81 src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider;
use Symfony\Component\Console\Terminal\TerminalDimensionsProviderInterface;

/**
* @group time-sensitive
Expand All @@ -22,7 +24,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
{
public function testMultipleStart()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->advance();
$bar->start();
Expand All @@ -38,7 +40,7 @@ public function testMultipleStart()

public function testAdvance()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->advance();

Expand All @@ -52,7 +54,7 @@ public function testAdvance()

public function testAdvanceWithStep()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->advance(5);

Expand All @@ -66,7 +68,7 @@ public function testAdvanceWithStep()

public function testAdvanceMultipleTimes()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->advance(3);
$bar->advance(2);
Expand All @@ -82,7 +84,7 @@ public function testAdvanceMultipleTimes()

public function testAdvanceOverMax()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider());
$bar->setProgress(9);
$bar->advance();
$bar->advance();
Expand All @@ -105,7 +107,7 @@ public function testFormat()
;

// max in construct, no format
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->advance(10);
$bar->finish();
Expand All @@ -114,7 +116,7 @@ public function testFormat()
$this->assertEquals($expected, stream_get_contents($output->getStream()));

// max in start, no format
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->start(10);
$bar->advance(10);
$bar->finish();
Expand All @@ -123,7 +125,7 @@ public function testFormat()
$this->assertEquals($expected, stream_get_contents($output->getStream()));

// max in construct, explicit format before
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider());
$bar->setFormat('normal');
$bar->start();
$bar->advance(10);
Expand All @@ -133,7 +135,7 @@ public function testFormat()
$this->assertEquals($expected, stream_get_contents($output->getStream()));

// max in start, explicit format before
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->setFormat('normal');
$bar->start(10);
$bar->advance(10);
Expand All @@ -145,7 +147,7 @@ public function testFormat()

public function testCustomizations()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider());
$bar->setBarWidth(10);
$bar->setBarCharacter('_');
$bar->setEmptyBarCharacter(' ');
Expand All @@ -164,7 +166,7 @@ public function testCustomizations()

public function testDisplayWithoutStart()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
$bar->display();

rewind($output->getStream());
Expand All @@ -176,7 +178,7 @@ public function testDisplayWithoutStart()

public function testDisplayWithQuietVerbosity()
{
$bar = new ProgressBar($output = $this->getOutputStream(true, StreamOutput::VERBOSITY_QUIET), 50);
$bar = new ProgressBar($output = $this->getOutputStream(true, StreamOutput::VERBOSITY_QUIET), 50, $this->createTerminalDimensionsProvider());
$bar->display();

rewind($output->getStream());
Expand All @@ -188,7 +190,7 @@ public function testDisplayWithQuietVerbosity()

public function testFinishWithoutStart()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
$bar->finish();

rewind($output->getStream());
Expand All @@ -200,7 +202,7 @@ public function testFinishWithoutStart()

public function testPercent()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->display();
$bar->advance();
Expand All @@ -218,7 +220,7 @@ public function testPercent()

public function testOverwriteWithShorterLine()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
$bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
$bar->start();
$bar->display();
Expand All @@ -240,7 +242,7 @@ public function testOverwriteWithShorterLine()

public function testStartWithMax()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->setFormat('%current%/%max% [%bar%]');
$bar->start(50);
$bar->advance();
Expand All @@ -255,7 +257,7 @@ public function testStartWithMax()

public function testSetCurrentProgress()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->display();
$bar->advance();
Expand Down Expand Up @@ -288,7 +290,7 @@ public function testSetCurrentBeforeStarting()
*/
public function testRegressProgress()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->setProgress(15);
$bar->setProgress(10);
Expand All @@ -309,7 +311,7 @@ public function testRedrawFrequency()

public function testMultiByteSupport()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->setBarCharacter('■');
$bar->advance(3);
Expand All @@ -324,7 +326,7 @@ public function testMultiByteSupport()

public function testClear()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->setProgress(25);
$bar->clear();
Expand All @@ -340,7 +342,7 @@ public function testClear()

public function testPercentNotHundredBeforeComplete()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 200);
$bar = new ProgressBar($output = $this->getOutputStream(), 200, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->display();
$bar->advance(199);
Expand All @@ -358,7 +360,7 @@ public function testPercentNotHundredBeforeComplete()

public function testNonDecoratedOutput()
{
$bar = new ProgressBar($output = $this->getOutputStream(false), 200);
$bar = new ProgressBar($output = $this->getOutputStream(false), 200, $this->createTerminalDimensionsProvider());
$bar->start();

for ($i = 0; $i < 200; ++$i) {
Expand Down Expand Up @@ -386,7 +388,7 @@ public function testNonDecoratedOutput()

public function testNonDecoratedOutputWithClear()
{
$bar = new ProgressBar($output = $this->getOutputStream(false), 50);
$bar = new ProgressBar($output = $this->getOutputStream(false), 50, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->setProgress(25);
$bar->clear();
Expand All @@ -404,7 +406,7 @@ public function testNonDecoratedOutputWithClear()

public function testNonDecoratedOutputWithoutMax()
{
$bar = new ProgressBar($output = $this->getOutputStream(false));
$bar = new ProgressBar($output = $this->getOutputStream(false), 0, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->advance();

Expand All @@ -419,10 +421,10 @@ public function testNonDecoratedOutputWithoutMax()
public function testParallelBars()
{
$output = $this->getOutputStream();
$bar1 = new ProgressBar($output, 2);
$bar2 = new ProgressBar($output, 3);
$bar1 = new ProgressBar($output, 2, $this->createTerminalDimensionsProvider());
$bar2 = new ProgressBar($output, 3, $this->createTerminalDimensionsProvider());
$bar2->setProgressCharacter('#');
$bar3 = new ProgressBar($output);
$bar3 = new ProgressBar($output, 0, $this->createTerminalDimensionsProvider());

$bar1->start();
$output->write("\n");
Expand Down Expand Up @@ -479,7 +481,7 @@ public function testWithoutMax()
{
$output = $this->getOutputStream();

$bar = new ProgressBar($output);
$bar = new ProgressBar($output, 0, $this->createTerminalDimensionsProvider());
$bar->start();
$bar->advance();
$bar->advance();
Expand All @@ -502,7 +504,7 @@ public function testAddingPlaceholderFormatter()
ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) {
return $bar->getMaxSteps() - $bar->getProgress();
});
$bar = new ProgressBar($output = $this->getOutputStream(), 3);
$bar = new ProgressBar($output = $this->getOutputStream(), 3, $this->createTerminalDimensionsProvider());
$bar->setFormat(' %remaining_steps% [%bar%]');

$bar->start();
Expand All @@ -520,7 +522,7 @@ public function testAddingPlaceholderFormatter()

public function testMultilineFormat()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 3);
$bar = new ProgressBar($output = $this->getOutputStream(), 3, $this->createTerminalDimensionsProvider());
$bar->setFormat("%bar%\nfoobar");

$bar->start();
Expand All @@ -540,7 +542,7 @@ public function testMultilineFormat()

public function testAnsiColorsAndEmojis()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 15);
$bar = new ProgressBar($output = $this->getOutputStream(), 15, $this->createTerminalDimensionsProvider());
ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) {
static $i = 0;
$mem = 100000 * $i;
Expand Down Expand Up @@ -583,7 +585,7 @@ public function testAnsiColorsAndEmojis()

public function testSetFormat()
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->setFormat('normal');
$bar->start();
rewind($output->getStream());
Expand All @@ -592,7 +594,7 @@ public function testSetFormat()
stream_get_contents($output->getStream())
);

$bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider());
$bar->setFormat('normal');
$bar->start();
rewind($output->getStream());
Expand All @@ -607,7 +609,7 @@ public function testSetFormat()
*/
public function testFormatsWithoutMax($format)
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider());
$bar->setFormat($format);
$bar->start();

Expand Down Expand Up @@ -641,4 +643,15 @@ protected function generateOutput($expected)

return "\x0D".($count ? sprintf("\033[%dA", $count) : '').$expected;
}

/**
* @return TerminalDimensionsProviderInterface
*/
private function createTerminalDimensionsProvider()
{
$terminalDimensionsProvider = new TerminalDimensionsProvider();
$terminalDimensionsProvider->setTerminalDimensions(800, 5);

return $terminalDimensionsProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,7 @@ public function testGetTerminalDimensions()
$dimensions = $this->terminalDimensionsProvider->getTerminalDimensions();
$this->assertCount(2, $dimensions);

if ($this->isUnixEnvironment()) {
$this->assertSame((int) exec('tput cols'), $dimensions[0]);
}
}

public function testGetTerminalWidth()
{
$width = $this->terminalDimensionsProvider->getTerminalWidth();
if ($this->isUnixEnvironment()) {
$this->assertSame((int) exec('tput cols'), $width);
}
}

/**
* @return bool
*/
private function isUnixEnvironment()
{
return '/' === DIRECTORY_SEPARATOR;
$this->terminalDimensionsProvider->setTerminalDimensions(100, 50);
$this->assertSame(array(100, 50), $this->terminalDimensionsProvider->getTerminalDimensions());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.