UtilTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_Gdata
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Http/Client.php';
  22. require_once 'Zend/Gdata/App/Util.php';
  23. require_once 'Zend/Gdata/App/Exception.php';
  24. /**
  25. * @package Zend_Gdata
  26. * @subpackage UnitTests
  27. */
  28. class Zend_Gdata_App_UtilTest extends PHPUnit_Framework_TestCase
  29. {
  30. public function testFormatTimestampFromString()
  31. {
  32. // assert that a correctly formatted timestamp is not modified
  33. $date = Zend_Gdata_App_Util::formatTimestamp('2006-12-01');
  34. $this->assertEquals('2006-12-01', $date);
  35. }
  36. public function testFormatTimestampFromStringWithTimezone()
  37. {
  38. // assert that a correctly formatted timestamp is not modified
  39. $date = Zend_Gdata_App_Util::formatTimestamp('2007-01-10T13:31:12-04:00');
  40. $this->assertEquals('2007-01-10T13:31:12-04:00', $date);
  41. }
  42. public function testFormatTimestampWithMilliseconds()
  43. {
  44. // assert that a correctly formatted timestamp is not modified
  45. $date = Zend_Gdata_App_Util::formatTimestamp('1956-12-14T43:09:54.52376Z');
  46. $this->assertEquals('1956-12-14T43:09:54.52376Z', $date);
  47. }
  48. public function testFormatTimestampUsingZuluAsOffset()
  49. {
  50. // assert that a correctly formatted timestamp is not modified
  51. $date = Zend_Gdata_App_Util::formatTimestamp('2024-03-19T01:38:12Z');
  52. $this->assertEquals('2024-03-19T01:38:12Z', $date);
  53. }
  54. public function testFormatTimestampUsingLowercaseTAndZ()
  55. {
  56. // assert that a correctly formatted timestamp is not modified
  57. $date = Zend_Gdata_App_Util::formatTimestamp('1945-07-19t12:19:08z');
  58. $this->assertEquals('1945-07-19t12:19:08z', $date);
  59. }
  60. public function testFormatTimestampFromStringWithNonCompliantDate()
  61. {
  62. // assert that a non-compliant date is converted to RFC 3339
  63. $date = Zend_Gdata_App_Util::formatTimestamp('2007/07/13');
  64. $this->assertEquals('2007-07-13T00:00:00', $date);
  65. }
  66. public function testFormatTimestampFromInteger()
  67. {
  68. $ts = 1164960000; // Fri Dec 1 00:00:00 PST 2006
  69. $date = Zend_Gdata_App_Util::formatTimestamp($ts);
  70. $this->assertEquals('2006-12-01T08:00:00+00:00', $date);
  71. }
  72. public function testExceptionFormatTimestampNonsense()
  73. {
  74. $util = new Zend_Gdata_App_Util();
  75. try {
  76. $ts = Zend_Gdata_App_Util::formatTimestamp('nonsense string');
  77. } catch (Zend_Gdata_App_Exception $e) {
  78. $this->assertEquals('Invalid timestamp: nonsense string.', $e->getMessage());
  79. return;
  80. }
  81. // Excetion not thrown, this is bad.
  82. $this->fail("Exception not thrown.");
  83. }
  84. public function testExceptionFormatTimestampSemiInvalid()
  85. {
  86. $util = new Zend_Gdata_App_Util();
  87. try {
  88. $ts = Zend_Gdata_App_Util::formatTimestamp('2007-06-05adslfkja');
  89. } catch (Zend_Gdata_App_Exception $e) {
  90. $this->assertEquals('Invalid timestamp: 2007-06-05adslfkja.', $e->getMessage());
  91. return;
  92. }
  93. // Excetion not thrown, this is bad.
  94. $this->fail("Exception not thrown.");
  95. }
  96. public function testExceptionFormatTimestampInvalidTime()
  97. {
  98. $util = new Zend_Gdata_App_Util();
  99. try {
  100. $ts = Zend_Gdata_App_Util::formatTimestamp('2007-06-05Tadslfkja');
  101. } catch (Zend_Gdata_App_Exception $e) {
  102. $this->assertEquals('Invalid timestamp: 2007-06-05Tadslfkja.', $e->getMessage());
  103. return;
  104. }
  105. // Excetion not thrown, this is bad.
  106. $this->fail("Exception not thrown.");
  107. }
  108. public function testExceptionFormatTimestampInvalidOffset()
  109. {
  110. $util = new Zend_Gdata_App_Util();
  111. try {
  112. $ts = Zend_Gdata_App_Util::formatTimestamp('2007-06-05T02:51:12+egg');
  113. } catch (Zend_Gdata_App_Exception $e) {
  114. $this->assertEquals('Invalid timestamp: 2007-06-05T02:51:12+egg.', $e->getMessage());
  115. return;
  116. }
  117. // Excetion not thrown, this is bad.
  118. $this->fail("Exception not thrown.");
  119. }
  120. public function testExceptionFormatTimestampInvalidOffsetHours()
  121. {
  122. $util = new Zend_Gdata_App_Util();
  123. try {
  124. $ts = Zend_Gdata_App_Util::formatTimestamp('2007-06-05T02:51:12-ab:00');
  125. } catch (Zend_Gdata_App_Exception $e) {
  126. $this->assertEquals('Invalid timestamp: 2007-06-05T02:51:12-ab:00.', $e->getMessage());
  127. return;
  128. }
  129. // Excetion not thrown, this is bad.
  130. $this->fail("Exception not thrown.");
  131. }
  132. public function testFindGreatestBoundedValueReturnsMax() {
  133. $data = array(-1 => null,
  134. 0 => null,
  135. 1 => null,
  136. 2 => null,
  137. 3 => null,
  138. 5 => null,
  139. -2 => null);
  140. $result = Zend_Gdata_App_Util::findGreatestBoundedValue(99, $data);
  141. $this->assertEquals(5, $result);
  142. }
  143. public function testFindGreatestBoundedValueReturnsMaxWhenBounded() {
  144. $data = array(-1 => null,
  145. 0 => null,
  146. 1 => null,
  147. 2 => null,
  148. 3 => null,
  149. 5 => null,
  150. -2 => null);
  151. $result = Zend_Gdata_App_Util::findGreatestBoundedValue(4, $data);
  152. $this->assertEquals(3, $result);
  153. }
  154. public function testFindGreatestBoundedValueReturnsMaxWhenUnbounded() {
  155. $data = array(-1 => null,
  156. 0 => null,
  157. 1 => null,
  158. 2 => null,
  159. 3 => null,
  160. 5 => null,
  161. -2 => null);
  162. $result = Zend_Gdata_App_Util::findGreatestBoundedValue(null, $data);
  163. $this->assertEquals(5, $result);
  164. }
  165. public function testFindGreatestBoundedValueReturnsZeroWhenZeroBounded() {
  166. $data = array(-1 => null,
  167. 0 => null,
  168. 1 => null,
  169. 2 => null,
  170. 3 => null,
  171. 5 => null,
  172. -2 => null);
  173. $result = Zend_Gdata_App_Util::findGreatestBoundedValue(0, $data);
  174. $this->assertEquals(0, $result);
  175. }
  176. public function testFindGreatestBoundedValueFailsWhenNegativelyBounded() {
  177. $data = array(-1 => null,
  178. 0 => null,
  179. 1 => null,
  180. 2 => null,
  181. 3 => null,
  182. 5 => null,
  183. -2 => null);
  184. try {
  185. $result = Zend_Gdata_App_Util::findGreatestBoundedValue(-1, $data);
  186. $failed = true;
  187. } catch (Zend_Gdata_App_Exception $e) {
  188. $failed = false;
  189. }
  190. $this->assertFalse($failed, 'Exception not raised.');
  191. }
  192. }