PageRenderTime 58ms CodeModel.GetById 20ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

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

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