UtilTest.php 7.9 KB

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