ProgressHelperTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 3.1.0
  13. * @license http://www.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 that a callback is required.
  39. *
  40. * @expectedException \RuntimeException
  41. */
  42. public function testOutputFailure()
  43. {
  44. $this->helper->output(['not a callback']);
  45. }
  46. /**
  47. * Test that the callback is invoked until 100 is reached.
  48. *
  49. * @return void
  50. */
  51. public function testOutputSuccess()
  52. {
  53. $this->helper->output([function ($progress) {
  54. $progress->increment(20);
  55. }]);
  56. $expected = [
  57. '',
  58. '==============> 20%',
  59. '',
  60. '=============================> 40%',
  61. '',
  62. '============================================> 60%',
  63. '',
  64. '===========================================================> 80%',
  65. '',
  66. '==========================================================================> 100%',
  67. '',
  68. ];
  69. $this->assertEquals($expected, $this->stub->messages());
  70. }
  71. /**
  72. * Test output with options
  73. *
  74. * @return void
  75. */
  76. public function testOutputSuccessOptions()
  77. {
  78. $this->helper->output([
  79. 'total' => 10,
  80. 'width' => 20,
  81. 'callback' => function ($progress) {
  82. $progress->increment(2);
  83. }
  84. ]);
  85. $expected = [
  86. '',
  87. '==> 20%',
  88. '',
  89. '=====> 40%',
  90. '',
  91. '========> 60%',
  92. '',
  93. '===========> 80%',
  94. '',
  95. '==============> 100%',
  96. '',
  97. ];
  98. $this->assertEquals($expected, $this->stub->messages());
  99. }
  100. /**
  101. * Test using the helper manually.
  102. *
  103. * @return void
  104. */
  105. public function testIncrementAndRender()
  106. {
  107. $this->helper->init();
  108. $this->helper->increment(20);
  109. $this->helper->draw();
  110. $this->helper->increment(40);
  111. $this->helper->draw();
  112. $this->helper->increment(40);
  113. $this->helper->draw();
  114. $expected = [
  115. '',
  116. '==============> 20%',
  117. '',
  118. '============================================> 60%',
  119. '',
  120. '==========================================================================> 100%',
  121. ];
  122. $this->assertEquals($expected, $this->stub->messages());
  123. }
  124. /**
  125. * Test negative numbers
  126. *
  127. * @return void
  128. */
  129. public function testIncrementWithNegatives()
  130. {
  131. $this->helper->init();
  132. $this->helper->increment(40);
  133. $this->helper->draw();
  134. $this->helper->increment(-60);
  135. $this->helper->draw();
  136. $this->helper->increment(80);
  137. $this->helper->draw();
  138. $expected = [
  139. '',
  140. '=============================> 40%',
  141. '',
  142. ' 0%',
  143. '',
  144. '===========================================================> 80%',
  145. ];
  146. $this->assertEquals($expected, $this->stub->messages());
  147. }
  148. /**
  149. * Test increment and draw with options
  150. *
  151. * @return void
  152. */
  153. public function testIncrementWithOptions()
  154. {
  155. $this->helper->init([
  156. 'total' => 10,
  157. 'width' => 20,
  158. ]);
  159. $expected = [
  160. '',
  161. '=====> 40%',
  162. '',
  163. '===========> 80%',
  164. '',
  165. '==============> 100%',
  166. ];
  167. $this->helper->increment(4);
  168. $this->helper->draw();
  169. $this->helper->increment(4);
  170. $this->helper->draw();
  171. $this->helper->increment(4);
  172. $this->helper->draw();
  173. $this->assertEquals($expected, $this->stub->messages());
  174. }
  175. /**
  176. * Test increment and draw with value that makes the pad
  177. * be a float
  178. *
  179. * @return void
  180. */
  181. public function testIncrementFloatPad()
  182. {
  183. $this->helper->init([
  184. 'total' => 50
  185. ]);
  186. $expected = [
  187. '',
  188. '=========> 14%',
  189. '',
  190. '====================> 28%',
  191. '',
  192. '==============================> 42%',
  193. '',
  194. '=========================================> 56%',
  195. '',
  196. '===================================================> 70%',
  197. '',
  198. '========================================================> 76%',
  199. '',
  200. '==============================================================> 84%',
  201. '',
  202. '==========================================================================> 100%',
  203. ];
  204. $this->helper->increment(7);
  205. $this->helper->draw();
  206. $this->helper->increment(7);
  207. $this->helper->draw();
  208. $this->helper->increment(7);
  209. $this->helper->draw();
  210. $this->helper->increment(7);
  211. $this->helper->draw();
  212. $this->helper->increment(7);
  213. $this->helper->draw();
  214. $this->helper->increment(3);
  215. $this->helper->draw();
  216. $this->helper->increment(4);
  217. $this->helper->draw();
  218. $this->helper->increment(8);
  219. $this->helper->draw();
  220. $this->assertEquals($expected, $this->stub->messages());
  221. }
  222. }