ProgressBarTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_ProgressBar
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Console_ProgressBarTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_ProgressBar_ProgressBarTest::main");
  25. }
  26. /**
  27. * Zend_ProgressBar
  28. */
  29. require_once 'Zend/ProgressBar.php';
  30. /**
  31. * Zend_ProgressBar_Adapter
  32. */
  33. require_once 'Zend/ProgressBar/Adapter.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Console
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_ProgressBar
  41. */
  42. class Zend_ProgressBar_ProgressBarTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_ProgressBar_ProgressBarTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. public function testGreaterMin()
  55. {
  56. try {
  57. $progressBar = $this->_getProgressBar(1, 0);
  58. $this->fail('An expected Zend_Console_Exception has not been raised');
  59. } catch (Zend_ProgressBar_Exception $expected) {
  60. $this->assertContains('$max must be greater than $min', $expected->getMessage());
  61. }
  62. }
  63. public function testPersistence()
  64. {
  65. $progressBar = $this->_getProgressBar(0, 100, 'foobar');
  66. $progressBar->update(25);
  67. $progressBar = $this->_getProgressBar(0, 100, 'foobar');
  68. $progressBar->update();
  69. $this->assertEquals(25, $progressBar->getCurrent());
  70. }
  71. public function testDefaultPercentage()
  72. {
  73. $progressBar = $this->_getProgressBar(0, 100);
  74. $progressBar->update(25);
  75. $this->assertEquals(.25, $progressBar->getPercent());
  76. }
  77. public function testPositiveToPositivePercentage()
  78. {
  79. $progressBar = $this->_getProgressBar(10, 20);
  80. $progressBar->update(12.5);
  81. $this->assertEquals(.25, $progressBar->getPercent());
  82. }
  83. public function testNegativeToPositivePercentage()
  84. {
  85. $progressBar = $this->_getProgressBar(-5, 5);
  86. $progressBar->update(-2.5);
  87. $this->assertEquals(.25, $progressBar->getPercent());
  88. }
  89. public function testNegativeToNegativePercentage()
  90. {
  91. $progressBar = $this->_getProgressBar(-20, -10);
  92. $progressBar->update(-17.5);
  93. $this->assertEquals(.25, $progressBar->getPercent());
  94. }
  95. public function testEtaCalculation()
  96. {
  97. $progressBar = $this->_getProgressBar(0, 100);
  98. $progressBar->sleep(3);
  99. $progressBar->update(33);
  100. $progressBar->sleep(3);
  101. $progressBar->update(66);
  102. $this->assertEquals(3, $progressBar->getTimeRemaining());
  103. }
  104. public function testEtaZeroPercent()
  105. {
  106. $progressBar = $this->_getProgressBar(0, 100);
  107. $progressBar->sleep(5);
  108. $progressBar->update(0);
  109. $this->assertEquals(null, $progressBar->getTimeRemaining());
  110. }
  111. protected function _getProgressBar($min, $max, $persistenceNamespace = null)
  112. {
  113. return new Zend_ProgressBar_Stub(new Zend_ProgressBar_Adapter_MockUp(), $min, $max, $persistenceNamespace);
  114. }
  115. }
  116. class Zend_ProgressBar_Stub extends Zend_ProgressBar
  117. {
  118. public function sleep($seconds)
  119. {
  120. $this->_startTime -= $seconds;
  121. }
  122. public function getCurrent()
  123. {
  124. return $this->_adapter->getCurrent();
  125. }
  126. public function getMax()
  127. {
  128. return $this->_adapter->getMax();
  129. }
  130. public function getPercent()
  131. {
  132. return $this->_adapter->getPercent();
  133. }
  134. public function getTimeTaken()
  135. {
  136. return $this->_adapter->getTimeTaken();
  137. }
  138. public function getTimeRemaining()
  139. {
  140. return $this->_adapter->getTimeRemaining();
  141. }
  142. public function getText()
  143. {
  144. return $this->_adapter->getText();
  145. }
  146. }
  147. class Zend_ProgressBar_Adapter_MockUp extends Zend_ProgressBar_Adapter
  148. {
  149. protected $_current;
  150. protected $_max;
  151. protected $_percent;
  152. protected $_timeTaken;
  153. protected $_timeRemaining;
  154. protected $_text;
  155. public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $text)
  156. {
  157. $this->_current = $current;
  158. $this->_max = $max;
  159. $this->_percent = $percent;
  160. $this->_timeTaken = $timeTaken;
  161. $this->_timeRemaining = $timeRemaining;
  162. $this->_text = $text;
  163. }
  164. public function finish()
  165. {
  166. }
  167. public function getCurrent()
  168. {
  169. return $this->_current;
  170. }
  171. public function getMax()
  172. {
  173. return $this->_max;
  174. }
  175. public function getPercent()
  176. {
  177. return $this->_percent;
  178. }
  179. public function getTimeTaken()
  180. {
  181. return $this->_timeTaken;
  182. }
  183. public function getTimeRemaining()
  184. {
  185. return $this->_timeRemaining;
  186. }
  187. public function getText()
  188. {
  189. return $this->_text;
  190. }
  191. }
  192. // Call Zend_ProgressBar_ProgressBarTest::main() if this source file is executed directly.
  193. if (PHPUnit_MAIN_METHOD == "Zend_ProgressBar_ProgressBarTest::main") {
  194. Zend_ProgressBar_ProgressBarTest::main();
  195. }