ProgressHelperTest.php 8.3 KB

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