ProgressHelperTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. */
  54. public function testOutputFailure()
  55. {
  56. $this->expectException(\RuntimeException::class);
  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 using the helper chained.
  141. *
  142. * @return void
  143. */
  144. public function testIncrementAndRenderChained()
  145. {
  146. $this->helper->init()
  147. ->increment(20)
  148. ->draw()
  149. ->increment(40)
  150. ->draw()
  151. ->increment(40)
  152. ->draw();
  153. $expected = [
  154. '',
  155. '==============> 20%',
  156. '',
  157. '============================================> 60%',
  158. '',
  159. '==========================================================================> 100%',
  160. ];
  161. $this->assertEquals($expected, $this->stub->messages());
  162. }
  163. /**
  164. * Test negative numbers
  165. *
  166. * @return void
  167. */
  168. public function testIncrementWithNegatives()
  169. {
  170. $this->helper->init();
  171. $this->helper->increment(40);
  172. $this->helper->draw();
  173. $this->helper->increment(-60);
  174. $this->helper->draw();
  175. $this->helper->increment(80);
  176. $this->helper->draw();
  177. $expected = [
  178. '',
  179. '=============================> 40%',
  180. '',
  181. ' 0%',
  182. '',
  183. '===========================================================> 80%',
  184. ];
  185. $this->assertEquals($expected, $this->stub->messages());
  186. }
  187. /**
  188. * Test increment and draw with options
  189. *
  190. * @return void
  191. */
  192. public function testIncrementWithOptions()
  193. {
  194. $this->helper->init([
  195. 'total' => 10,
  196. 'width' => 20,
  197. ]);
  198. $expected = [
  199. '',
  200. '=====> 40%',
  201. '',
  202. '===========> 80%',
  203. '',
  204. '==============> 100%',
  205. ];
  206. $this->helper->increment(4);
  207. $this->helper->draw();
  208. $this->helper->increment(4);
  209. $this->helper->draw();
  210. $this->helper->increment(4);
  211. $this->helper->draw();
  212. $this->assertEquals($expected, $this->stub->messages());
  213. }
  214. /**
  215. * Test increment and draw with value that makes the pad
  216. * be a float
  217. *
  218. * @return void
  219. */
  220. public function testIncrementFloatPad()
  221. {
  222. $this->helper->init([
  223. 'total' => 50
  224. ]);
  225. $expected = [
  226. '',
  227. '=========> 14%',
  228. '',
  229. '====================> 28%',
  230. '',
  231. '==============================> 42%',
  232. '',
  233. '=========================================> 56%',
  234. '',
  235. '===================================================> 70%',
  236. '',
  237. '========================================================> 76%',
  238. '',
  239. '==============================================================> 84%',
  240. '',
  241. '==========================================================================> 100%',
  242. ];
  243. $this->helper->increment(7);
  244. $this->helper->draw();
  245. $this->helper->increment(7);
  246. $this->helper->draw();
  247. $this->helper->increment(7);
  248. $this->helper->draw();
  249. $this->helper->increment(7);
  250. $this->helper->draw();
  251. $this->helper->increment(7);
  252. $this->helper->draw();
  253. $this->helper->increment(3);
  254. $this->helper->draw();
  255. $this->helper->increment(4);
  256. $this->helper->draw();
  257. $this->helper->increment(8);
  258. $this->helper->draw();
  259. $this->assertEquals($expected, $this->stub->messages());
  260. }
  261. }