ProgressIndicatorTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Helper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Helper\ProgressIndicator;
  13. use Symfony\Component\Console\Output\StreamOutput;
  14. /**
  15. * @group time-sensitive
  16. */
  17. class ProgressIndicatorTest extends TestCase
  18. {
  19. public function testDefaultIndicator()
  20. {
  21. $bar = new ProgressIndicator($output = $this->getOutputStream());
  22. $bar->start('Starting...');
  23. usleep(101000);
  24. $bar->advance();
  25. usleep(101000);
  26. $bar->advance();
  27. usleep(101000);
  28. $bar->advance();
  29. usleep(101000);
  30. $bar->advance();
  31. usleep(101000);
  32. $bar->advance();
  33. usleep(101000);
  34. $bar->setMessage('Advancing...');
  35. $bar->advance();
  36. $bar->finish('Done...');
  37. $bar->start('Starting Again...');
  38. usleep(101000);
  39. $bar->advance();
  40. $bar->finish('Done Again...');
  41. rewind($output->getStream());
  42. $this->assertEquals(
  43. $this->generateOutput(' - Starting...').
  44. $this->generateOutput(' \\ Starting...').
  45. $this->generateOutput(' | Starting...').
  46. $this->generateOutput(' / Starting...').
  47. $this->generateOutput(' - Starting...').
  48. $this->generateOutput(' \\ Starting...').
  49. $this->generateOutput(' \\ Advancing...').
  50. $this->generateOutput(' | Advancing...').
  51. $this->generateOutput(' | Done...').
  52. \PHP_EOL.
  53. $this->generateOutput(' - Starting Again...').
  54. $this->generateOutput(' \\ Starting Again...').
  55. $this->generateOutput(' \\ Done Again...').
  56. \PHP_EOL,
  57. stream_get_contents($output->getStream())
  58. );
  59. }
  60. public function testNonDecoratedOutput()
  61. {
  62. $bar = new ProgressIndicator($output = $this->getOutputStream(false));
  63. $bar->start('Starting...');
  64. $bar->advance();
  65. $bar->advance();
  66. $bar->setMessage('Midway...');
  67. $bar->advance();
  68. $bar->advance();
  69. $bar->finish('Done...');
  70. rewind($output->getStream());
  71. $this->assertEquals(
  72. ' Starting...'.\PHP_EOL.
  73. ' Midway...'.\PHP_EOL.
  74. ' Done...'.\PHP_EOL.\PHP_EOL,
  75. stream_get_contents($output->getStream())
  76. );
  77. }
  78. public function testCustomIndicatorValues()
  79. {
  80. $bar = new ProgressIndicator($output = $this->getOutputStream(), null, 100, ['a', 'b', 'c']);
  81. $bar->start('Starting...');
  82. usleep(101000);
  83. $bar->advance();
  84. usleep(101000);
  85. $bar->advance();
  86. usleep(101000);
  87. $bar->advance();
  88. rewind($output->getStream());
  89. $this->assertEquals(
  90. $this->generateOutput(' a Starting...').
  91. $this->generateOutput(' b Starting...').
  92. $this->generateOutput(' c Starting...').
  93. $this->generateOutput(' a Starting...'),
  94. stream_get_contents($output->getStream())
  95. );
  96. }
  97. public function testCannotSetInvalidIndicatorCharacters()
  98. {
  99. $this->expectException(\InvalidArgumentException::class);
  100. $this->expectExceptionMessage('Must have at least 2 indicator value characters.');
  101. new ProgressIndicator($this->getOutputStream(), null, 100, ['1']);
  102. }
  103. public function testCannotStartAlreadyStartedIndicator()
  104. {
  105. $this->expectException(\LogicException::class);
  106. $this->expectExceptionMessage('Progress indicator already started.');
  107. $bar = new ProgressIndicator($this->getOutputStream());
  108. $bar->start('Starting...');
  109. $bar->start('Starting Again.');
  110. }
  111. public function testCannotAdvanceUnstartedIndicator()
  112. {
  113. $this->expectException(\LogicException::class);
  114. $this->expectExceptionMessage('Progress indicator has not yet been started.');
  115. $bar = new ProgressIndicator($this->getOutputStream());
  116. $bar->advance();
  117. }
  118. public function testCannotFinishUnstartedIndicator()
  119. {
  120. $this->expectException(\LogicException::class);
  121. $this->expectExceptionMessage('Progress indicator has not yet been started.');
  122. $bar = new ProgressIndicator($this->getOutputStream());
  123. $bar->finish('Finished');
  124. }
  125. /**
  126. * @dataProvider provideFormat
  127. */
  128. public function testFormats($format)
  129. {
  130. $bar = new ProgressIndicator($output = $this->getOutputStream(), $format);
  131. $bar->start('Starting...');
  132. $bar->advance();
  133. rewind($output->getStream());
  134. $this->assertNotEmpty(stream_get_contents($output->getStream()));
  135. }
  136. /**
  137. * Provides each defined format.
  138. */
  139. public static function provideFormat(): array
  140. {
  141. return [
  142. ['normal'],
  143. ['verbose'],
  144. ['very_verbose'],
  145. ['debug'],
  146. ];
  147. }
  148. protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
  149. {
  150. return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);
  151. }
  152. protected function generateOutput($expected)
  153. {
  154. $count = substr_count($expected, "\n");
  155. return "\x0D\x1B[2K".($count ? sprintf("\033[%dA", $count) : '').$expected;
  156. }
  157. }