ProgressHelperTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. * @var \Cake\Shell\Helper\ProgressHelper
  28. */
  29. protected $helper;
  30. /**
  31. * @var \Cake\TestSuite\Stub\ConsoleOutput
  32. */
  33. protected $stub;
  34. /**
  35. * @var \Cake\Console\ConsoleIo
  36. */
  37. protected $io;
  38. /**
  39. * setUp method
  40. *
  41. * @return void
  42. */
  43. public function setUp(): void
  44. {
  45. parent::setUp();
  46. $this->stub = new ConsoleOutput();
  47. $this->io = new ConsoleIo($this->stub);
  48. $this->helper = new ProgressHelper($this->io);
  49. }
  50. /**
  51. * Test using the helper manually.
  52. *
  53. * @return void
  54. */
  55. public function testInit()
  56. {
  57. $helper = $this->helper->init([
  58. 'total' => 200,
  59. 'width' => 50,
  60. ]);
  61. $this->assertSame($helper, $this->helper, 'Should be chainable');
  62. }
  63. /**
  64. * Test that a callback is required.
  65. *
  66. */
  67. public function testOutputFailure()
  68. {
  69. $this->expectException(\RuntimeException::class);
  70. $this->helper->output(['not a callback']);
  71. }
  72. /**
  73. * Test that the callback is invoked until 100 is reached.
  74. *
  75. * @return void
  76. */
  77. public function testOutputSuccess()
  78. {
  79. $this->helper->output([function (ProgressHelper $progress) {
  80. $progress->increment(20);
  81. }]);
  82. $expected = [
  83. '',
  84. '',
  85. '==============> 20%',
  86. '',
  87. '=============================> 40%',
  88. '',
  89. '============================================> 60%',
  90. '',
  91. '===========================================================> 80%',
  92. '',
  93. '==========================================================================> 100%',
  94. '',
  95. ];
  96. $this->assertEquals($expected, $this->stub->messages());
  97. }
  98. /**
  99. * Test output with options
  100. *
  101. * @return void
  102. */
  103. public function testOutputSuccessOptions()
  104. {
  105. $this->helper->output([
  106. 'total' => 10,
  107. 'width' => 20,
  108. 'callback' => function (ProgressHelper $progress) {
  109. $progress->increment(2);
  110. },
  111. ]);
  112. $expected = [
  113. '',
  114. '',
  115. '==> 20%',
  116. '',
  117. '=====> 40%',
  118. '',
  119. '========> 60%',
  120. '',
  121. '===========> 80%',
  122. '',
  123. '==============> 100%',
  124. '',
  125. ];
  126. $this->assertEquals($expected, $this->stub->messages());
  127. }
  128. /**
  129. * Test using the helper manually.
  130. *
  131. * @return void
  132. */
  133. public function testIncrementAndRender()
  134. {
  135. $this->helper->init();
  136. $this->helper->increment(20);
  137. $this->helper->draw();
  138. $this->helper->increment(40.0);
  139. $this->helper->draw();
  140. $this->helper->increment(40);
  141. $this->helper->draw();
  142. $expected = [
  143. '',
  144. '==============> 20%',
  145. '',
  146. '============================================> 60%',
  147. '',
  148. '==========================================================================> 100%',
  149. ];
  150. $this->assertEquals($expected, $this->stub->messages());
  151. }
  152. /**
  153. * Test using the helper chained.
  154. *
  155. * @return void
  156. */
  157. public function testIncrementAndRenderChained()
  158. {
  159. $this->helper->init()
  160. ->increment(20)
  161. ->draw()
  162. ->increment(40)
  163. ->draw()
  164. ->increment(40)
  165. ->draw();
  166. $expected = [
  167. '',
  168. '==============> 20%',
  169. '',
  170. '============================================> 60%',
  171. '',
  172. '==========================================================================> 100%',
  173. ];
  174. $this->assertEquals($expected, $this->stub->messages());
  175. }
  176. /**
  177. * Test negative numbers
  178. *
  179. * @return void
  180. */
  181. public function testIncrementWithNegatives()
  182. {
  183. $this->helper->init();
  184. $this->helper->increment(40);
  185. $this->helper->draw();
  186. $this->helper->increment(-60);
  187. $this->helper->draw();
  188. $this->helper->increment(80);
  189. $this->helper->draw();
  190. $expected = [
  191. '',
  192. '=============================> 40%',
  193. '',
  194. ' 0%',
  195. '',
  196. '===========================================================> 80%',
  197. ];
  198. $this->assertEquals($expected, $this->stub->messages());
  199. }
  200. /**
  201. * Test increment and draw with options
  202. *
  203. * @return void
  204. */
  205. public function testIncrementWithOptions()
  206. {
  207. $this->helper->init([
  208. 'total' => 10,
  209. 'width' => 20,
  210. ]);
  211. $expected = [
  212. '',
  213. '=====> 40%',
  214. '',
  215. '===========> 80%',
  216. '',
  217. '==============> 100%',
  218. ];
  219. $this->helper->increment(4);
  220. $this->helper->draw();
  221. $this->helper->increment(4);
  222. $this->helper->draw();
  223. $this->helper->increment(4);
  224. $this->helper->draw();
  225. $this->assertEquals($expected, $this->stub->messages());
  226. }
  227. /**
  228. * Test increment and draw with value that makes the pad
  229. * be a float
  230. *
  231. * @return void
  232. */
  233. public function testIncrementFloatPad()
  234. {
  235. $this->helper->init([
  236. 'total' => 50,
  237. ]);
  238. $expected = [
  239. '',
  240. '=========> 14%',
  241. '',
  242. '====================> 28%',
  243. '',
  244. '==============================> 42%',
  245. '',
  246. '=========================================> 56%',
  247. '',
  248. '===================================================> 70%',
  249. '',
  250. '========================================================> 76%',
  251. '',
  252. '==============================================================> 84%',
  253. '',
  254. '==========================================================================> 100%',
  255. ];
  256. $this->helper->increment(7);
  257. $this->helper->draw();
  258. $this->helper->increment(7);
  259. $this->helper->draw();
  260. $this->helper->increment(7);
  261. $this->helper->draw();
  262. $this->helper->increment(7);
  263. $this->helper->draw();
  264. $this->helper->increment(7);
  265. $this->helper->draw();
  266. $this->helper->increment(3);
  267. $this->helper->draw();
  268. $this->helper->increment(4);
  269. $this->helper->draw();
  270. $this->helper->increment(8);
  271. $this->helper->draw();
  272. $this->assertEquals($expected, $this->stub->messages());
  273. }
  274. }