fixed CS, simplified code

This commit is contained in:
Fabien Potencier 2016-06-09 13:42:05 +02:00
parent b030c24263
commit 8f206c86d7
6 changed files with 182 additions and 166 deletions

View File

@ -36,7 +36,7 @@ use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent; use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Exception\CommandNotFoundException; use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; use Symfony\Component\Console\Terminal;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
@ -66,24 +66,19 @@ class Application
private $definition; private $definition;
private $helperSet; private $helperSet;
private $dispatcher; private $dispatcher;
private $terminal;
private $defaultCommand; private $defaultCommand;
private $singleCommand; private $singleCommand;
/** /**
* @var TerminalDimensionsProvider * @param string $name The name of the application
* @param string $version The version of the application
*/ */
private $terminalDimensionsProvider; public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
/**
* @param string $name The name of the application
* @param string $version The version of the application
* @param TerminalDimensionsProvider $terminalDimensionsProvider
*/
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', TerminalDimensionsProvider $terminalDimensionsProvider = null)
{ {
$this->name = $name; $this->name = $name;
$this->version = $version; $this->version = $version;
$this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider(); $this->terminal = new Terminal();
$this->defaultCommand = 'list'; $this->defaultCommand = 'list';
$this->helperSet = $this->getDefaultHelperSet(); $this->helperSet = $this->getDefaultHelperSet();
$this->definition = $this->getDefaultInputDefinition(); $this->definition = $this->getDefaultInputDefinition();
@ -697,7 +692,7 @@ class Application
*/ */
protected function getTerminalWidth() protected function getTerminalWidth()
{ {
return $this->terminalDimensionsProvider->getTerminalWidth(); return $this->terminal->getWidth();
} }
/** /**
@ -707,7 +702,7 @@ class Application
*/ */
protected function getTerminalHeight() protected function getTerminalHeight()
{ {
return $this->terminalDimensionsProvider->getTerminalWidth(); return $this->terminal->getHeight();
} }
/** /**
@ -717,7 +712,7 @@ class Application
*/ */
public function getTerminalDimensions() public function getTerminalDimensions()
{ {
return $this->terminalDimensionsProvider->getTerminalDimensions(); return $this->terminal->getDimensions();
} }
/** /**
@ -732,7 +727,7 @@ class Application
*/ */
public function setTerminalDimensions($width, $height) public function setTerminalDimensions($width, $height)
{ {
$this->terminalDimensionsProvider->setTerminalDimensions($width, $height); $this->terminal->setDimensions($width, $height);
return $this; return $this;
} }

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\Console\Helper;
use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; use Symfony\Component\Console\Terminal;
/** /**
* The ProgressBar provides helpers to display progress output. * The ProgressBar provides helpers to display progress output.
@ -45,21 +45,16 @@ class ProgressBar
private $formatLineCount; private $formatLineCount;
private $messages = array(); private $messages = array();
private $overwrite = true; private $overwrite = true;
private $terminal;
private static $formatters; private static $formatters;
private static $formats; private static $formats;
/** /**
* @var TerminalDimensionsProvider * @param OutputInterface $output An OutputInterface instance
* @param int $max Maximum steps (0 if unknown)
*/ */
private $terminalDimensionsProvider; public function __construct(OutputInterface $output, $max = 0)
/**
* @param OutputInterface $output An OutputInterface instance
* @param int $max Maximum steps (0 if unknown)
* @param TerminalDimensionsProvider $terminalDimensionsProvider
*/
public function __construct(OutputInterface $output, $max = 0, TerminalDimensionsProvider $terminalDimensionsProvider = null)
{ {
if ($output instanceof ConsoleOutputInterface) { if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput(); $output = $output->getErrorOutput();
@ -67,7 +62,7 @@ class ProgressBar
$this->output = $output; $this->output = $output;
$this->setMaxSteps($max); $this->setMaxSteps($max);
$this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider(); $this->terminal = new Terminal();
if (!$this->output->isDecorated()) { if (!$this->output->isDecorated()) {
// disable overwrite when output does not support ANSI codes. // disable overwrite when output does not support ANSI codes.
@ -443,6 +438,20 @@ class ProgressBar
$this->overwrite(''); $this->overwrite('');
} }
/**
* Gets the terminal.
*
* Can be useful to force terminal dimensions for functional tests.
*
* @return Terminal
*
* @internal
*/
public function getTerminal()
{
return $this->terminal;
}
/** /**
* Sets the progress bar format. * Sets the progress bar format.
* *
@ -617,7 +626,7 @@ class ProgressBar
private function adjustLineWidthToTerminalWidth($line) private function adjustLineWidthToTerminalWidth($line)
{ {
$lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line); $lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line);
$terminalWidth = $this->terminalDimensionsProvider->getTerminalWidth(); $terminalWidth = $this->terminal->getWidth();
if ($lineLength > $terminalWidth) { if ($lineLength > $terminalWidth) {
$newBarWidth = $this->barWidth - $lineLength + $terminalWidth; $newBarWidth = $this->barWidth - $lineLength + $terminalWidth;
$this->setBarWidth($newBarWidth); $this->setBarWidth($newBarWidth);

View File

@ -9,59 +9,35 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Symfony\Component\Console\Terminal; namespace Symfony\Component\Console;
class TerminalDimensionsProvider class Terminal
{ {
/** private $width;
* @var int[] private $height;
*/
private $terminalDimensions = array();
/**
* Tries to figure out the terminal dimensions based on the current environment.
*
* @return int[] Array containing width and height
*/
public function getTerminalDimensions()
{
if ($this->terminalDimensions) {
return $this->terminalDimensions;
}
if ($this->isWindowsEnvironment()) {
// extract [w, H] from "wxh (WxH)"
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
return array((int) $matches[1], (int) $matches[2]);
}
// extract [w, h] from "wxh"
if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) {
return array((int) $matches[1], (int) $matches[2]);
}
}
if ($sttyString = $this->getSttyColumns()) {
// extract [w, h] from "rows h; columns w;"
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
return array((int) $matches[2], (int) $matches[1]);
}
// extract [w, h] from "; h rows; w columns"
if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
return array((int) $matches[2], (int) $matches[1]);
}
}
return array(null, null);
}
/** /**
* Tries to figure out the terminal width in which this application runs. * Tries to figure out the terminal width in which this application runs.
* *
* @return int|null * @return int|null
*/ */
public function getTerminalWidth() public function getWidth()
{ {
return $this->getTerminalDimensions()[0]; if (null === $this->width) {
$this->initDimensions();
}
return $this->width;
}
/**
* Sets the terminal width.
*
* @param int
*/
public function setWidth($width)
{
$this->width = $width;
} }
/** /**
@ -69,28 +45,67 @@ class TerminalDimensionsProvider
* *
* @return int|null * @return int|null
*/ */
public function getTerminalHeight() public function getHeight()
{ {
return $this->getTerminalDimensions()[1]; if (null === $this->height) {
$this->initDimensions();
}
return $this->height;
} }
/** /**
* Sets terminal dimensions. * Sets the terminal height.
* *
* Can be useful to force terminal dimensions for functional tests. * @param int
*
* @param int $width
* @param int $height
*/ */
public function setTerminalDimensions($width, $height) public function setHeight($height)
{ {
$this->terminalDimensions = array($width, $height); $this->height = $height;
}
private function initDimensions()
{
if (null !== $this->width && null !== $this->height) {
return;
}
$width = $height = null;
if ($this->isWindowsEnvironment()) {
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
// extract [w, H] from "wxh (WxH)"
$width = (int) $matches[1];
$height = (int) $matches[2];
} elseif (null != $dimensions = $this->getConsoleMode()) {
// extract [w, h] from "wxh"
$width = $dimensions[0];
$height = $dimensions[1];
}
} elseif ($sttyString = $this->getSttyColumns()) {
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
// extract [w, h] from "rows h; columns w;"
$width = (int) $matches[1];
$height = (int) $matches[2];
} elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
// extract [w, h] from "; h rows; w columns"
$width = (int) $matches[2];
$heighth = (int) $matches[1];
}
}
if (null === $this->width) {
$this->width = $width;
}
if (null === $this->height) {
$this->height = $height;
}
} }
/** /**
* Runs and parses mode CON if it's available, suppressing any error output. * Runs and parses mode CON if it's available, suppressing any error output.
* *
* @return string <width>x<height> or null if it could not be parsed * @return array|null An array composed of the width and the height or null if it could not be parsed
*/ */
private function getConsoleMode() private function getConsoleMode()
{ {
@ -110,7 +125,7 @@ class TerminalDimensionsProvider
proc_close($process); proc_close($process);
if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) { if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
return $matches[2].'x'.$matches[1]; return array((int) $matches[2], (int) $matches[1]);
} }
} }
} }

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\Console\Tests\Helper;
use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider; use Symfony\Component\Console\Terminal;
/** /**
* @group time-sensitive * @group time-sensitive
@ -23,7 +23,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
{ {
public function testMultipleStart() public function testMultipleStart()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->start(); $bar->start();
$bar->advance(); $bar->advance();
$bar->start(); $bar->start();
@ -39,7 +39,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testAdvance() public function testAdvance()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->start(); $bar->start();
$bar->advance(); $bar->advance();
@ -53,7 +53,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testAdvanceWithStep() public function testAdvanceWithStep()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->start(); $bar->start();
$bar->advance(5); $bar->advance(5);
@ -67,7 +67,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testAdvanceMultipleTimes() public function testAdvanceMultipleTimes()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->start(); $bar->start();
$bar->advance(3); $bar->advance(3);
$bar->advance(2); $bar->advance(2);
@ -83,7 +83,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testAdvanceOverMax() public function testAdvanceOverMax()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->setProgress(9); $bar->setProgress(9);
$bar->advance(); $bar->advance();
$bar->advance(); $bar->advance();
@ -106,7 +106,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
; ;
// max in construct, no format // max in construct, no format
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->start(); $bar->start();
$bar->advance(10); $bar->advance(10);
$bar->finish(); $bar->finish();
@ -115,7 +115,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, stream_get_contents($output->getStream())); $this->assertEquals($expected, stream_get_contents($output->getStream()));
// max in start, no format // max in start, no format
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->start(10); $bar->start(10);
$bar->advance(10); $bar->advance(10);
$bar->finish(); $bar->finish();
@ -124,7 +124,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, stream_get_contents($output->getStream())); $this->assertEquals($expected, stream_get_contents($output->getStream()));
// max in construct, explicit format before // max in construct, explicit format before
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->setFormat('normal'); $bar->setFormat('normal');
$bar->start(); $bar->start();
$bar->advance(10); $bar->advance(10);
@ -134,7 +134,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, stream_get_contents($output->getStream())); $this->assertEquals($expected, stream_get_contents($output->getStream()));
// max in start, explicit format before // max in start, explicit format before
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->setFormat('normal'); $bar->setFormat('normal');
$bar->start(10); $bar->start(10);
$bar->advance(10); $bar->advance(10);
@ -146,7 +146,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testCustomizations() public function testCustomizations()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->setBarWidth(10); $bar->setBarWidth(10);
$bar->setBarCharacter('_'); $bar->setBarCharacter('_');
$bar->setEmptyBarCharacter(' '); $bar->setEmptyBarCharacter(' ');
@ -165,7 +165,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testDisplayWithoutStart() public function testDisplayWithoutStart()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar->display(); $bar->display();
rewind($output->getStream()); rewind($output->getStream());
@ -177,7 +177,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testDisplayWithQuietVerbosity() public function testDisplayWithQuietVerbosity()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(true, StreamOutput::VERBOSITY_QUIET), 50, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(true, StreamOutput::VERBOSITY_QUIET), 50);
$bar->display(); $bar->display();
rewind($output->getStream()); rewind($output->getStream());
@ -189,7 +189,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testFinishWithoutStart() public function testFinishWithoutStart()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar->finish(); $bar->finish();
rewind($output->getStream()); rewind($output->getStream());
@ -201,7 +201,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testPercent() public function testPercent()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar->start(); $bar->start();
$bar->display(); $bar->display();
$bar->advance(); $bar->advance();
@ -219,7 +219,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testOverwriteWithShorterLine() public function testOverwriteWithShorterLine()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%'); $bar->setFormat(' %current%/%max% [%bar%] %percent:3s%%');
$bar->start(); $bar->start();
$bar->display(); $bar->display();
@ -241,7 +241,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testStartWithMax() public function testStartWithMax()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->setFormat('%current%/%max% [%bar%]'); $bar->setFormat('%current%/%max% [%bar%]');
$bar->start(50); $bar->start(50);
$bar->advance(); $bar->advance();
@ -256,7 +256,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testSetCurrentProgress() public function testSetCurrentProgress()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar->start(); $bar->start();
$bar->display(); $bar->display();
$bar->advance(); $bar->advance();
@ -289,7 +289,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRegressProgress() public function testRegressProgress()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar->start(); $bar->start();
$bar->setProgress(15); $bar->setProgress(15);
$bar->setProgress(10); $bar->setProgress(10);
@ -330,7 +330,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testMultiByteSupport() public function testMultiByteSupport()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->start(); $bar->start();
$bar->setBarCharacter('■'); $bar->setBarCharacter('■');
$bar->advance(3); $bar->advance(3);
@ -345,7 +345,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testClear() public function testClear()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 50, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 50);
$bar->start(); $bar->start();
$bar->setProgress(25); $bar->setProgress(25);
$bar->clear(); $bar->clear();
@ -361,7 +361,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testPercentNotHundredBeforeComplete() public function testPercentNotHundredBeforeComplete()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 200, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 200);
$bar->start(); $bar->start();
$bar->display(); $bar->display();
$bar->advance(199); $bar->advance(199);
@ -379,7 +379,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testNonDecoratedOutput() public function testNonDecoratedOutput()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(false), 200, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(false), 200);
$bar->start(); $bar->start();
for ($i = 0; $i < 200; ++$i) { for ($i = 0; $i < 200; ++$i) {
@ -407,7 +407,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testNonDecoratedOutputWithClear() public function testNonDecoratedOutputWithClear()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(false), 50, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(false), 50);
$bar->start(); $bar->start();
$bar->setProgress(25); $bar->setProgress(25);
$bar->clear(); $bar->clear();
@ -425,7 +425,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testNonDecoratedOutputWithoutMax() public function testNonDecoratedOutputWithoutMax()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(false), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(false));
$bar->start(); $bar->start();
$bar->advance(); $bar->advance();
@ -440,10 +440,10 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testParallelBars() public function testParallelBars()
{ {
$output = $this->getOutputStream(); $output = $this->getOutputStream();
$bar1 = new ProgressBar($output, 2, $this->createTerminalDimensionsProvider()); $bar1 = new ProgressBar($output, 2);
$bar2 = new ProgressBar($output, 3, $this->createTerminalDimensionsProvider()); $bar2 = new ProgressBar($output, 3);
$bar2->setProgressCharacter('#'); $bar2->setProgressCharacter('#');
$bar3 = new ProgressBar($output, 0, $this->createTerminalDimensionsProvider()); $bar3 = new ProgressBar($output);
$bar1->start(); $bar1->start();
$output->write("\n"); $output->write("\n");
@ -500,7 +500,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
{ {
$output = $this->getOutputStream(); $output = $this->getOutputStream();
$bar = new ProgressBar($output, 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output);
$bar->start(); $bar->start();
$bar->advance(); $bar->advance();
$bar->advance(); $bar->advance();
@ -518,12 +518,29 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testWithSmallScreen()
{
$output = $this->getOutputStream();
$bar = new ProgressBar($output);
$bar->getTerminal()->setDimensions(12, 50);
$bar->start();
$bar->advance();
rewind($output->getStream());
$this->assertEquals(
$this->generateOutput(' 0 [>---]').
$this->generateOutput(' 1 [->--]'),
stream_get_contents($output->getStream())
);
}
public function testAddingPlaceholderFormatter() public function testAddingPlaceholderFormatter()
{ {
ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) { ProgressBar::setPlaceholderFormatterDefinition('remaining_steps', function (ProgressBar $bar) {
return $bar->getMaxSteps() - $bar->getProgress(); return $bar->getMaxSteps() - $bar->getProgress();
}); });
$bar = new ProgressBar($output = $this->getOutputStream(), 3, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 3);
$bar->setFormat(' %remaining_steps% [%bar%]'); $bar->setFormat(' %remaining_steps% [%bar%]');
$bar->start(); $bar->start();
@ -541,7 +558,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testMultilineFormat() public function testMultilineFormat()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 3, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 3);
$bar->setFormat("%bar%\nfoobar"); $bar->setFormat("%bar%\nfoobar");
$bar->start(); $bar->start();
@ -561,7 +578,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testAnsiColorsAndEmojis() public function testAnsiColorsAndEmojis()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 15, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 15);
ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) { ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) {
static $i = 0; static $i = 0;
$mem = 100000 * $i; $mem = 100000 * $i;
@ -604,7 +621,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
public function testSetFormat() public function testSetFormat()
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->setFormat('normal'); $bar->setFormat('normal');
$bar->start(); $bar->start();
rewind($output->getStream()); rewind($output->getStream());
@ -613,7 +630,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
stream_get_contents($output->getStream()) stream_get_contents($output->getStream())
); );
$bar = new ProgressBar($output = $this->getOutputStream(), 10, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream(), 10);
$bar->setFormat('normal'); $bar->setFormat('normal');
$bar->start(); $bar->start();
rewind($output->getStream()); rewind($output->getStream());
@ -628,7 +645,7 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
*/ */
public function testFormatsWithoutMax($format) public function testFormatsWithoutMax($format)
{ {
$bar = new ProgressBar($output = $this->getOutputStream(), 0, $this->createTerminalDimensionsProvider()); $bar = new ProgressBar($output = $this->getOutputStream());
$bar->setFormat($format); $bar->setFormat($format);
$bar->start(); $bar->start();
@ -662,15 +679,4 @@ class ProgressBarTest extends \PHPUnit_Framework_TestCase
return "\x0D\x1B[2K".($count ? str_repeat("\x1B[1A\x1B[2K", $count) : '').$expected; return "\x0D\x1B[2K".($count ? str_repeat("\x1B[1A\x1B[2K", $count) : '').$expected;
} }
/**
* @return TerminalDimensionsProvider
*/
private function createTerminalDimensionsProvider()
{
$terminalDimensionsProvider = new TerminalDimensionsProvider();
$terminalDimensionsProvider->setTerminalDimensions(800, 5);
return $terminalDimensionsProvider;
}
} }

