PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/olink/src/cake/tests/cases/libs/view/helpers/time.test.php

http://myopensources.googlecode.com/
PHP | 640 lines | 389 code | 66 blank | 185 comment | 22 complexity | 043c3f72600bdbd8018f93f836a19cb4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * TimeHelperTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.view.helpers
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  28. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  29. }
  30. App::import('Helper', 'Time');
  31. /**
  32. * TimeHelperTest class
  33. *
  34. * @package cake
  35. * @subpackage cake.tests.cases.libs.view.helpers
  36. */
  37. class TimeHelperTest extends CakeTestCase {
  38. /**
  39. * setUp method
  40. *
  41. * @access public
  42. * @return void
  43. */
  44. function setUp() {
  45. $this->Time = new TimeHelper();
  46. }
  47. /**
  48. * tearDown method
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. function tearDown() {
  54. unset($this->Time);
  55. }
  56. /**
  57. * testToQuarter method
  58. *
  59. * @access public
  60. * @return void
  61. */
  62. function testToQuarter() {
  63. $result = $this->Time->toQuarter('2007-12-25');
  64. $this->assertEqual($result, 4);
  65. $result = $this->Time->toQuarter('2007-9-25');
  66. $this->assertEqual($result, 3);
  67. $result = $this->Time->toQuarter('2007-3-25');
  68. $this->assertEqual($result, 1);
  69. $result = $this->Time->toQuarter('2007-3-25', true);
  70. $this->assertEqual($result, array('2007-01-01', '2007-03-31'));
  71. $result = $this->Time->toQuarter('2007-5-25', true);
  72. $this->assertEqual($result, array('2007-04-01', '2007-06-30'));
  73. $result = $this->Time->toQuarter('2007-8-25', true);
  74. $this->assertEqual($result, array('2007-07-01', '2007-09-30'));
  75. $result = $this->Time->toQuarter('2007-12-25', true);
  76. $this->assertEqual($result, array('2007-10-01', '2007-12-31'));
  77. }
  78. /**
  79. * testTimeAgoInWords method
  80. *
  81. * @access public
  82. * @return void
  83. */
  84. function testTimeAgoInWords() {
  85. $result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 3 days'), array('end' => '8 years'), true);
  86. $this->assertEqual($result, '4 months, 2 weeks, 3 days');
  87. $result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 2 days'), array('end' => '8 years'), true);
  88. $this->assertEqual($result, '4 months, 2 weeks, 2 days');
  89. $result = $this->Time->timeAgoInWords(strtotime('4 months, 2 weeks, 1 day'), array('end' => '8 years'), true);
  90. $this->assertEqual($result, '4 months, 2 weeks, 1 day');
  91. $result = $this->Time->timeAgoInWords(strtotime('3 months, 2 weeks, 1 day'), array('end' => '8 years'), true);
  92. $this->assertEqual($result, '3 months, 2 weeks, 1 day');
  93. $result = $this->Time->timeAgoInWords(strtotime('3 months, 2 weeks'), array('end' => '8 years'), true);
  94. $this->assertEqual($result, '3 months, 2 weeks');
  95. $result = $this->Time->timeAgoInWords(strtotime('3 months, 1 week, 6 days'), array('end' => '8 years'), true);
  96. $this->assertEqual($result, '3 months, 1 week, 6 days');
  97. $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 weeks, 1 day'), array('end' => '8 years'), true);
  98. $this->assertEqual($result, '2 months, 2 weeks, 1 day');
  99. $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 weeks'), array('end' => '8 years'), true);
  100. $this->assertEqual($result, '2 months, 2 weeks');
  101. $result = $this->Time->timeAgoInWords(strtotime('2 months, 1 week, 6 days'), array('end' => '8 years'), true);
  102. $this->assertEqual($result, '2 months, 1 week, 6 days');
  103. $result = $this->Time->timeAgoInWords(strtotime('1 month, 1 week, 6 days'), array('end' => '8 years'), true);
  104. $this->assertEqual($result, '1 month, 1 week, 6 days');
  105. for($i = 0; $i < 200; $i ++) {
  106. $years = mt_rand(0, 3);
  107. $months = mt_rand(0, 11);
  108. $weeks = mt_rand(0, 3);
  109. $days = mt_rand(0, 6);
  110. $hours = 0;
  111. $minutes = 0;
  112. $seconds = 0;
  113. $relative_date = '';
  114. if ($years > 0) {
  115. // years and months and days
  116. $relative_date .= ($relative_date ? ', -' : '-') . $years . ' year' . ($years > 1 ? 's' : '');
  117. $relative_date .= $months > 0 ? ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '') : '';
  118. $relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  119. $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  120. } elseif (abs($months) > 0) {
  121. // months, weeks and days
  122. $relative_date .= ($relative_date ? ', -' : '-') . $months . ' month' . ($months > 1 ? 's' : '');
  123. $relative_date .= $weeks > 0 ? ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  124. $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  125. } elseif (abs($weeks) > 0) {
  126. // weeks and days
  127. $relative_date .= ($relative_date ? ', -' : '-') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
  128. $relative_date .= $days > 0 ? ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  129. } elseif (abs($days) > 0) {
  130. // days and hours
  131. $relative_date .= ($relative_date ? ', -' : '-') . $days . ' day' . ($days > 1 ? 's' : '');
  132. $relative_date .= $hours > 0 ? ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
  133. } elseif (abs($hours) > 0) {
  134. // hours and minutes
  135. $relative_date .= ($relative_date ? ', -' : '-') . $hours . ' hour' . ($hours > 1 ? 's' : '');
  136. $relative_date .= $minutes > 0 ? ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
  137. } elseif (abs($minutes) > 0) {
  138. // minutes only
  139. $relative_date .= ($relative_date ? ', -' : '-') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
  140. } else {
  141. // seconds only
  142. $relative_date .= ($relative_date ? ', -' : '-') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
  143. }
  144. if (date('j/n/y', strtotime($relative_date)) != '1/1/70') {
  145. $result = $this->Time->timeAgoInWords(strtotime($relative_date), array('end' => '8 years'), true);
  146. if ($relative_date == '0 seconds') {
  147. $relative_date = '0 seconds ago';
  148. }
  149. $relative_date = str_replace('-', '', $relative_date) . ' ago';
  150. $this->assertEqual($result, $relative_date);
  151. }
  152. }
  153. for ($i = 0; $i < 200; $i ++) {
  154. $years = mt_rand(0, 3);
  155. $months = mt_rand(0, 11);
  156. $weeks = mt_rand(0, 3);
  157. $days = mt_rand(0, 6);
  158. $hours = 0;
  159. $minutes = 0;
  160. $seconds = 0;
  161. $relative_date = '';
  162. if ($years > 0) {
  163. // years and months and days
  164. $relative_date .= ($relative_date ? ', ' : '') . $years . ' year' . ($years > 1 ? 's' : '');
  165. $relative_date .= $months > 0 ? ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '') : '';
  166. $relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  167. $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  168. } elseif (abs($months) > 0) {
  169. // months, weeks and days
  170. $relative_date .= ($relative_date ? ', ' : '') . $months . ' month' . ($months > 1 ? 's' : '');
  171. $relative_date .= $weeks > 0 ? ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '') : '';
  172. $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  173. } elseif (abs($weeks) > 0) {
  174. // weeks and days
  175. $relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
  176. $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
  177. } elseif (abs($days) > 0) {
  178. // days and hours
  179. $relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '');
  180. $relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
  181. } elseif (abs($hours) > 0) {
  182. // hours and minutes
  183. $relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '');
  184. $relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
  185. } elseif (abs($minutes) > 0) {
  186. // minutes only
  187. $relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
  188. } else {
  189. // seconds only
  190. $relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
  191. }
  192. if (date('j/n/y', strtotime($relative_date)) != '1/1/70') {
  193. // echo $relative_date."<br />";
  194. $result = $this->Time->timeAgoInWords(strtotime($relative_date), array('end' => '8 years'), true);
  195. if ($relative_date == '0 seconds') {
  196. $relative_date = '0 seconds ago';
  197. }
  198. $relative_date = str_replace('-', '', $relative_date) . '';
  199. $this->assertEqual($result, $relative_date);
  200. }
  201. }
  202. $result = $this->Time->timeAgoInWords(strtotime('-2 years, -5 months, -2 days'), array('end' => '3 years'), true);
  203. $this->assertEqual($result, '2 years, 5 months, 2 days ago');
  204. $result = $this->Time->timeAgoInWords('2007-9-25');
  205. $this->assertEqual($result, 'on 25/9/07');
  206. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
  207. $this->assertEqual($result, 'on 2007-09-25');
  208. $result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d', true);
  209. $this->assertEqual($result, 'on 2007-09-25');
  210. $result = $this->Time->timeAgoInWords(strtotime('-2 weeks, -2 days'), 'Y-m-d', false);
  211. $this->assertEqual($result, '2 weeks, 2 days ago');
  212. $result = $this->Time->timeAgoInWords(strtotime('2 weeks, 2 days'), 'Y-m-d', true);
  213. $this->assertPattern('/^2 weeks, [1|2] day(s)?$/', $result);
  214. $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '1 month'));
  215. $this->assertEqual($result, 'on ' . date('j/n/y', strtotime('2 months, 2 days')));
  216. $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '3 month'));
  217. $this->assertPattern('/2 months/', $result);
  218. $result = $this->Time->timeAgoInWords(strtotime('2 months, 12 days'), array('end' => '3 month'));
  219. $this->assertPattern('/2 months, 1 week/', $result);
  220. $result = $this->Time->timeAgoInWords(strtotime('3 months, 5 days'), array('end' => '4 month'));
  221. $this->assertEqual($result, '3 months, 5 days');
  222. $result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '3 month'));
  223. $this->assertEqual($result, '2 months, 2 days ago');
  224. $result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '3 month'));
  225. $this->assertEqual($result, '2 months, 2 days ago');
  226. $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '3 month'));
  227. $this->assertPattern('/2 months/', $result);
  228. $result = $this->Time->timeAgoInWords(strtotime('2 months, 2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
  229. $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('2 months, 2 days')));
  230. $result = $this->Time->timeAgoInWords(strtotime('-2 months, -2 days'), array('end' => '1 month', 'format' => 'Y-m-d'));
  231. $this->assertEqual($result, 'on ' . date('Y-m-d', strtotime('-2 months, -2 days')));
  232. $result = $this->Time->timeAgoInWords(strtotime('-13 months, -5 days'), array('end' => '2 years'));
  233. $this->assertEqual($result, '1 year, 1 month, 5 days ago');
  234. $fourHours = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => -4));
  235. $result = $this->Time->timeAgoInWords(strtotime('-5 days, -2 hours'), array('userOffset' => 4));
  236. $this->assertEqual($fourHours, $result);
  237. $result = $this->Time->timeAgoInWords(strtotime('-2 hours'));
  238. $expected = '2 hours ago';
  239. $this->assertEqual($expected, $result);
  240. $result = $this->Time->timeAgoInWords(strtotime('-12 minutes'));
  241. $expected = '12 minutes ago';
  242. $this->assertEqual($expected, $result);
  243. $result = $this->Time->timeAgoInWords(strtotime('-12 seconds'));
  244. $expected = '12 seconds ago';
  245. $this->assertEqual($expected, $result);
  246. $time = strtotime('-3 years -12 months');
  247. $result = $this->Time->timeAgoInWords($time);
  248. $expected = 'on ' . date('j/n/y', $time);
  249. $this->assertEqual($expected, $result);
  250. }
  251. /**
  252. * testRelative method
  253. *
  254. * @access public
  255. * @return void
  256. */
  257. function testRelative() {
  258. $result = $this->Time->relativeTime('-1 week');
  259. $this->assertEqual($result, '1 week ago');
  260. $result = $this->Time->relativeTime('+1 week');
  261. $this->assertEqual($result, '1 week');
  262. }
  263. /**
  264. * testNice method
  265. *
  266. * @access public
  267. * @return void
  268. */
  269. function testNice() {
  270. $time = time() + 2 * DAY;
  271. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  272. $time = time() - 2 * DAY;
  273. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  274. $time = time();
  275. $this->assertEqual(date('D, M jS Y, H:i', $time), $this->Time->nice($time));
  276. $time = 0;
  277. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  278. $time = null;
  279. $this->assertEqual(date('D, M jS Y, H:i', time()), $this->Time->nice($time));
  280. }
  281. /**
  282. * testNiceShort method
  283. *
  284. * @access public
  285. * @return void
  286. */
  287. function testNiceShort() {
  288. $time = time() + 2 * DAY;
  289. if (date('Y', $time) == date('Y')) {
  290. $this->assertEqual(date('M jS, H:i', $time), $this->Time->niceShort($time));
  291. } else {
  292. $this->assertEqual(date('M jSY, H:i', $time), $this->Time->niceShort($time));
  293. }
  294. $time = time();
  295. $this->assertEqual('Today, '.date('H:i', $time), $this->Time->niceShort($time));
  296. $time = time() - DAY;
  297. $this->assertEqual('Yesterday, '.date('H:i', $time), $this->Time->niceShort($time));
  298. }
  299. /**
  300. * testDaysAsSql method
  301. *
  302. * @access public
  303. * @return void
  304. */
  305. function testDaysAsSql() {
  306. $begin = time();
  307. $end = time() + DAY;
  308. $field = 'my_field';
  309. $expected = '(my_field >= \''.date('Y-m-d', $begin).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $end).' 23:59:59\')';
  310. $this->assertEqual($expected, $this->Time->daysAsSql($begin, $end, $field));
  311. }
  312. /**
  313. * testDayAsSql method
  314. *
  315. * @access public
  316. * @return void
  317. */
  318. function testDayAsSql() {
  319. $time = time();
  320. $field = 'my_field';
  321. $expected = '(my_field >= \''.date('Y-m-d', $time).' 00:00:00\') AND (my_field <= \''.date('Y-m-d', $time).' 23:59:59\')';
  322. $this->assertEqual($expected, $this->Time->dayAsSql($time, $field));
  323. }
  324. /**
  325. * testToUnix method
  326. *
  327. * @access public
  328. * @return void
  329. */
  330. function testToUnix() {
  331. $this->assertEqual(time(), $this->Time->toUnix(time()));
  332. $this->assertEqual(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
  333. $this->assertEqual(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
  334. $this->assertEqual(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
  335. $this->assertEqual(false, $this->Time->toUnix(''));
  336. $this->assertEqual(false, $this->Time->toUnix(null));
  337. }
  338. /**
  339. * testToAtom method
  340. *
  341. * @access public
  342. * @return void
  343. */
  344. function testToAtom() {
  345. $this->assertEqual(date('Y-m-d\TH:i:s\Z'), $this->Time->toAtom(time()));
  346. }
  347. /**
  348. * testToRss method
  349. *
  350. * @access public
  351. * @return void
  352. */
  353. function testToRss() {
  354. $this->assertEqual(date('r'), $this->Time->toRss(time()));
  355. }
  356. /**
  357. * testFormat method
  358. *
  359. * @access public
  360. * @return void
  361. */
  362. function testFormat() {
  363. $format = 'D-M-Y';
  364. $arr = array(time(), strtotime('+1 days'), strtotime('+1 days'), strtotime('+0 days'));
  365. foreach ($arr as $val) {
  366. $this->assertEqual(date($format, $val), $this->Time->format($format, $val));
  367. }
  368. $result = $this->Time->format('Y-m-d', null, 'never');
  369. $this->assertEqual($result, 'never');
  370. }
  371. /**
  372. * testOfGmt method
  373. *
  374. * @access public
  375. * @return void
  376. */
  377. function testGmt() {
  378. $hour = 3;
  379. $min = 4;
  380. $sec = 2;
  381. $month = 5;
  382. $day = 14;
  383. $year = 2007;
  384. $time = mktime($hour, $min, $sec, $month, $day, $year);
  385. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  386. $this->assertEqual($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
  387. $hour = date('H');
  388. $min = date('i');
  389. $sec = date('s');
  390. $month = date('m');
  391. $day = date('d');
  392. $year = date('Y');
  393. $expected = gmmktime($hour, $min, $sec, $month, $day, $year);
  394. $this->assertEqual($expected, $this->Time->gmt(null));
  395. }
  396. /**
  397. * testIsToday method
  398. *
  399. * @access public
  400. * @return void
  401. */
  402. function testIsToday() {
  403. $result = $this->Time->isToday('+1 day');
  404. $this->assertFalse($result);
  405. $result = $this->Time->isToday('+1 days');
  406. $this->assertFalse($result);
  407. $result = $this->Time->isToday('+0 day');
  408. $this->assertTrue($result);
  409. $result = $this->Time->isToday('-1 day');
  410. $this->assertFalse($result);
  411. }
  412. /**
  413. * testIsThisWeek method
  414. *
  415. * @access public
  416. * @return void
  417. */
  418. function testIsThisWeek() {
  419. // A map of days which goes from -1 day of week to +1 day of week
  420. $map = array(
  421. 'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
  422. 'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
  423. 'Sun' => array(-7, 1)
  424. );
  425. $days = $map[date('D')];
  426. for ($day = $days[0] + 1; $day < $days[1]; $day++) {
  427. $this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
  428. }
  429. $this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
  430. $this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
  431. }
  432. /**
  433. * testIsThisMonth method
  434. *
  435. * @access public
  436. * @return void
  437. */
  438. function testIsThisMonth() {
  439. $result = $this->Time->isThisMonth('+0 day');
  440. $this->assertTrue($result);
  441. $result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
  442. $this->assertTrue($result);
  443. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
  444. $this->assertFalse($result);
  445. $result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
  446. $this->assertFalse($result);
  447. }
  448. /**
  449. * testIsThisYear method
  450. *
  451. * @access public
  452. * @return void
  453. */
  454. function testIsThisYear() {
  455. $result = $this->Time->isThisYear('+0 day');
  456. $this->assertTrue($result);
  457. $result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
  458. $this->assertTrue($result);
  459. }
  460. /**
  461. * testWasYesterday method
  462. *
  463. * @access public
  464. * @return void
  465. */
  466. function testWasYesterday() {
  467. $result = $this->Time->wasYesterday('+1 day');
  468. $this->assertFalse($result);
  469. $result = $this->Time->wasYesterday('+1 days');
  470. $this->assertFalse($result);
  471. $result = $this->Time->wasYesterday('+0 day');
  472. $this->assertFalse($result);
  473. $result = $this->Time->wasYesterday('-1 day');
  474. $this->assertTrue($result);
  475. $result = $this->Time->wasYesterday('-1 days');
  476. $this->assertTrue($result);
  477. $result = $this->Time->wasYesterday('-2 days');
  478. $this->assertFalse($result);
  479. }
  480. /**
  481. * testIsTomorrow method
  482. *
  483. * @access public
  484. * @return void
  485. */
  486. function testIsTomorrow() {
  487. $result = $this->Time->isTomorrow('+1 day');
  488. $this->assertTrue($result);
  489. $result = $this->Time->isTomorrow('+1 days');
  490. $this->assertTrue($result);
  491. $result = $this->Time->isTomorrow('+0 day');
  492. $this->assertFalse($result);
  493. $result = $this->Time->isTomorrow('-1 day');
  494. $this->assertFalse($result);
  495. }
  496. /**
  497. * testWasWithinLast method
  498. *
  499. * @access public
  500. * @return void
  501. */
  502. function testWasWithinLast() {
  503. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  504. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
  505. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  506. $this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
  507. $this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
  508. $this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
  509. $this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
  510. $this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
  511. $this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
  512. $this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
  513. $this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
  514. $this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
  515. $this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
  516. $this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
  517. $this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
  518. $this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
  519. $this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
  520. $this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
  521. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  522. $this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
  523. $this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
  524. $this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
  525. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
  526. $this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
  527. $this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
  528. $this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
  529. $this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
  530. $this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
  531. $this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
  532. $this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
  533. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 hour'));
  534. $this->assertTrue($this->Time->wasWithinLast('1 ', '-1 minute'));
  535. $this->assertTrue($this->Time->wasWithinLast('1 ', '-23 hours -59 minutes -59 seconds'));
  536. }
  537. /**
  538. * testUserOffset method
  539. *
  540. * @access public
  541. * @return void
  542. */
  543. function testUserOffset() {
  544. if ($this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) {
  545. return;
  546. }
  547. $timezoneServer = new DateTimeZone(date_default_timezone_get());
  548. $timeServer = new DateTime('now', $timezoneServer);
  549. $yourTimezone = $timezoneServer->getOffset($timeServer) / HOUR;
  550. $expected = time();
  551. $result = $this->Time->fromString(time(), $yourTimezone);
  552. $this->assertEqual($result, $expected);
  553. }
  554. /**
  555. * test fromString()
  556. *
  557. * @access public
  558. * @return void
  559. */
  560. function testFromString() {
  561. $result = $this->Time->fromString('');
  562. $this->assertFalse($result);
  563. $result = $this->Time->fromString(0, 0);
  564. $this->assertFalse($result);
  565. $result = $this->Time->fromString('+1 hour');
  566. $expected = strtotime('+1 hour');
  567. $this->assertEqual($result, $expected);
  568. $timezone = date('Z', time());
  569. $result = $this->Time->fromString('+1 hour', $timezone);
  570. $expected = $this->Time->convert(strtotime('+1 hour'), $timezone);
  571. $this->assertEqual($result, $expected);
  572. }
  573. }
  574. ?>