ProgressTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use League\CLImate\Exceptions\UnexpectedValueException;
  4. class ProgressTest extends TestBase
  5. {
  6. /**
  7. * The string length of the bar when at 100%
  8. *
  9. * @var integer $bar_str_len
  10. */
  11. protected $bar_str_len;
  12. /**
  13. * @param integer $length
  14. * @return string
  15. */
  16. private function repeat($length)
  17. {
  18. if (!$this->bar_str_len) {
  19. // Subtract 10 because of the '> 100%' plus some padding, max 100
  20. $this->bar_str_len = min($this->util->width() - 10, 100);
  21. }
  22. $repeat = ($length / 100) * $this->bar_str_len;
  23. $bar = str_repeat('=', $repeat);
  24. $bar .= '>';
  25. $bar .= str_repeat(' ', max($this->bar_str_len - $repeat, 0));
  26. return $bar;
  27. }
  28. /**
  29. * @test
  30. * @doesNotPerformAssertions
  31. */
  32. public function it_can_output_a_progress_bar()
  33. {
  34. $this->shouldWrite('');
  35. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(0)} 0%\e[0m");
  36. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(10)} 10%\e[0m");
  37. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(20)} 20%\e[0m");
  38. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(30)} 30%\e[0m");
  39. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(40)} 40%\e[0m");
  40. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
  41. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(60)} 60%\e[0m");
  42. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(70)} 70%\e[0m");
  43. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(80)} 80%\e[0m");
  44. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(90)} 90%\e[0m");
  45. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  46. $progress = $this->cli->progress()->total(10);
  47. for ($i = 0; $i <= 10; $i++) {
  48. $progress->current($i);
  49. }
  50. }
  51. /**
  52. * @test
  53. * @doesNotPerformAssertions
  54. */
  55. public function it_can_output_a_progress_bar_via_constructor()
  56. {
  57. $this->shouldWrite('');
  58. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(0)} 0%\e[0m");
  59. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(10)} 10%\e[0m");
  60. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(20)} 20%\e[0m");
  61. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(30)} 30%\e[0m");
  62. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(40)} 40%\e[0m");
  63. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
  64. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(60)} 60%\e[0m");
  65. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(70)} 70%\e[0m");
  66. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(80)} 80%\e[0m");
  67. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(90)} 90%\e[0m");
  68. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  69. $progress = $this->cli->progress(10);
  70. for ($i = 0; $i <= 10; $i++) {
  71. $progress->current($i);
  72. }
  73. }
  74. /**
  75. * @test
  76. * @doesNotPerformAssertions
  77. */
  78. public function it_can_output_a_progress_bar_with_current_labels()
  79. {
  80. $this->shouldWrite('');
  81. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(0)} 0%\n\r\e[Kzeroth\e[0m");
  82. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(10)} 10%\n\r\e[Kfirst\e[0m");
  83. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(20)} 20%\n\r\e[Ksecond\e[0m");
  84. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(30)} 30%\n\r\e[Kthird\e[0m");
  85. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(40)} 40%\n\r\e[Kfourth\e[0m");
  86. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(50)} 50%\n\r\e[Kfifth\e[0m");
  87. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(60)} 60%\n\r\e[Ksixth\e[0m");
  88. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(70)} 70%\n\r\e[Kseventh\e[0m");
  89. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(80)} 80%\n\r\e[Keighth\e[0m");
  90. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(90)} 90%\n\r\e[Kninth\e[0m");
  91. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(100)} 100%\n\r\e[Ktenth\e[0m");
  92. $progress = $this->cli->progress(10);
  93. $labels = [
  94. 'zeroth',
  95. 'first',
  96. 'second',
  97. 'third',
  98. 'fourth',
  99. 'fifth',
  100. 'sixth',
  101. 'seventh',
  102. 'eighth',
  103. 'ninth',
  104. 'tenth',
  105. ];
  106. for ($i = 0; $i <= 10; $i++) {
  107. $progress->current($i, $labels[$i]);
  108. }
  109. }
  110. /**
  111. * @test
  112. * @doesNotPerformAssertions
  113. */
  114. public function it_can_output_a_progress_bar_with_current_optional_labels()
  115. {
  116. $this->shouldWrite('');
  117. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(0)} 0%\n\r\e[Kzeroth\e[0m");
  118. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(10)} 10%\n\r\e[K\e[0m");
  119. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(20)} 20%\n\r\e[Ksecond\e[0m");
  120. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(30)} 30%\n\r\e[Kthird\e[0m");
  121. $progress = $this->cli->progress(10);
  122. $labels = [
  123. 'zeroth',
  124. '',
  125. 'second',
  126. 'third',
  127. ];
  128. for ($i = 0; $i <= 3; $i++) {
  129. $progress->current($i, $labels[$i]);
  130. }
  131. }
  132. /**
  133. * @test
  134. * @doesNotPerformAssertions
  135. */
  136. public function it_can_output_a_styled_progress_bar()
  137. {
  138. $this->shouldWrite('');
  139. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(0)} 0%\e[0m");
  140. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(10)} 10%\e[0m");
  141. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(20)} 20%\e[0m");
  142. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(30)} 30%\e[0m");
  143. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(40)} 40%\e[0m");
  144. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
  145. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(60)} 60%\e[0m");
  146. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(70)} 70%\e[0m");
  147. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(80)} 80%\e[0m");
  148. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(90)} 90%\e[0m");
  149. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  150. $progress = $this->cli->redProgress(10);
  151. for ($i = 0; $i <= 10; $i++) {
  152. $progress->current($i);
  153. }
  154. }
  155. /**
  156. * @test
  157. * @doesNotPerformAssertions
  158. */
  159. public function it_can_output_a_styled_progress_bar_and_resets_the_style()
  160. {
  161. $this->shouldWrite('');
  162. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(0)} 0%\e[0m");
  163. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(10)} 10%\e[0m");
  164. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(20)} 20%\e[0m");
  165. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(30)} 30%\e[0m");
  166. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(40)} 40%\e[0m");
  167. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
  168. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(60)} 60%\e[0m");
  169. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(70)} 70%\e[0m");
  170. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(80)} 80%\e[0m");
  171. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(90)} 90%\e[0m");
  172. $this->shouldWrite("\e[31m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  173. $this->shouldWrite("\e[mand back to normal\e[0m");
  174. $this->shouldHavePersisted();
  175. $progress = $this->cli->redProgress(10);
  176. for ($i = 0; $i <= 10; $i++) {
  177. $progress->current($i);
  178. }
  179. $this->cli->out('and back to normal');
  180. }
  181. /**
  182. * @test
  183. */
  184. public function it_can_throws_an_exception_for_a_zero_total_progress_bar()
  185. {
  186. $this->expectException(UnexpectedValueException::class);
  187. $this->expectExceptionMessage("The progress total must be greater than zero.");
  188. $progress = $this->cli->progress();
  189. $progress->current(0);
  190. }
  191. /**
  192. * @test
  193. */
  194. public function it_can_throws_an_exception_when_the_current_is_greater_than_the_total()
  195. {
  196. $this->expectException(UnexpectedValueException::class);
  197. $this->expectExceptionMessage("The current is greater than the total.");
  198. $progress = $this->cli->progress(1);
  199. $progress->current(2);
  200. }
  201. /**
  202. * @test
  203. * @doesNotPerformAssertions
  204. */
  205. public function it_can_output_a_progress_bar_using_increments()
  206. {
  207. $this->shouldWrite('');
  208. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(10)} 10%\e[0m");
  209. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(20)} 20%\e[0m");
  210. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(70)} 70%\e[0m");
  211. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
  212. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  213. $progress = $this->cli->progress()->total(10);
  214. $progress->advance();
  215. $progress->advance(1);
  216. $progress->advance(5);
  217. $progress->advance(-2);
  218. $progress->advance(5);
  219. }
  220. /**
  221. * @test
  222. * @doesNotPerformAssertions
  223. */
  224. public function it_can_output_a_progress_bar_using_increments_with_label()
  225. {
  226. $this->shouldWrite('');
  227. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(10)} 10%\n\r\e[Kstart\e[0m");
  228. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(20)} 20%\n\r\e[Knext\e[0m");
  229. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(100)} 100%\n\r\e[Kfinal\e[0m");
  230. $progress = $this->cli->progress()->total(10);
  231. $progress->advance(1, 'start');
  232. $progress->advance(1, 'next');
  233. $progress->advance(8, 'final');
  234. }
  235. /**
  236. * @test
  237. * @doesNotPerformAssertions
  238. */
  239. public function it_will_force_a_redraw_if_specified()
  240. {
  241. $this->shouldWrite('');
  242. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(20)} 20%\e[0m");
  243. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(40)} 40%\e[0m");
  244. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(40)} 40%\e[0m");
  245. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(60)} 60%\e[0m");
  246. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(80)} 80%\e[0m");
  247. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  248. $progress = $this->cli->progress()->total(5);
  249. $progress->forceRedraw();
  250. $items = [1, 2, 2, 3, 4, 5];
  251. foreach ($items as $item) {
  252. $progress->current($item);
  253. }
  254. }
  255. /**
  256. * @test
  257. * @doesNotPerformAssertions
  258. */
  259. public function it_will_not_force_a_redraw_if_disabled()
  260. {
  261. $this->shouldWrite('');
  262. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(20)} 20%\e[0m");
  263. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(40)} 40%\e[0m");
  264. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(60)} 60%\e[0m");
  265. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(80)} 80%\e[0m");
  266. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  267. $progress = $this->cli->progress()->total(5);
  268. $progress->forceRedraw(false);
  269. $items = [1, 2, 2, 3, 4, 5];
  270. foreach ($items as $item) {
  271. $progress->current($item);
  272. }
  273. }
  274. /**
  275. * @doesNotPerformAssertions
  276. */
  277. public function testEach1()
  278. {
  279. $this->shouldWrite('');
  280. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
  281. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  282. $this->cli->progress()->each([1, 2]);
  283. }
  284. public function testEach2()
  285. {
  286. $this->shouldWrite('');
  287. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
  288. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  289. $items = [];
  290. $this->cli->progress()->each(["two", "one"], function ($item) use (&$items) {
  291. $items[] = $item;
  292. });
  293. $this->assertSame(["two", "one"], $items);
  294. }
  295. public function testEach3()
  296. {
  297. $this->shouldWrite('');
  298. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
  299. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");
  300. $items = [];
  301. $this->cli->progress()->each(["key2" => "two", "key1" => "one"], function ($item, $key) use (&$items) {
  302. $items[$key] = $item;
  303. });
  304. $this->assertSame(["key2" => "two", "key1" => "one"], $items);
  305. }
  306. /**
  307. * @doesNotPerformAssertions
  308. */
  309. public function testEach4()
  310. {
  311. $this->shouldWrite('');
  312. $this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(20)} 20%\n\r\e[Kone\e[0m");
  313. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(40)} 40%\n\r\e[Ktwo\e[0m");
  314. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(60)} 60%\n\r\e[Kthree\e[0m");
  315. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(80)} 80%\n\r\e[Kfour\e[0m");
  316. $this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(100)} 100%\n\r\e[Kfive\e[0m");
  317. $this->cli->progress()->each(["one", "two", "three", "four", "five"], function ($item) {
  318. return $item;
  319. });
  320. }
  321. }