DatetimeHelperTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. * @return void
  76. * 2009-03-11 ms
  77. */
  78. public function testCweek() {
  79. $year = 2008;
  80. $month = 12;
  81. $day = 29;
  82. $date = mktime(0,0,0, $month, $day, $year);
  83. $this->assertEquals('01/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
  84. $year = 2009;
  85. $month = 1;
  86. $day = 1;
  87. $date = mktime(0,0,0, $month, $day, $year);
  88. $this->assertEquals('01/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
  89. $year = 2009;
  90. $month = 1;
  91. $day = 9;
  92. $date = mktime(0,0,0, $month, $day, $year);
  93. $this->assertEquals('02/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day.' 00:00:00'));
  94. $year = 2009;
  95. $month = 12;
  96. $day = 26;
  97. $date = mktime(0,0,0, $month, $day, $year);
  98. $this->assertEquals('52/'.$year, $this->Datetime->cweek($year.'-'.$month.'-'.$day));
  99. }
  100. /**
  101. * test age
  102. *
  103. * @return void
  104. * 2009-03-11 ms
  105. */
  106. public function testAge() {
  107. list($year, $month, $day) = explode('-',date('Y-m-d'));
  108. $this->assertEquals('0', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  109. list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years')));
  110. $this->assertEquals('10', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  111. list($year, $month, $day) = explode('-',date('Y-m-d',strtotime('-10 years +1 day')));
  112. $this->assertEquals('9', $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('10', $this->Datetime->age($year.'-'.$month.'-'.$day, null));
  115. # jahresübertritt
  116. list($year, $month, $day) = explode('-','2005-12-01');
  117. list($yearE, $monthE, $dayE) = explode('-','2008-02-29');
  118. $this->assertEquals('2', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  119. list($year, $month, $day) = explode('-','2002-01-29');
  120. list($yearE, $monthE, $dayE) = explode('-','2008-12-02');
  121. $this->assertEquals('6', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  122. # schaltjahr
  123. list($year, $month, $day) = explode('-','2005-02-29');
  124. list($yearE, $monthE, $dayE) = explode('-','2008-03-01');
  125. $this->assertEquals('3', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  126. list($year, $month, $day) = explode('-','2005-03-01');
  127. list($yearE, $monthE, $dayE) = explode('-','2008-02-29');
  128. $this->assertEquals('2', $this->Datetime->age($year.'-'.$month.'-'.$day, $yearE.'-'.$monthE.'-'.$dayE));
  129. #zukunft
  130. list($yearE, $monthE, $dayE) = explode('-',date('Y-m-d',strtotime('+10 years -1 day')));
  131. $this->assertEquals('9', $this->Datetime->age(null, $yearE.'-'.$monthE.'-'.$dayE));
  132. list($yearE, $monthE, $dayE) = explode('-',date('Y-m-d',strtotime('+10 years +1 day')));
  133. $this->assertEquals('10', $this->Datetime->age(null, $yearE.'-'.$monthE.'-'.$dayE));
  134. $birthday = '1985-04-08';
  135. $relativeDate = '2010-04-07';
  136. $this->assertEquals('24', $this->Datetime->age($birthday, $relativeDate));
  137. $relativeDate = '2010-04-08';
  138. $this->assertEquals('25', $this->Datetime->age($birthday, $relativeDate));
  139. $relativeDate = '2010-04-09';
  140. $this->assertEquals('25', $this->Datetime->age($birthday, $relativeDate));
  141. }
  142. /**
  143. * test IsInTheFuture
  144. *
  145. * @return void
  146. * 2010-02-18 ms
  147. */
  148. public function testIsInTheFuture() {
  149. $testDate = date(FORMAT_DB_DATE, time()+2*DAY);
  150. $is = $this->Datetime->isInTheFuture($testDate);
  151. $this->assertTrue($is);
  152. $testDate = date(FORMAT_DB_DATETIME, time()-1*MINUTE);
  153. $is = $this->Datetime->isInTheFuture($testDate);
  154. $this->assertFalse($is);
  155. }
  156. /**
  157. * test IsNotTodayAndInTheFuture
  158. *
  159. * @return void
  160. * 2010-02-18 ms
  161. */
  162. public function testIsNotTodayAndInTheFuture() {
  163. $testDate = date(FORMAT_DB_DATE, time());
  164. $is = $this->Datetime->isNotTodayAndInTheFuture($testDate);
  165. $this->assertFalse($is);
  166. $testDate = date(FORMAT_DB_DATETIME, time()+1*DAY);
  167. $is = $this->Datetime->isNotTodayAndInTheFuture($testDate);
  168. $this->assertTrue($is);
  169. }
  170. /**
  171. * test IsDayAfterTomorrow
  172. *
  173. * @return void
  174. * 2010-02-18 ms
  175. */
  176. public function testIsDayAfterTomorrow() {
  177. $testDate = date(FORMAT_DB_DATE, time()+2*DAY);
  178. $is = $this->Datetime->isDayAfterTomorrow($testDate);
  179. $this->assertTrue($is);
  180. $testDate = date(FORMAT_DB_DATETIME, time()-1*MINUTE);
  181. $is = $this->Datetime->isDayAfterTomorrow($testDate);
  182. $this->assertFalse($is);
  183. }
  184. /**
  185. * tearDown method
  186. *
  187. * @return void
  188. */
  189. public function tearDown() {
  190. parent::tearDown();
  191. unset($this->Datetime);
  192. }
  193. }