ProgressHelperTest.php 6.5 KB

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