PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/aegir/provision
PHP | 175 lines | 140 code | 24 blank | 11 comment | 0 complexity | 11ba23b8105aaf9d430f82ca2c136d18 MD5 | raw file
  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, ['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. public function testCannotSetInvalidIndicatorCharacters()
  90. {
  91. $this->expectException('InvalidArgumentException');
  92. $this->expectExceptionMessage('Must have at least 2 indicator value characters.');
  93. new ProgressIndicator($this->getOutputStream(), null, 100, ['1']);
  94. }
  95. public function testCannotStartAlreadyStartedIndicator()
  96. {
  97. $this->expectException('LogicException');
  98. $this->expectExceptionMessage('Progress indicator already started.');
  99. $bar = new ProgressIndicator($this->getOutputStream());
  100. $bar->start('Starting...');
  101. $bar->start('Starting Again.');
  102. }
  103. public function testCannotAdvanceUnstartedIndicator()
  104. {
  105. $this->expectException('LogicException');
  106. $this->expectExceptionMessage('Progress indicator has not yet been started.');
  107. $bar = new ProgressIndicator($this->getOutputStream());
  108. $bar->advance();
  109. }
  110. public function testCannotFinishUnstartedIndicator()
  111. {
  112. $this->expectException('LogicException');
  113. $this->expectExceptionMessage('Progress indicator has not yet been started.');
  114. $bar = new ProgressIndicator($this->getOutputStream());
  115. $bar->finish('Finished');
  116. }
  117. /**
  118. * @dataProvider provideFormat
  119. */
  120. public function testFormats($format)
  121. {
  122. $bar = new ProgressIndicator($output = $this->getOutputStream(), $format);
  123. $bar->start('Starting...');
  124. $bar->advance();
  125. rewind($output->getStream());
  126. $this->assertNotEmpty(stream_get_contents($output->getStream()));
  127. }
  128. /**
  129. * Provides each defined format.
  130. *
  131. * @return array
  132. */
  133. public function provideFormat()
  134. {
  135. return [
  136. ['normal'],
  137. ['verbose'],
  138. ['very_verbose'],
  139. ['debug'],
  140. ];
  141. }
  142. protected function getOutputStream($decorated = true, $verbosity = StreamOutput::VERBOSITY_NORMAL)
  143. {
  144. return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, $decorated);
  145. }
  146. protected function generateOutput($expected)
  147. {
  148. $count = substr_count($expected, "\n");
  149. return "\x0D\x1B[2K".($count ? sprintf("\033[%dA", $count) : '').$expected;
  150. }
  151. }