No Description

LegacyProgressHelperTest.php 7.9KB

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