ProgressIndicatorTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Symfony\Component\Console\Tests\Helper;
  3. use PHPUnit\Framework\TestCase;
  4. use Symfony\Component\Console\Helper\ProgressIndicator;
  5. use Symfony\Component\Console\Output\StreamOutput;
  6. /**
  7. * @group time-sensitive
  8. */
  9. class ProgressIndicatorTest extends TestCase
  10. {
  11. public function testDefaultIndicator()
  12. {
  13. $bar = new ProgressIndicator($output = $this->getOutputStream());
  14. $bar->start('Starting...');
  15. usleep(101000);
  16. $bar->advance();
  17. usleep(101000);
  18. $bar->advance();
  19. usleep(101000);
  20. $bar->advance();
  21. usleep(101000);
  22. $bar->advance();
  23. usleep(101000);
  24. $bar->advance();
  25. usleep(101000);
  26. $bar->setMessage('Advancing...');
  27. $bar->advance();
  28. $bar->finish('Done...');
  29. $bar->start('Starting Again...');
  30. usleep(101000);
  31. $bar->advance();
  32. $bar->finish('Done Again...');
  33. rewind($output->getStream());
  34. $this->assertEquals(
  35. $this->generateOutput(' - Starting...').
  36. $this->generateOutput(' \\ Starting...').
  37. $this->generateOutput(' | Starting...').
  38. $this->generateOutput(' / Starting...').
  39. $this->generateOutput(' - Starting...').
  40. $this->generateOutput(' \\ Starting...').
  41. $this->generateOutput(' \\ Advancing...').
  42. $this->generateOutput(' | Advancing...').
  43. $this->generateOutput(' | Done...').
  44. PHP_EOL.
  45. $this->generateOutput(' - Starting Again...').
  46. $this->generateOutput(' \\ Starting Again...').
  47. $this->generateOutput(' \\ Done Again...').
  48. PHP_EOL,
  49. stream_get_contents($output->getStream())
  50. );
  51. }
  52. public function testNonDecoratedOutput()
  53. {
  54. $bar = new ProgressIndicator($output = $this->getOutputStream(false));
  55. $bar->start('Starting...');
  56. $bar->advance();
  57. $bar->advance();
  58. $bar->setMessage('Midway...');
  59. $bar->advance();
  60. $bar->advance();
  61. $bar->finish('Done...');
  62. rewind($output->getStream());
  63. $this->assertEquals(
  64. ' Starting...'.PHP_EOL.
  65. ' Midway...'.PHP_EOL.
  66. ' Done...'.PHP_EOL.PHP_EOL,
  67. stream_get_contents($output->getStream())
  68. );
  69. }
  70. public function testCustomIndicatorValues()
  71. {
  72. $bar = new ProgressIndicator($output = $this->getOutputStream(), null, 100, array('a', 'b', 'c'));
  73. $bar->start('Starting...');
  74. usleep(101000);
  75. $bar->advance();
  76. usleep(101000);
  77. $bar->advance();
  78. usleep(101000);
  79. $bar->advance();
  80. rewind($output->getStream());
  81. $this->assertEquals(
  82. $this->generateOutput(' a Starting...').
  83. $this->generateOutput(' b Starting...').
  84. $this->generateOutput(' c Starting...').
  85. $this->generateOutput(' a Starting...'),
  86. stream_get_contents($output->getStream())
  87. );
  88. }
  89. /**
  90. * @expectedException \InvalidArgumentException
  91. * @expectedExceptionMessage Must have at least 2 indicator value characters.
  92. */
  93. public function testCannotSetInvalidIndicatorCharacters()
  94. {
  95. $bar = new ProgressIndicator($this->getOutputStream(), null, 100, array('1'));
  96. }
  97. /**
  98. * @expectedException \LogicException
  99. * @expectedExceptionMessage Progress indicator already started.
  100. */
  101. public function testCannotStartAlreadyStartedIndicator()
  102. {
  103. $bar = new ProgressIndicator($this->getOutputStream());
  104. $bar->start('Starting...');
  105. $bar->start('Starting Again.');
  106. }
  107. /**
  108. * @expectedException \LogicException
  109. * @expectedExceptionMessage Progress indicator has not yet been started.
  110. */
  111. public function testCannotAdvanceUnstartedIndicator()
  112. {
  113. $bar = new ProgressIndicator($this->getOutputStream());
  114. $bar->advance();
  115. }
  116. /**
  117. * @expectedException \LogicException
  118. * @expectedExceptionMessage Progress indicator has not yet been started.
  119. */
  120. public function testCannotFinishUnstartedIndicator()
  121. {
  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. * @return array
  140. */
  141. public function provideFormat()
  142. {
  143. return array(
  144. array('normal'),
  145. array('verbose'),
  146. array('very_verbose'),
  147. array('debug'),
  148. );
  149. }
  150. protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
  151. {
  152. return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);
  153. }
  154. protected function generateOutput($expected)
  155. {
  156. $count = substr_count($expected, "\n");
  157. return "\x0D\x1B[2K".($count ? sprintf("\033[%dA", $count) : '').$expected;
  158. }
  159. }