ProgressHelperTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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\Command\Helper;
  17. use Cake\Command\Helper\ProgressHelper;
  18. use Cake\Console\ConsoleIo;
  19. use Cake\Console\TestSuite\StubConsoleOutput;
  20. use Cake\TestSuite\TestCase;
  21. use InvalidArgumentException;
  22. /**
  23. * ProgressHelper test.
  24. */
  25. class ProgressHelperTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\Command\Helper\ProgressHelper
  29. */
  30. protected ProgressHelper $helper;
  31. /**
  32. * @var \Cake\Console\TestSuite\StubConsoleOutput
  33. */
  34. protected StubConsoleOutput $stub;
  35. /**
  36. * @var \Cake\Console\ConsoleIo
  37. */
  38. protected ConsoleIo $io;
  39. /**
  40. * setUp method
  41. */
  42. public function setUp(): void
  43. {
  44. parent::setUp();
  45. $this->stub = new StubConsoleOutput();
  46. $this->io = new ConsoleIo($this->stub);
  47. $this->helper = new ProgressHelper($this->io);
  48. }
  49. /**
  50. * Test using the helper manually.
  51. */
  52. public function testInit(): void
  53. {
  54. $helper = $this->helper->init([
  55. 'total' => 200,
  56. 'width' => 50,
  57. ]);
  58. $this->assertSame($helper, $this->helper, 'Should be chainable');
  59. }
  60. public function testIncrementWithoutInit(): void
  61. {
  62. $this->helper->increment(10);
  63. $this->helper->draw();
  64. $expected = [
  65. '',
  66. '======> 10%',
  67. ];
  68. $this->assertEquals($expected, $this->stub->messages());
  69. }
  70. /**
  71. * Test that a callback is required.
  72. */
  73. public function testOutputFailure(): void
  74. {
  75. $this->expectException(InvalidArgumentException::class);
  76. $this->helper->output(['not a callback']);
  77. }
  78. /**
  79. * Test that the callback is invoked until 100 is reached.
  80. */
  81. public function testOutputSuccess(): void
  82. {
  83. $this->helper->output([function (ProgressHelper $progress): void {
  84. $progress->increment(20);
  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 output with options
  104. */
  105. public function testOutputSuccessOptions(): void
  106. {
  107. $this->helper->output([
  108. 'total' => 10,
  109. 'width' => 20,
  110. 'callback' => function (ProgressHelper $progress): void {
  111. $progress->increment(2);
  112. },
  113. ]);
  114. $expected = [
  115. '',
  116. '',
  117. '==> 20%',
  118. '',
  119. '=====> 40%',
  120. '',
  121. '========> 60%',
  122. '',
  123. '===========> 80%',
  124. '',
  125. '==============> 100%',
  126. '',
  127. ];
  128. $this->assertEquals($expected, $this->stub->messages());
  129. }
  130. /**
  131. * Test using the helper manually.
  132. */
  133. public function testIncrementAndRender(): void
  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. public function testIncrementAndRenderChained(): void
  156. {
  157. $this->helper->init()
  158. ->increment(20)
  159. ->draw()
  160. ->increment(40)
  161. ->draw()
  162. ->increment(40)
  163. ->draw();
  164. $expected = [
  165. '',
  166. '==============> 20%',
  167. '',
  168. '============================================> 60%',
  169. '',
  170. '==========================================================================> 100%',
  171. ];
  172. $this->assertEquals($expected, $this->stub->messages());
  173. }
  174. /**
  175. * Test negative numbers
  176. */
  177. public function testIncrementWithNegatives(): void
  178. {
  179. $this->helper->init();
  180. $this->helper->increment(40);
  181. $this->helper->draw();
  182. $this->helper->increment(-60);
  183. $this->helper->draw();
  184. $this->helper->increment(80);
  185. $this->helper->draw();
  186. $expected = [
  187. '',
  188. '=============================> 40%',
  189. '',
  190. ' 0%',
  191. '',
  192. '===========================================================> 80%',
  193. ];
  194. $this->assertEquals($expected, $this->stub->messages());
  195. }
  196. /**
  197. * Test increment and draw with options
  198. */
  199. public function testIncrementWithOptions(): void
  200. {
  201. $this->helper->init([
  202. 'total' => 10,
  203. 'width' => 20,
  204. ]);
  205. $expected = [
  206. '',
  207. '=====> 40%',
  208. '',
  209. '===========> 80%',
  210. '',
  211. '==============> 100%',
  212. ];
  213. $this->helper->increment(4);
  214. $this->helper->draw();
  215. $this->helper->increment(4);
  216. $this->helper->draw();
  217. $this->helper->increment(4);
  218. $this->helper->draw();
  219. $this->assertEquals($expected, $this->stub->messages());
  220. }
  221. /**
  222. * Test increment and draw with value that makes the pad
  223. * be a float
  224. */
  225. public function testIncrementFloatPad(): void
  226. {
  227. $this->helper->init([
  228. 'total' => 50,
  229. ]);
  230. $expected = [
  231. '',
  232. '=========> 14%',
  233. '',
  234. '====================> 28%',
  235. '',
  236. '==============================> 42%',
  237. '',
  238. '=========================================> 56%',
  239. '',
  240. '===================================================> 70%',
  241. '',
  242. '========================================================> 76%',
  243. '',
  244. '==============================================================> 84%',
  245. '',
  246. '==========================================================================> 100%',
  247. ];
  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(7);
  255. $this->helper->draw();
  256. $this->helper->increment(7);
  257. $this->helper->draw();
  258. $this->helper->increment(3);
  259. $this->helper->draw();
  260. $this->helper->increment(4);
  261. $this->helper->draw();
  262. $this->helper->increment(8);
  263. $this->helper->draw();
  264. $this->assertEquals($expected, $this->stub->messages());
  265. }
  266. }