LegacyProgressHelperTest.php 8.1 KB

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