ProgressHelperTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 3.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell\Helper;
  16. use Cake\Console\ConsoleIo;
  17. use Cake\Shell\Helper\ProgressHelper;
  18. use Cake\TestSuite\Stub\ConsoleOutput;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * ProgressHelper test.
  22. */
  23. class ProgressHelperTest extends TestCase
  24. {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->stub = new ConsoleOutput();
  34. $this->io = new ConsoleIo($this->stub);
  35. $this->helper = new ProgressHelper($this->io);
  36. }
  37. /**
  38. * Test that a callback is required.
  39. *
  40. * @expectedException \RuntimeException
  41. */
  42. public function testOutputFailure()
  43. {
  44. $this->helper->output(['not a callback']);
  45. }
  46. /**
  47. * Test that the callback is invoked until 100 is reached.
  48. *
  49. * @return void
  50. */
  51. public function testOutputSuccess()
  52. {
  53. $this->helper->output([function ($progress) {
  54. $progress->increment(20);
  55. }]);
  56. $expected = [
  57. '',
  58. '',
  59. '==============> 20%',
  60. '',
  61. '=============================> 40%',
  62. '',
  63. '============================================> 60%',
  64. '',
  65. '===========================================================> 80%',
  66. '',
  67. '==========================================================================> 100%',
  68. '',
  69. ];
  70. $this->assertEquals($expected, $this->stub->messages());
  71. }
  72. /**
  73. * Test output with options
  74. *
  75. * @return void
  76. */
  77. public function testOutputSuccessOptions()
  78. {
  79. $this->helper->output([
  80. 'total' => 10,
  81. 'width' => 20,
  82. 'callback' => function ($progress) {
  83. $progress->increment(2);
  84. }
  85. ]);
  86. $expected = [
  87. '',
  88. '',
  89. '==> 20%',
  90. '',
  91. '=====> 40%',
  92. '',
  93. '========> 60%',
  94. '',
  95. '===========> 80%',
  96. '',
  97. '==============> 100%',
  98. '',
  99. ];
  100. $this->assertEquals($expected, $this->stub->messages());
  101. }
  102. /**
  103. * Test using the helper manually.
  104. *
  105. * @return void
  106. */
  107. public function testIncrementAndRender()
  108. {
  109. $this->helper->init();
  110. $this->helper->increment(20);
  111. $this->helper->draw();
  112. $this->helper->increment(40);
  113. $this->helper->draw();
  114. $this->helper->increment(40);
  115. $this->helper->draw();
  116. $expected = [
  117. '',
  118. '==============> 20%',
  119. '',
  120. '============================================> 60%',
  121. '',
  122. '==========================================================================> 100%',
  123. ];
  124. $this->assertEquals($expected, $this->stub->messages());
  125. }
  126. /**
  127. * Test negative numbers
  128. *
  129. * @return void
  130. */
  131. public function testIncrementWithNegatives()
  132. {
  133. $this->helper->init();
  134. $this->helper->increment(40);
  135. $this->helper->draw();
  136. $this->helper->increment(-60);
  137. $this->helper->draw();
  138. $this->helper->increment(80);
  139. $this->helper->draw();
  140. $expected = [
  141. '',
  142. '=============================> 40%',
  143. '',
  144. ' 0%',
  145. '',
  146. '===========================================================> 80%',
  147. ];
  148. $this->assertEquals($expected, $this->stub->messages());
  149. }
  150. /**
  151. * Test increment and draw with options
  152. *
  153. * @return void
  154. */
  155. public function testIncrementWithOptions()
  156. {
  157. $this->helper->init([
  158. 'total' => 10,
  159. 'width' => 20,
  160. ]);
  161. $expected = [
  162. '',
  163. '=====> 40%',
  164. '',
  165. '===========> 80%',
  166. '',
  167. '==============> 100%',
  168. ];
  169. $this->helper->increment(4);
  170. $this->helper->draw();
  171. $this->helper->increment(4);
  172. $this->helper->draw();
  173. $this->helper->increment(4);
  174. $this->helper->draw();
  175. $this->assertEquals($expected, $this->stub->messages());
  176. }
  177. /**
  178. * Test increment and draw with value that makes the pad
  179. * be a float
  180. *
  181. * @return void
  182. */
  183. public function testIncrementFloatPad()
  184. {
  185. $this->helper->init([
  186. 'total' => 50
  187. ]);
  188. $expected = [
  189. '',
  190. '=========> 14%',
  191. '',
  192. '====================> 28%',
  193. '',
  194. '==============================> 42%',
  195. '',
  196. '=========================================> 56%',
  197. '',
  198. '===================================================> 70%',
  199. '',
  200. '========================================================> 76%',
  201. '',
  202. '==============================================================> 84%',
  203. '',
  204. '==========================================================================> 100%',
  205. ];
  206. $this->helper->increment(7);
  207. $this->helper->draw();
  208. $this->helper->increment(7);
  209. $this->helper->draw();
  210. $this->helper->increment(7);
  211. $this->helper->draw();
  212. $this->helper->increment(7);
  213. $this->helper->draw();
  214. $this->helper->increment(7);
  215. $this->helper->draw();
  216. $this->helper->increment(3);
  217. $this->helper->draw();
  218. $this->helper->increment(4);
  219. $this->helper->draw();
  220. $this->helper->increment(8);
  221. $this->helper->draw();
  222. $this->assertEquals($expected, $this->stub->messages());
  223. }
  224. }