ProgressIndicatorTest.php 5.2 KB

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