ProgressBarTest.php 6.0 KB

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