PageRenderTime 108ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 1ms

/tine20/library/qCal/tests/UnitTestCase/Date.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 253 lines | 106 code | 49 blank | 98 comment | 3 complexity | e26055a236ec448cfda374357df4cb7a MD5 | raw file
  1. <?php
  2. class UnitTestCase_Date extends UnitTestCase {
  3. /**
  4. * Set up test environment
  5. */
  6. public function setUp() {
  7. }
  8. /**
  9. * Tear down test environment
  10. */
  11. public function tearDown() {
  12. }
  13. /*public function testDateExceptionAcceptsDateObject() {
  14. $date = new qCal_Date;
  15. $exception = new qCal_DateTime_Exception("Foo", 0, null, $date);
  16. $this->assertEqual($exception->getDate(), $date);
  17. $this->expectException($exception);
  18. throw $exception;
  19. }*/
  20. /**
  21. * The object should default to the current date and time
  22. */
  23. public function testDateDefaultsToNow() {
  24. $date = new qCal_Date();
  25. $now = time();
  26. $this->assertEqual($date->getMonth(), date("m", $now));
  27. $this->assertEqual($date->getDay(), date("d", $now));
  28. $this->assertEqual($date->getYear(), date("Y", $now));
  29. // make sure that if only a portion of the date is given, the rest default to now
  30. $date2 = new qCal_Date(2006);
  31. $this->assertEqual($date2->getMonth(), date("m", $now));
  32. $this->assertEqual($date2->getDay(), date("d", $now));
  33. $this->assertEqual($date2->getYear(), "2006");
  34. $date3 = new qCal_Date(2006, 5);
  35. $this->assertEqual($date3->getDay(), date("d", $now));
  36. $this->assertEqual($date3->getMonth(), "5");
  37. $this->assertEqual($date3->getYear(), "2006");
  38. }
  39. public function testInvalidDateThrowsException() {
  40. $this->expectException(new qCal_DateTime_Exception_InvalidDate("Invalid date specified for qCal_Date: \"1/35/2009\""));
  41. $date = new qCal_Date(2009, 1, 35);
  42. }
  43. /**
  44. * The same instantiation as was done in the test above will not throw an exception, but instead
  45. * will just roll over the extra four days into the next month if you specify the fourth argument as true
  46. */
  47. public function testDateRollover() {
  48. $date = new qCal_Date(2009, 1, 35, true);
  49. $this->assertEqual($date->getMonth(), 2);
  50. $this->assertEqual($date->getDay(), 4);
  51. $this->assertEqual($date->getYear(), 2009);
  52. // make sure year can roll over too
  53. $date2 = new qCal_Date(2009, 12, 41, true);
  54. $this->assertEqual($date2->getMonth(), 1);
  55. $this->assertEqual($date2->getDay(), 10);
  56. $this->assertEqual($date2->getYear(), 2010);
  57. }
  58. /**
  59. * @todo I wish I could set the server's date to leap-year so that when the qCal_Date
  60. * class calls time() it would be leap-year. Then I could test this properly...
  61. */
  62. public function testInvalidLeapYear() {
  63. // you cannot specify a leap-year for a date that is not a leap-year
  64. $this->expectException(new qCal_DateTime_Exception_InvalidDate("Invalid date specified for qCal_Date: \"2/29/2009\""));
  65. $date = new qCal_Date(2009, 2, 29);
  66. }
  67. public function testSetDateByString() {
  68. // test that something like "tomorrow" works
  69. $tomorrow = qCal_Date::factory("tomorrow");
  70. // coming soon!
  71. // $this->assertEqual($tomorrow->getDay());
  72. }
  73. /**
  74. * Calling __toString will output the date in the format specified by calling setFormat()
  75. * You can use any of the date-related meta characters from php's date() function. Time-related
  76. * formatting will not work.
  77. */
  78. public function testToString() {
  79. $date = new qCal_Date(2009, 12, 7);
  80. // format defaults to m/d/Y
  81. $this->assertEqual($date->__toString(), '12/07/2009');
  82. // european format
  83. $date->setFormat('d/m/Y');
  84. $this->assertEqual($date->__toString(), '07/12/2009');
  85. // no leading zeros
  86. $date->setFormat('n/j/Y');
  87. $this->assertEqual($date->__toString(), '12/7/2009');
  88. // two-digit year
  89. $date->setFormat('n/j/y');
  90. $this->assertEqual($date->__toString(), '12/7/09');
  91. // time-related date meta-characters do not work
  92. $date->setFormat('m/d/Y h:i:sa');
  93. $this->assertEqual($date->__toString(), '12/07/2009 h:i:sa');
  94. // you can escape meta-characters with a backslash
  95. $date->setFormat('\m\d\ymdy');
  96. $this->assertEqual($date->__toString(), 'mdy120709');
  97. }
  98. /**
  99. * The date object has many getters which allow for you to determine things like day of the week,
  100. * day of the year, etc. The following tests those getters.
  101. */
  102. public function testGetters() {
  103. $date = new qCal_Date(2009, 4, 23);
  104. /**
  105. * Month
  106. */
  107. $this->assertEqual($date->getMonth(), 4);
  108. $this->assertEqual($date->getMonthName(), "April");
  109. $this->assertEqual($date->getNumDaysInMonth(), 30);
  110. /**
  111. * Day
  112. */
  113. $this->assertEqual($date->getDay(), 23);
  114. $this->assertEqual($date->getYearDay(), 112);
  115. $this->assertEqual($date->getFirstDayOfMonth()->__toString(), "04/01/2009");
  116. $this->assertEqual($date->getFirstDayOfMonth()->format("l"), "Wednesday");
  117. $this->assertEqual($date->getLastDayOfMonth()->__toString(), "04/30/2009");
  118. $this->assertEqual($date->getLastDayOfMonth()->format("l"), "Thursday");
  119. // find the xth weekday (mon-sun) of the month
  120. $this->assertEqual($date->getXthWeekdayOfMonth(2)->__toString(), "04/09/2009"); // find the second Thursday of the month (Because 4/23/2009 was on a Thursday, the weekday defaults to that. The year defaults to 2009 for basically the same reason)
  121. $this->assertEqual($date->getXthWeekdayOfMonth(2, "Monday")->__toString(), "04/13/2009"); // find the second monday of the month (month defaults to april because that's what $date is currently set to)
  122. $this->assertEqual($date->getXthWeekdayOfMonth(2, "Monday", "January")->__toString(), "01/12/2009"); // find the second monday in January (year defaults to 2009)
  123. $this->assertEqual($date->getXthWeekdayOfMonth(2, "Monday", "January", 2008)->__toString(), "01/14/2008"); // find the second Monday in January, 2008
  124. // now try negatives and positives
  125. $this->assertEqual($date->getXthWeekdayOfMonth(-2)->__toString(), "04/23/2009"); // get the second to last Thursday of the month
  126. $this->assertEqual($date->getXthWeekdayOfMonth("-2")->__toString(), "04/23/2009"); // get the second to last Thursday of the month
  127. $this->assertEqual($date->getXthWeekdayOfMonth(+2)->__toString(), "04/09/2009"); // surprisingly, this works... interesting...
  128. $this->assertEqual($date->getXthWeekdayOfMonth("+2")->__toString(), "04/09/2009");
  129. // we can also use numbers instead of spelling out the names of weekdays and months. For the weekday part, use 0 for Sunday through 6 for Saturday (the same as PHP's date function's "w" metacharacter)
  130. $this->assertEqual($date->getXthWeekdayOfMonth(2, 1)->__toString(), "04/13/2009"); // second monday
  131. $this->assertEqual($date->getXthWeekdayOfMonth(2, 1, 1)->__toString(), "01/12/2009"); // second monday in january
  132. $this->assertEqual($date->getXthWeekdayOfMonth(-2, 1)->__toString(), "04/20/2009"); // second to last monday in april
  133. /**
  134. * Year
  135. */
  136. $this->assertEqual($date->getYear(), 2009);
  137. /**
  138. * Week
  139. */
  140. $this->assertEqual($date->getWeekDay(), 4);
  141. $this->assertEqual($date->getWeekDayName(), "Thursday");
  142. $this->assertEqual($date->getWeekOfYear(), 17);
  143. /**
  144. * Unix Timestamp
  145. */
  146. $this->assertEqual($date->getUnixTimestamp(), gmmktime(0, 0, 0, 4, 23, 2009));
  147. }
  148. /**
  149. * Test that an exception is thrown if there is a request for an non-existant weekday in the month
  150. */
  151. public function testInvalidXthWeekday() {
  152. $this->expectException(new qCal_DateTime_Exception_InvalidDate("You have specified an incorrect number of days for qCal_Date::getXthWeekdayOfMonth()"));
  153. $date = new qCal_Date(2010, 1, 1);
  154. $tenth_tuesday = $date->getXthWeekdayOfMonth(10, "Tuesday");
  155. }
  156. /**
  157. * Any of the setters in the object will return the object itself
  158. */
  159. public function testFluidMethods() {
  160. $date = new qCal_Date(2009, 12, 17);
  161. $this->assertEqual($date->setFormat("m/d/Y")->__toString(), "12/17/2009");
  162. }
  163. /**
  164. * Test that date can determine if it is in a leap year
  165. */
  166. public function testIsLeapyear() {
  167. $date = new qCal_Date(2009, 4, 23);
  168. $this->assertFalse($date->isLeapYear());
  169. $date2 = new qCal_Date(2008, 4, 23);
  170. $this->assertTrue($date2->isLeapYear());
  171. }
  172. /**
  173. * Test that you can determine the amount of days in the year (usually 365, but 366 on leap-year)
  174. */
  175. public function testNumDaysInYear() {
  176. $date = new qCal_Date(2009, 4, 23);
  177. $this->assertEqual($date->getNumDaysInYear(), 365);
  178. $date2 = new qCal_Date(2008, 4, 23);
  179. $this->assertTrue($date2->getNumDaysInYear(), 366);
  180. }
  181. /**
  182. * The following are methods that test the date component's ability to do "date magic".
  183. * It tests things such as the date component's ability to determine if this is the 2nd
  184. * monday of the month, or the 2nd to last monday of the month. Or how many days from the
  185. * end of the year it is. Or whether it is the 2nd Tuesday of the year. Or whether
  186. */
  187. /**
  188. * This method tests that the date component is capable of determining of the date is the
  189. * Xth Xday of the month. For instance, it can determine if this date is the third Sunday
  190. * of the month or the second to last Tuesday of the month.
  191. *
  192. * Internally, maybe the date component should do some of this kind of stuff and cache it
  193. * so that it doesn't have to actually do anything when a method like this is called.
  194. */
  195. /*public function test_Is_Xth_Xday_Of_The_Month() {
  196. $date = new qCal_Date(2009, 12, 15);
  197. $this->assertTrue($date->isXthWeekdayOfMonth("Tuesday", 3));
  198. $this->assertTrue($date->isXthWeekdayOfMonth("Tuesday", -3));
  199. }*/
  200. }