ProgressHelperTest.php 8.0 KB

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