LegacyProgressHelperTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\ProgressHelper;
  13. use Symfony\Component\Console\Output\StreamOutput;
  14. /**
  15. * @group legacy
  16. * @group time-sensitive
  17. */
  18. class LegacyProgressHelperTest extends TestCase
  19. {
  20. public function testAdvance()
  21. {
  22. $progress = new ProgressHelper();
  23. $progress->start($output = $this->getOutputStream());
  24. $progress->advance();
  25. rewind($output->getStream());
  26. $this->assertEquals($this->generateOutput(' 1 [->--------------------------]'), stream_get_contents($output->getStream()));
  27. }
  28. public function testAdvanceWithStep()
  29. {
  30. $progress = new ProgressHelper();
  31. $progress->start($output = $this->getOutputStream());
  32. $progress->advance(5);
  33. rewind($output->getStream());
  34. $this->assertEquals($this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output->getStream()));
  35. }
  36. public function testAdvanceMultipleTimes()
  37. {
  38. $progress = new ProgressHelper();
  39. $progress->start($output = $this->getOutputStream());
  40. $progress->advance(3);
  41. $progress->advance(2);
  42. rewind($output->getStream());
  43. $this->assertEquals($this->generateOutput(' 3 [--->------------------------]').$this->generateOutput(' 5 [----->----------------------]'), stream_get_contents($output->getStream()));
  44. }
  45. public function testCustomizations()
  46. {
  47. $progress = new ProgressHelper();
  48. $progress->setBarWidth(10);
  49. $progress->setBarCharacter('_');
  50. $progress->setEmptyBarCharacter(' ');
  51. $progress->setProgressCharacter('/');
  52. $progress->setFormat(' %current%/%max% [%bar%] %percent%%');
  53. $progress->start($output = $this->getOutputStream(), 10);
  54. $progress->advance();
  55. rewind($output->getStream());
  56. $this->assertEquals($this->generateOutput(' 1/10 [_/ ] 10%'), stream_get_contents($output->getStream()));
  57. }
  58. public function testPercent()
  59. {
  60. $progress = new ProgressHelper();
  61. $progress->start($output = $this->getOutputStream(), 50);
  62. $progress->display();
  63. $progress->advance();
  64. $progress->advance();
  65. rewind($output->getStream());
  66. $this->assertEquals($this->generateOutput(' 0/50 [>---------------------------] 0%').$this->generateOutput(' 1/50 [>---------------------------] 2%').$this->generateOutput(' 2/50 [=>--------------------------] 4%'), stream_get_contents($output->getStream()));
  67. }
  68. public function testOverwriteWithShorterLine()
  69. {
  70. $progress = new ProgressHelper();
  71. $progress->setFormat(' %current%/%max% [%bar%] %percent%%');
  72. $progress->start($output = $this->getOutputStream(), 50);
  73. $progress->display();
  74. $progress->advance();
  75. // set shorter format
  76. $progress->setFormat(' %current%/%max% [%bar%]');
  77. $progress->advance();
  78. rewind($output->getStream());
  79. $this->assertEquals(
  80. $this->generateOutput(' 0/50 [>---------------------------] 0%').
  81. $this->generateOutput(' 1/50 [>---------------------------] 2%').
  82. $this->generateOutput(' 2/50 [=>--------------------------] '),
  83. stream_get_contents($output->getStream())
  84. );
  85. }
  86. public function testSetCurrentProgress()
  87. {
  88. $progress = new ProgressHelper();
  89. $progress->start($output = $this->getOutputStream(), 50);
  90. $progress->display();
  91. $progress->advance();
  92. $progress->setCurrent(15);
  93. $progress->setCurrent(25);
  94. rewind($output->getStream());
  95. $this->assertEquals(
  96. $this->generateOutput(' 0/50 [>---------------------------] 0%').
  97. $this->generateOutput(' 1/50 [>---------------------------] 2%').
  98. $this->generateOutput(' 15/50 [========>-------------------] 30%').
  99. $this->generateOutput(' 25/50 [==============>-------------] 50%'),
  100. stream_get_contents($output->getStream())
  101. );
  102. }
  103. /**
  104. * @expectedException \LogicException
  105. * @expectedExceptionMessage You must start the progress bar
  106. */
  107. public function testSetCurrentBeforeStarting()
  108. {
  109. $progress = new ProgressHelper();
  110. $progress->setCurrent(15);
  111. }
  112. /**
  113. * @expectedException \LogicException
  114. * @expectedExceptionMessage You can't regress the progress bar
  115. */
  116. public function testRegressProgress()
  117. {
  118. $progress = new ProgressHelper();
  119. $progress->start($output = $this->getOutputStream(), 50);
  120. $progress->setCurrent(15);
  121. $progress->setCurrent(10);
  122. }
  123. public function testRedrawFrequency()
  124. {
  125. $progress = $this->getMockBuilder('Symfony\Component\Console\Helper\ProgressHelper')->setMethods(array('display'))->getMock();
  126. $progress->expects($this->exactly(4))
  127. ->method('display');
  128. $progress->setRedrawFrequency(2);
  129. $progress->start($output = $this->getOutputStream(), 6);
  130. $progress->setCurrent(1);
  131. $progress->advance(2);
  132. $progress->advance(2);
  133. $progress->advance(1);
  134. }
  135. public function testMultiByteSupport()
  136. {
  137. $progress = new ProgressHelper();
  138. $progress->start($output = $this->getOutputStream());
  139. $progress->setBarCharacter('■');
  140. $progress->advance(3);
  141. rewind($output->getStream());
  142. $this->assertEquals($this->generateOutput(' 3 [■■■>------------------------]'), stream_get_contents($output->getStream()));
  143. }
  144. public function testClear()
  145. {
  146. $progress = new ProgressHelper();
  147. $progress->start($output = $this->getOutputStream(), 50);
  148. $progress->setCurrent(25);
  149. $progress->clear();
  150. rewind($output->getStream());
  151. $this->assertEquals(
  152. $this->generateOutput(' 25/50 [==============>-------------] 50%').$this->generateOutput(''),
  153. stream_get_contents($output->getStream())
  154. );
  155. }
  156. public function testPercentNotHundredBeforeComplete()
  157. {
  158. $progress = new ProgressHelper();
  159. $progress->start($output = $this->getOutputStream(), 200);
  160. $progress->display();
  161. $progress->advance(199);
  162. $progress->advance();
  163. rewind($output->getStream());
  164. $this->assertEquals($this->generateOutput(' 0/200 [>---------------------------] 0%').$this->generateOutput(' 199/200 [===========================>] 99%').$this->generateOutput(' 200/200 [============================] 100%'), stream_get_contents($output->getStream()));
  165. }
  166. public function testNonDecoratedOutput()
  167. {
  168. $progress = new ProgressHelper();
  169. $progress->start($output = $this->getOutputStream(false));
  170. $progress->advance();
  171. rewind($output->getStream());
  172. $this->assertEquals('', stream_get_contents($output->getStream()));
  173. }
  174. protected function getOutputStream($decorated = true)
  175. {
  176. return new StreamOutput(fopen('php://memory', 'r+', false), StreamOutput::VERBOSITY_NORMAL, $decorated);
  177. }
  178. protected $lastMessagesLength;
  179. protected function generateOutput($expected)
  180. {
  181. $expectedout = $expected;
  182. if (null !== $this->lastMessagesLength) {
  183. $expectedout = str_pad($expected, $this->lastMessagesLength, "\x20", STR_PAD_RIGHT);
  184. }
  185. $this->lastMessagesLength = \strlen($expectedout);
  186. return "\x0D".$expectedout;
  187. }
  188. }