2
0

ProgressBarTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_Text
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 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. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../TestHelper.php';
  30. /**
  31. * Zend_ProgressBar
  32. */
  33. require_once 'Zend/ProgressBar.php';
  34. /**
  35. * Zend_ProgressBar_Adapter
  36. */
  37. require_once 'Zend/ProgressBar/Adapter.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Console
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. class Zend_ProgressBar_ProgressBarTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @return void
  51. */
  52. public static function main()
  53. {
  54. $suite = new PHPUnit_Framework_TestSuite("Zend_ProgressBar_ProgressBarTest");
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. public function testGreaterMin()
  58. {
  59. try {
  60. $progressBar = $this->_getProgressBar(1, 0);
  61. $this->fail('An expected Zend_Console_Exception has not been raised');
  62. } catch (Zend_ProgressBar_Exception $expected) {
  63. $this->assertContains('$max must be greater than $min', $expected->getMessage());
  64. }
  65. }
  66. public function testPersistence()
  67. {
  68. $progressBar = $this->_getProgressBar(0, 100, 'foobar');
  69. $progressBar->update(25);
  70. $progressBar = $this->_getProgressBar(0, 100, 'foobar');
  71. $progressBar->update();
  72. $this->assertEquals(25, $progressBar->getCurrent());
  73. }
  74. public function testDefaultPercentage()
  75. {
  76. $progressBar = $this->_getProgressBar(0, 100);
  77. $progressBar->update(25);
  78. $this->assertEquals(.25, $progressBar->getPercent());
  79. }
  80. public function testPositiveToPositivePercentage()
  81. {
  82. $progressBar = $this->_getProgressBar(10, 20);
  83. $progressBar->update(12.5);
  84. $this->assertEquals(.25, $progressBar->getPercent());
  85. }
  86. public function testNegativeToPositivePercentage()
  87. {
  88. $progressBar = $this->_getProgressBar(-5, 5);
  89. $progressBar->update(-2.5);
  90. $this->assertEquals(.25, $progressBar->getPercent());
  91. }
  92. public function testNegativeToNegativePercentage()
  93. {
  94. $progressBar = $this->_getProgressBar(-20, -10);
  95. $progressBar->update(-17.5);
  96. $this->assertEquals(.25, $progressBar->getPercent());
  97. }
  98. public function testEtaCalculation()
  99. {
  100. $progressBar = $this->_getProgressBar(0, 100);
  101. $progressBar->sleep(3);
  102. $progressBar->update(33);
  103. $progressBar->sleep(3);
  104. $progressBar->update(66);
  105. $this->assertEquals(3, $progressBar->getTimeRemaining());
  106. }
  107. public function testEtaZeroPercent()
  108. {
  109. $progressBar = $this->_getProgressBar(0, 100);
  110. $progressBar->sleep(5);
  111. $progressBar->update(0);
  112. $this->assertEquals(null, $progressBar->getTimeRemaining());
  113. }
  114. protected function _getProgressBar($min, $max, $persistenceNamespace = null)
  115. {
  116. return new Zend_ProgressBar_Stub(new Zend_ProgressBar_Adapter_MockUp(), $min, $max, $persistenceNamespace);
  117. }
  118. }
  119. class Zend_ProgressBar_Stub extends Zend_ProgressBar
  120. {
  121. public function sleep($seconds)
  122. {
  123. $this->_startTime -= $seconds;
  124. }
  125. public function getCurrent()
  126. {
  127. return $this->_adapter->getCurrent();
  128. }
  129. public function getMax()
  130. {
  131. return $this->_adapter->getMax();
  132. }
  133. public function getPercent()
  134. {
  135. return $this->_adapter->getPercent();
  136. }
  137. public function getTimeTaken()
  138. {
  139. return $this->_adapter->getTimeTaken();
  140. }
  141. public function getTimeRemaining()
  142. {
  143. return $this->_adapter->getTimeRemaining();
  144. }
  145. public function getText()
  146. {
  147. return $this->_adapter->getText();
  148. }
  149. }
  150. class Zend_ProgressBar_Adapter_MockUp extends Zend_ProgressBar_Adapter
  151. {
  152. protected $_current;
  153. protected $_max;
  154. protected $_percent;
  155. protected $_timeTaken;
  156. protected $_timeRemaining;
  157. protected $_text;
  158. public function notify($current, $max, $percent, $timeTaken, $timeRemaining, $text)
  159. {
  160. $this->_current = $current;
  161. $this->_max = $max;
  162. $this->_percent = $percent;
  163. $this->_timeTaken = $timeTaken;
  164. $this->_timeRemaining = $timeRemaining;
  165. $this->_text = $text;
  166. }
  167. public function finish()
  168. {
  169. }
  170. public function getCurrent()
  171. {
  172. return $this->_current;
  173. }
  174. public function getMax()
  175. {
  176. return $this->_max;
  177. }
  178. public function getPercent()
  179. {
  180. return $this->_percent;
  181. }
  182. public function getTimeTaken()
  183. {
  184. return $this->_timeTaken;
  185. }
  186. public function getTimeRemaining()
  187. {
  188. return $this->_timeRemaining;
  189. }
  190. public function getText()
  191. {
  192. return $this->_text;
  193. }
  194. }
  195. // Call Zend_ProgressBar_ProgressBarTest::main() if this source file is executed directly.
  196. if (PHPUnit_MAIN_METHOD == "Zend_ProgressBar_ProgressBarTest::main") {
  197. Zend_ProgressBar_ProgressBarTest::main();
  198. }