View File

@ -1,38 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests\Terminal;
use PHPUnit_Framework_TestCase;
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider;
use Symfony\Component\Console\Terminal\TerminalDimensionsProviderInterface;
class TerminalDimensionsProviderTest extends PHPUnit_Framework_TestCase
{
/**
* @var TerminalDimensionsProviderInterface
*/
private $terminalDimensionsProvider;
protected function setUp()
{
$this->terminalDimensionsProvider = new TerminalDimensionsProvider();
}
public function testGetTerminalDimensions()
{
$dimensions = $this->terminalDimensionsProvider->getTerminalDimensions();
$this->assertCount(2, $dimensions);
$this->terminalDimensionsProvider->setTerminalDimensions(100, 50);
$this->assertSame(array(100, 50), $this->terminalDimensionsProvider->getTerminalDimensions());
}
}

View File

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests;
use Symfony\Component\Console\Terminal;
class TerminalTest extends \PHPUnit_Framework_TestCase
{
public function testGetDimensions()
{
$terminal = new Terminal();
$dimensions = $terminal->getDimensions();
$this->assertCount(2, $dimensions);
$terminal->setDimensions(100, 50);
$this->assertSame(array(100, 50), $terminal->getDimensions());
$this->assertSame(100, $terminal->getWidth());
$this->assertSame(50, $terminal->getHeight());
}
}