3 namespace Symfony\Component\Console\Tests\Helper;
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Console\Helper\ProgressIndicator;
7 use Symfony\Component\Console\Output\StreamOutput;
10 * @group time-sensitive
12 class ProgressIndicatorTest extends TestCase
14 public function testDefaultIndicator()
16 $bar = new ProgressIndicator($output = $this->getOutputStream());
17 $bar->start('Starting...');
29 $bar->setMessage('Advancing...');
31 $bar->finish('Done...');
32 $bar->start('Starting Again...');
35 $bar->finish('Done Again...');
37 rewind($output->getStream());
40 $this->generateOutput(' - Starting...').
41 $this->generateOutput(' \\ Starting...').
42 $this->generateOutput(' | Starting...').
43 $this->generateOutput(' / Starting...').
44 $this->generateOutput(' - Starting...').
45 $this->generateOutput(' \\ Starting...').
46 $this->generateOutput(' \\ Advancing...').
47 $this->generateOutput(' | Advancing...').
48 $this->generateOutput(' | Done...').
50 $this->generateOutput(' - Starting Again...').
51 $this->generateOutput(' \\ Starting Again...').
52 $this->generateOutput(' \\ Done Again...').
54 stream_get_contents($output->getStream())
58 public function testNonDecoratedOutput()
60 $bar = new ProgressIndicator($output = $this->getOutputStream(false));
62 $bar->start('Starting...');
65 $bar->setMessage('Midway...');
68 $bar->finish('Done...');
70 rewind($output->getStream());
73 ' Starting...'.PHP_EOL.
75 ' Done...'.PHP_EOL.PHP_EOL,
76 stream_get_contents($output->getStream())
80 public function testCustomIndicatorValues()
82 $bar = new ProgressIndicator($output = $this->getOutputStream(), null, 100, array('a', 'b', 'c'));
84 $bar->start('Starting...');
92 rewind($output->getStream());
95 $this->generateOutput(' a Starting...').
96 $this->generateOutput(' b Starting...').
97 $this->generateOutput(' c Starting...').
98 $this->generateOutput(' a Starting...'),
99 stream_get_contents($output->getStream())
104 * @expectedException \InvalidArgumentException
105 * @expectedExceptionMessage Must have at least 2 indicator value characters.
107 public function testCannotSetInvalidIndicatorCharacters()
109 $bar = new ProgressIndicator($this->getOutputStream(), null, 100, array('1'));
113 * @expectedException \LogicException
114 * @expectedExceptionMessage Progress indicator already started.
116 public function testCannotStartAlreadyStartedIndicator()
118 $bar = new ProgressIndicator($this->getOutputStream());
119 $bar->start('Starting...');
120 $bar->start('Starting Again.');
124 * @expectedException \LogicException
125 * @expectedExceptionMessage Progress indicator has not yet been started.
127 public function testCannotAdvanceUnstartedIndicator()
129 $bar = new ProgressIndicator($this->getOutputStream());
134 * @expectedException \LogicException
135 * @expectedExceptionMessage Progress indicator has not yet been started.
137 public function testCannotFinishUnstartedIndicator()
139 $bar = new ProgressIndicator($this->getOutputStream());
140 $bar->finish('Finished');
144 * @dataProvider provideFormat
146 public function testFormats($format)
148 $bar = new ProgressIndicator($output = $this->getOutputStream(), $format);
149 $bar->start('Starting...');
152 rewind($output->getStream());
154 $this->assertNotEmpty(stream_get_contents($output->getStream()));
158 * Provides each defined format.
162 public function provideFormat()
167 array('very_verbose'),
172 protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
174 return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);
177 protected function generateOutput($expected)
179 $count = substr_count($expected, "\n");
181 return "\x0D\x1B[2K".($count ? sprintf("\033[%dA", $count) : '').$expected;