ProgressHelperTest.php 8.3 KB

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