/vendor/symfony/console/Symfony/Component/Console/Tests/Helper/LegacyProgressHelperTest.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 232 lines · 173 code · 39 blank · 20 comment · 3 complexity · bedf10f012fdd94ed132401136c4f081 MD5 · raw file

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