ProgressHelperTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP Project
  12. * @since 3.1.0
  13. * @license https://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 using the helper manually.
  39. *
  40. * @return void
  41. */
  42. public function testInit()
  43. {
  44. $helper = $this->helper->init([
  45. 'total' => 200,
  46. 'width' => 50
  47. ]);
  48. $this->assertSame($helper, $this->helper, 'init should be chainable');
  49. }
  50. /**
  51. * Test that a callback is required.
  52. *
  53. * @expectedException \RuntimeException
  54. */
  55. public function testOutputFailure()
  56. {
  57. $this->helper->output(['not a callback']);
  58. }
  59. /**
  60. * Test that the callback is invoked until 100 is reached.
  61. *
  62. * @return void
  63. */
  64. public function testOutputSuccess()
  65. {
  66. $this->helper->output([function ($progress) {
  67. $progress->increment(20);
  68. }]);
  69. $expected = [
  70. '',
  71. '',
  72. '==============> 20%',
  73. '',
  74. '=============================> 40%',
  75. '',
  76. '============================================> 60%',
  77. '',
  78. '===========================================================> 80%',
  79. '',
  80. '==========================================================================> 100%',
  81. '',
  82. ];
  83. $this->assertEquals($expected, $this->stub->messages());
  84. }
  85. /**
  86. * Test output with options
  87. *
  88. * @return void
  89. */
  90. public function testOutputSuccessOptions()
  91. {
  92. $this->helper->output([
  93. 'total' => 10,
  94. 'width' => 20,
  95. 'callback' => function ($progress) {
  96. $progress->increment(2);
  97. }
  98. ]);
  99. $expected = [
  100. '',
  101. '',
  102. '==> 20%',
  103. '',
  104. '=====> 40%',
  105. '',
  106. '========> 60%',
  107. '',
  108. '===========> 80%',
  109. '',
  110. '==============> 100%',
  111. '',
  112. ];
  113. $this->assertEquals($expected, $this->stub->messages());
  114. }
  115. /**
  116. * Test using the helper manually.
  117. *
  118. * @return void
  119. */
  120. public function testIncrementAndRender()
  121. {
  122. $this->helper->init();
  123. $this->helper->increment(20);
  124. $this->helper->draw();
  125. $this->helper->increment(40);
  126. $this->helper->draw();
  127. $this->helper->increment(40);
  128. $this->helper->draw();
  129. $expected = [
  130. '',
  131. '==============> 20%',
  132. '',
  133. '============================================> 60%',
  134. '',
  135. '==========================================================================> 100%',
  136. ];
  137. $this->assertEquals($expected, $this->stub->messages());
  138. }
  139. /**
  140. * Test negative numbers
  141. *
  142. * @return void
  143. */
  144. public function testIncrementWithNegatives()
  145. {
  146. $this->helper->init();
  147. $this->helper->increment(40);
  148. $this->helper->draw();
  149. $this->helper->increment(-60);
  150. $this->helper->draw();
  151. $this->helper->increment(80);
  152. $this->helper->draw();
  153. $expected = [
  154. '',
  155. '=============================> 40%',
  156. '',
  157. ' 0%',
  158. '',
  159. '===========================================================> 80%',
  160. ];
  161. $this->assertEquals($expected, $this->stub->messages());
  162. }
  163. /**
  164. * Test increment and draw with options
  165. *
  166. * @return void
  167. */
  168. public function testIncrementWithOptions()
  169. {
  170. $this->helper->init([
  171. 'total' => 10,
  172. 'width' => 20,
  173. ]);
  174. $expected = [
  175. '',
  176. '=====> 40%',
  177. '',
  178. '===========> 80%',
  179. '',
  180. '==============> 100%',
  181. ];
  182. $this->helper->increment(4);
  183. $this->helper->draw();
  184. $this->helper->increment(4);
  185. $this->helper->draw();
  186. $this->helper->increment(4);
  187. $this->helper->draw();
  188. $this->assertEquals($expected, $this->stub->messages());
  189. }
  190. /**
  191. * Test increment and draw with value that makes the pad
  192. * be a float
  193. *
  194. * @return void
  195. */
  196. public function testIncrementFloatPad()
  197. {
  198. $this->helper->init([
  199. 'total' => 50
  200. ]);
  201. $expected = [
  202. '',
  203. '=========> 14%',
  204. '',
  205. '====================> 28%',
  206. '',
  207. '==============================> 42%',
  208. '',
  209. '=========================================> 56%',
  210. '',
  211. '===================================================> 70%',
  212. '',
  213. '========================================================> 76%',
  214. '',
  215. '==============================================================> 84%',
  216. '',
  217. '==========================================================================> 100%',
  218. ];
  219. $this->helper->increment(7);
  220. $this->helper->draw();
  221. $this->helper->increment(7);
  222. $this->helper->draw();
  223. $this->helper->increment(7);
  224. $this->helper->draw();
  225. $this->helper->increment(7);
  226. $this->helper->draw();
  227. $this->helper->increment(7);
  228. $this->helper->draw();
  229. $this->helper->increment(3);
  230. $this->helper->draw();
  231. $this->helper->increment(4);
  232. $this->helper->draw();
  233. $this->helper->increment(8);
  234. $this->helper->draw();
  235. $this->assertEquals($expected, $this->stub->messages());
  236. }
  237. }