/RestAPI/vendor/symfony/console/Tests/Helper/ProgressIndicatorTest.php

https://gitlab.com/martinstti/silex-microframework-rest · PHP · 179 lines · 131 code · 24 blank · 24 comment · 0 complexity · f138b505a4c574e24e7a60c17380c693 MD5 · raw file

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