ProgressHelperTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 a callback that never reaches 100 fails.
  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. $this->assertEquals($expected, $this->stub->messages());
  69. }
  70. /**
  71. * Test using the helper manually.
  72. *
  73. * @return void
  74. */
  75. public function testIncrementAndRender()
  76. {
  77. $this->helper->init();
  78. $this->helper->increment(20);
  79. $this->helper->draw();
  80. $this->helper->increment(40);
  81. $this->helper->draw();
  82. $this->helper->increment(40);
  83. $this->helper->draw();
  84. $expected = [
  85. '',
  86. '==============> 20%',
  87. '',
  88. '============================================> 60%',
  89. '',
  90. '==========================================================================> 100%',
  91. ];
  92. $this->assertEquals($expected, $this->stub->messages());
  93. }
  94. /**
  95. * Test negative numbers
  96. *
  97. * @return void
  98. */
  99. public function testIncrementWithNegatives()
  100. {
  101. $this->helper->init();
  102. $this->helper->increment(40);
  103. $this->helper->draw();
  104. $this->helper->increment(-60);
  105. $this->helper->draw();
  106. $this->helper->increment(80);
  107. $this->helper->draw();
  108. $expected = [
  109. '',
  110. '=============================> 40%',
  111. '',
  112. ' 0%',
  113. '',
  114. '===========================================================> 80%',
  115. ];
  116. $this->assertEquals($expected, $this->stub->messages());
  117. }
  118. }