DatetimeHelperTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. App::uses('DatetimeHelper', 'Tools.View/Helper');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. App::uses('View', 'View');
  5. /**
  6. * Datetime Test Case
  7. *
  8. */
  9. class DatetimeHelperTest extends MyCakeTestCase {
  10. public function setUp() {
  11. parent::setUp();
  12. $this->Datetime = new DatetimeHelper(new View(null));
  13. }
  14. /**
  15. * test user age
  16. *
  17. * 2012-04-10 ms
  18. */
  19. public function testUserAge() {
  20. $res = $this->Datetime->userAge('2010-01-01');
  21. $this->assertTrue($res >= 2);
  22. }
  23. /**
  24. * test cweek
  25. *
  26. * 2009-03-11 ms
  27. */
  28. public function testLengthOfTime() {
  29. $this->assertEquals('6 '.__('Minutes').', 40 '.__('Seconds'), $this->Datetime->lengthOfTime(400));
  30. $res = $this->Datetime->lengthOfTime(400, 'i');
  31. //pr($res);
  32. $this->assertEquals('6 '.__('Minutes'), $res);
  33. $res = $this->Datetime->lengthOfTime(6*DAY);
  34. //pr($res);
  35. $this->assertEquals('6 '.__('Days').', 0 '.__('Hours'), $res);
  36. #TODO: more
  37. }
  38. public function testRelLengthOfTime() {
  39. //echo $this->_header(__FUNCTION__);
  40. $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()-3600));
  41. //pr($res);
  42. $this->assertTrue(!empty($res));
  43. $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()-4*DAY-5*HOUR), null, array('plural'=>'n'));
  44. //pr($res);
  45. //$this->assertEquals($res, 'Vor 4 Tagen, 5 '.__('Hours'));
  46. $this->assertEquals(__('%s ago', '4 '.__('Days').', '.'5 '.__('Hours')), $res);
  47. $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()+4*DAY+5*HOUR), null, array('plural'=>'n'));
  48. //pr($res);
  49. $this->assertEquals(__('In %s', '4 '.__('Days').', '.'5 '.__('Hours')), $res);
  50. $res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time()), null, array('plural'=>'n'));
  51. //pr($res);
  52. $this->assertEquals($res, __('justNow'));
  53. }
  54. // Cake internal function...
  55. public function testTimeAgoInWords() {
  56. //echo $this->_header(__FUNCTION__);
  57. $res = $this->Datetime->timeAgoInWords(date(FORMAT_DB_DATETIME, time()-4*DAY-5*HOUR));
  58. //pr($res);
  59. }
  60. public function testIsInRange() {
  61. //echo $this->_header(__FUNCTION__);
  62. $day = date(FORMAT_DB_DATETIME, time()+10*DAY);
  63. $this->assertTrue($this->Datetime->isInRange($day, 11*DAY));
  64. $this->assertTrue($this->Datetime->isInRange($day, 10*DAY));
  65. $this->assertFalse($this->Datetime->isInRange($day, 9*DAY));
  66. $day = date(FORMAT_DB_DATETIME, time()-78*DAY);
  67. $this->assertTrue($this->Datetime->isInRange($day, 79*DAY));
  68. $this->assertTrue($this->Datetime->isInRange($day, 78*DAY));
  69. $this->assertFalse($this->Datetime->isInRange($day, 77*DAY));
  70. #TODO: more
  71. }
  72. /**
  73. * test cweek
  74. *
  75. * @access public
  76. * @return void
  77. * 2009-03-11 ms
  78. */
  79. public function testCweek() {
  80. $year = 2008;
  81. $month = 12;
  82. $day = 29;
  83. $date = mktime(0,0,0, $month, $day, $year);
  84. $this->assertEquals('01/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
  85. $year = 2009;
  86. $month = 1;
  87. $day = 1;
  88. $date = mktime(0,0,0, $month, $day, $year);
  89. $this->assertEquals('01/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
  90. $year = 2009;
  91. $month = 1;
  92. $day = 9;
  93. $date = mktime(0,0,0, $month, $day, $year);
  94. $this->assertEquals('02/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day.' 00:00:00'));
  95. $year = 2009;
  96. $month = 12;
  97. $day = 26;
  98. $date = mktime(0,0,0, $month, $day, $year);
  99. $this->assertEquals('52/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
  100. }
  101. /**
  102. * test age
  103. *
  104. * @access public
  105. * @return void
  106. * 2009-03-11 ms
  107. */
  108. public function testAge() {
  109. list($year, $month, $day) = explode('-',date('Y-m-d'));
  110. $this->assertEquals('0', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  111. list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years')));
  112. $this->assertEquals('10', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  113. list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years +1 day')));
  114. $this->assertEquals('9', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  115. list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years -1 day')));
  116. $this->assertEquals('10', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  117. # jahresübertritt
  118. list($year, $month, $day) = explode('-','2005-12-01');
  119. list($yearE, $monthE, $dayE) = explode('-','2008-02-29');
  120. $this->assertEquals('2', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  121. list($year, $month, $day) = explode('-','2002-01-29');
  122. list($yearE, $monthE, $dayE) = explode('-','2008-12-02');
  123. $this->assertEquals('6', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  124. # schaltjahr
  125. list($year, $month, $day) = explode('-','2005-02-29');
  126. list($yearE, $monthE, $dayE) = explode('-','2008-03-01');
  127. $this->assertEquals('3', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  128. list($year, $month, $day) = explode('-','2005-03-01');
  129. list($yearE, $monthE, $dayE) = explode('-','2008-02-29');
  130. $this->assertEquals('2', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  131. #zukunft
  132. list($yearE, $monthE, $dayE) = explode('-',date('Y-m-d',strtotime('+10 years -1 day')));
  133. $this->assertEquals('9', $this->Datetime->age(null, $yearE.'-'.$monthE.'-'.$dayE));
  134. list($yearE, $monthE, $dayE) = explode('-',date('Y-m-d',strtotime('+10 years +1 day')));
  135. $this->assertEquals('10', $this->Datetime->age(null, $yearE.'-'.$monthE.'-'.$dayE));
  136. $birthday = '1985-04-08';
  137. $relativeDate = '2010-04-07';
  138. $this->assertEquals('24', $this->Datetime->age($birthday, $relativeDate));
  139. $relativeDate = '2010-04-08';
  140. $this->assertEquals('25', $this->Datetime->age($birthday, $relativeDate));
  141. $relativeDate = '2010-04-09';
  142. $this->assertEquals('25', $this->Datetime->age($birthday, $relativeDate));
  143. }
  144. /**
  145. * test IsInTheFuture
  146. *
  147. * @access public
  148. * @return void
  149. * 2010-02-18 ms
  150. */
  151. public function testIsInTheFuture() {
  152. $testDate = date(FORMAT_DB_DATE, time()+2*DAY);
  153. $is = $this->Datetime->isInTheFuture($testDate);
  154. $this->assertTrue($is);
  155. $testDate = date(FORMAT_DB_DATETIME, time()-1*MINUTE);
  156. $is = $this->Datetime->isInTheFuture($testDate);
  157. $this->assertFalse($is);
  158. }
  159. /**
  160. * test IsNotTodayAndInTheFuture
  161. *
  162. * @access public
  163. * @return void
  164. * 2010-02-18 ms
  165. */
  166. public function testIsNotTodayAndInTheFuture() {
  167. $testDate = date(FORMAT_DB_DATE, time());
  168. $is = $this->Datetime->isNotTodayAndInTheFuture($testDate);
  169. $this->assertFalse($is);
  170. $testDate = date(FORMAT_DB_DATETIME, time()+1*DAY);
  171. $is = $this->Datetime->isNotTodayAndInTheFuture($testDate);
  172. $this->assertTrue($is);
  173. }
  174. /**
  175. * test IsDayAfterTomorrow
  176. *
  177. * @access public
  178. * @return void
  179. * 2010-02-18 ms
  180. */
  181. public function testIsDayAfterTomorrow() {
  182. $testDate = date(FORMAT_DB_DATE, time()+2*DAY);
  183. $is = $this->Datetime->isDayAfterTomorrow($testDate);
  184. $this->assertTrue($is);
  185. $testDate = date(FORMAT_DB_DATETIME, time()-1*MINUTE);
  186. $is = $this->Datetime->isDayAfterTomorrow($testDate);
  187. $this->assertFalse($is);
  188. }
  189. /**
  190. * tearDown method
  191. *
  192. * @access public
  193. * @return void
  194. */
  195. public function tearDown() {
  196. parent::tearDown();
  197. unset($this->Datetime);
  198. }
  199. }