Browse Source

Copying some of the old tests for TimeHelper

Jose Lorenzo Rodriguez 12 years ago
parent
commit
7fc3522b9c
2 changed files with 366 additions and 84 deletions
  1. 28 2
      src/View/Helper/TimeHelper.php
  2. 338 82
      tests/TestCase/View/Helper/TimeHelperTest.php

+ 28 - 2
src/View/Helper/TimeHelper.php

@@ -68,6 +68,30 @@ class TimeHelper extends Helper {
 	}
 
 /**
+ * Returns true if given datetime string is in the future.
+ *
+ * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
+ * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
+ * @return bool True if datetime string is today
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
+ */
+	public function isFuture($dateString, $timezone = null) {
+		return (new Time($dateString, $timezone))->isFuture();
+	}
+
+/**
+ * Returns true if given datetime string is in the past.
+ *
+ * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
+ * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
+ * @return bool True if datetime string is today
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
+ */
+	public function isPast($dateString, $timezone = null) {
+		return (new Time($dateString, $timezone))->isPast();
+	}
+
+/**
  * Returns true if given datetime string is within this week.
  *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
@@ -167,7 +191,8 @@ class TimeHelper extends Helper {
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
 	public function toAtom($dateString, $timezone = null) {
-		return (new Time($dateString, $timezone))->toATOMString();
+		$timezone = $timezone ?: date_default_timezone_get();
+		return (new Time($dateString))->timezone($timezone)->toATOMString();
 	}
 
 /**
@@ -179,7 +204,8 @@ class TimeHelper extends Helper {
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
 	public function toRSS($dateString, $timezone = null) {
-		return (new Time($dateString, $timezone))->toRSSString();
+		$timezone = $timezone ?: date_default_timezone_get();
+		return (new Time($dateString))->timezone($timezone)->toRSSString();
 	}
 
 /**

+ 338 - 82
tests/TestCase/View/Helper/TimeHelperTest.php

@@ -24,29 +24,6 @@ use Cake\View\Helper\TimeHelper;
 use Cake\View\View;
 
 /**
- * TimeHelperTestObject class
- *
- */
-class TimeHelperTestObject extends TimeHelper {
-
-	public function attach(TimeMock $cakeTime) {
-		$this->_engine = $cakeTime;
-	}
-
-	public function engine() {
-		return $this->_engine;
-	}
-
-}
-
-/**
- * TimeMock class
- *
- */
-class TimeMock {
-}
-
-/**
  * TimeHelperTest class
  *
  */
@@ -54,8 +31,6 @@ class TimeHelperTest extends TestCase {
 
 	public $Time = null;
 
-	public $CakeTime = null;
-
 /**
  * setUp method
  *
@@ -64,63 +39,7 @@ class TimeHelperTest extends TestCase {
 	public function setUp() {
 		parent::setUp();
 		$this->View = new View();
-
-		$this->_appNamespace = Configure::read('App.namespace');
-		Configure::write('App.namespace', 'TestApp');
-	}
-
-/**
- * tearDown method
- *
- * @return void
- */
-	public function tearDown() {
-		unset($this->View);
-		Configure::write('App.namespace', $this->_appNamespace);
-		parent::tearDown();
-	}
-
-/**
- * test Cake\Utility\Time class methods are called correctly
- *
- * @return void
- */
-	public function testTimeHelperProxyMethodCalls() {
-		$methods = array(
-			'convertSpecifiers', 'convert', 'serverOffset', 'fromString',
-			'nice', 'niceShort', 'daysAsSql', 'dayAsSql',
-			'isToday', 'isThisMonth', 'isThisYear', 'wasYesterday',
-			'isTomorrow', 'toQuarter', 'toUnix', 'toAtom', 'toRSS',
-			'wasWithinLast', 'gmt', 'format', 'i18nFormat',
-		);
-		$CakeTime = $this->getMock(__NAMESPACE__ . '\TimeMock', $methods);
-		$Time = new TimeHelperTestObject($this->View, array('engine' => __NAMESPACE__ . '\TimeMock'));
-		$Time->attach($CakeTime);
-		foreach ($methods as $method) {
-			$CakeTime->expects($this->at(0))->method($method);
-			$Time->{$method}('who', 'what', 'when', 'where', 'how');
-		}
-
-		$CakeTime = $this->getMock(__NAMESPACE__ . '\TimeMock', array('timeAgoInWords'));
-		$Time = new TimeHelperTestObject($this->View, array('engine' => __NAMESPACE__ . '\TimeMock'));
-		$Time->attach($CakeTime);
-		$CakeTime->expects($this->at(0))->method('timeAgoInWords');
-		$Time->timeAgoInWords('who', array('what'), array('when'), array('where'), array('how'));
-	}
-
-/**
- * test engine override
- *
- * @return void
- */
-	public function testEngineOverride() {
-		$Time = new TimeHelperTestObject($this->View, array('engine' => 'TestAppEngine'));
-		$this->assertInstanceOf('TestApp\Utility\TestAppEngine', $Time->engine());
-
-		Plugin::load('TestPlugin');
-		$Time = new TimeHelperTestObject($this->View, array('engine' => 'TestPlugin.TestPluginEngine'));
-		$this->assertInstanceOf('TestPlugin\Utility\TestPluginEngine', $Time->engine());
-		Plugin::unload('TestPlugin');
+		$this->Time = new TimeHelper($this->View);
 	}
 
 /**
@@ -179,4 +98,341 @@ class TimeHelperTest extends TestCase {
 		$this->assertTags($result, $expected);
 	}
 
+/**
+ * testToQuarter method
+ *
+ * @return void
+ */
+	public function testToQuarter() {
+		$this->assertEquals(4, $this->Time->toQuarter('2007-12-25'));
+		$this->assertEquals(['2007-10-01', '2007-12-31'], $this->Time->toQuarter('2007-12-25', true));
+	}
+
+/**
+ * testNice method
+ *
+ * @return void
+ */
+	public function testNice() {
+		$time = '2014-04-20 20:00';
+		$this->assertEquals('Apr 20, 2014, 8:00 PM', $this->Time->nice($time));
+
+		$result = $this->Time->nice($time, 'America/New_York');
+		$this->assertEquals('Apr 20, 2014, 4:00 PM', $result);
+	}
+
+/**
+ * testToUnix method
+ *
+ * @return void
+ */
+	public function testToUnix() {
+		$this->assertEquals(time(), $this->Time->toUnix(time()));
+		$this->assertEquals(strtotime('+1 day'), $this->Time->toUnix('+1 day'));
+		$this->assertEquals(strtotime('+0 days'), $this->Time->toUnix('+0 days'));
+		$this->assertEquals(strtotime('-1 days'), $this->Time->toUnix('-1 days'));
+	}
+
+/**
+ * testToAtom method
+ *
+ * @return void
+ */
+	public function testToAtom() {
+		$dateTime = new \DateTime;
+		$this->assertEquals($dateTime->format($dateTime::ATOM), $this->Time->toAtom($dateTime->getTimestamp()));
+	}
+
+/**
+ * testToRss method
+ *
+ * @return void
+ */
+	public function testToRss() {
+		$date = '2012-08-12 12:12:45';
+		$time = strtotime($date);
+		$this->assertEquals(date('r', $time), $this->Time->toRss($time));
+
+		$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
+		foreach ($timezones as $timezone) {
+			$yourTimezone = new \DateTimeZone($timezone);
+			$yourTime = new \DateTime($date, $yourTimezone);
+			$time = $yourTime->format('U');
+			$this->assertEquals($yourTime->format('r'), $this->Time->toRss($time, $timezone), "Failed on $timezone");
+		}
+	}
+
+/**
+ * testOfGmt method
+ *
+ * @return void
+ */
+	public function testGmt() {
+		$hour = 3;
+		$min = 4;
+		$sec = 2;
+		$month = 5;
+		$day = 14;
+		$year = 2007;
+		$time = mktime($hour, $min, $sec, $month, $day, $year);
+		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
+		$this->assertEquals($expected, $this->Time->gmt(date('Y-n-j G:i:s', $time)));
+
+		$hour = date('H');
+		$min = date('i');
+		$sec = date('s');
+		$month = date('m');
+		$day = date('d');
+		$year = date('Y');
+		$expected = gmmktime($hour, $min, $sec, $month, $day, $year);
+		$this->assertEquals($expected, $this->Time->gmt(null));
+	}
+
+/**
+ * testIsToday method
+ *
+ * @return void
+ */
+	public function testIsToday() {
+		$result = $this->Time->isToday('+1 day');
+		$this->assertFalse($result);
+		$result = $this->Time->isToday('+1 days');
+		$this->assertFalse($result);
+		$result = $this->Time->isToday('+0 day');
+		$this->assertTrue($result);
+		$result = $this->Time->isToday('-1 day');
+		$this->assertFalse($result);
+	}
+/**
+ * testIsFuture method
+ *
+ * @return void
+ */
+	public function testIsFuture() {
+		$this->assertTrue($this->Time->isFuture('+1 month'));
+		$this->assertTrue($this->Time->isFuture('+1 days'));
+		$this->assertTrue($this->Time->isFuture('+1 minute'));
+		$this->assertTrue($this->Time->isFuture('+1 second'));
+
+		$this->assertFalse($this->Time->isFuture('-1 second'));
+		$this->assertFalse($this->Time->isFuture('-1 day'));
+		$this->assertFalse($this->Time->isFuture('-1 week'));
+		$this->assertFalse($this->Time->isFuture('-1 month'));
+	}
+
+/**
+ * testIsPast method
+ *
+ * @return void
+ */
+	public function testIsPast() {
+		$this->assertFalse($this->Time->isPast('+1 month'));
+		$this->assertFalse($this->Time->isPast('+1 days'));
+		$this->assertFalse($this->Time->isPast('+1 minute'));
+		$this->assertFalse($this->Time->isPast('+1 second'));
+
+		$this->assertTrue($this->Time->isPast('-1 second'));
+		$this->assertTrue($this->Time->isPast('-1 day'));
+		$this->assertTrue($this->Time->isPast('-1 week'));
+		$this->assertTrue($this->Time->isPast('-1 month'));
+	}
+
+/**
+ * testIsThisWeek method
+ *
+ * @return void
+ */
+	public function testIsThisWeek() {
+		// A map of days which goes from -1 day of week to +1 day of week
+		$map = array(
+			'Mon' => array(-1, 7), 'Tue' => array(-2, 6), 'Wed' => array(-3, 5),
+			'Thu' => array(-4, 4), 'Fri' => array(-5, 3), 'Sat' => array(-6, 2),
+			'Sun' => array(-7, 1)
+		);
+		$days = $map[date('D')];
+
+		for ($day = $days[0] + 1; $day < $days[1]; $day++) {
+			$this->assertTrue($this->Time->isThisWeek(($day > 0 ? '+' : '') . $day . ' days'));
+		}
+		$this->assertFalse($this->Time->isThisWeek($days[0] . ' days'));
+		$this->assertFalse($this->Time->isThisWeek('+' . $days[1] . ' days'));
+	}
+
+/**
+ * testIsThisMonth method
+ *
+ * @return void
+ */
+	public function testIsThisMonth() {
+		$result = $this->Time->isThisMonth('+0 day');
+		$this->assertTrue($result);
+		$result = $this->Time->isThisMonth($time = mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y')));
+		$this->assertTrue($result);
+		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') - mt_rand(1, 12)));
+		$this->assertFalse($result);
+		$result = $this->Time->isThisMonth(mktime(0, 0, 0, date('m'), mt_rand(1, 28), date('Y') + mt_rand(1, 12)));
+		$this->assertFalse($result);
+	}
+
+/**
+ * testIsThisYear method
+ *
+ * @return void
+ */
+	public function testIsThisYear() {
+		$result = $this->Time->isThisYear('+0 day');
+		$this->assertTrue($result);
+		$result = $this->Time->isThisYear(mktime(0, 0, 0, mt_rand(1, 12), mt_rand(1, 28), date('Y')));
+		$this->assertTrue($result);
+	}
+
+/**
+ * testWasYesterday method
+ *
+ * @return void
+ */
+	public function testWasYesterday() {
+		$result = $this->Time->wasYesterday('+1 day');
+		$this->assertFalse($result);
+		$result = $this->Time->wasYesterday('+1 days');
+		$this->assertFalse($result);
+		$result = $this->Time->wasYesterday('+0 day');
+		$this->assertFalse($result);
+		$result = $this->Time->wasYesterday('-1 day');
+		$this->assertTrue($result);
+		$result = $this->Time->wasYesterday('-1 days');
+		$this->assertTrue($result);
+		$result = $this->Time->wasYesterday('-2 days');
+		$this->assertFalse($result);
+	}
+
+/**
+ * testIsTomorrow method
+ *
+ * @return void
+ */
+	public function testIsTomorrow() {
+		$result = $this->Time->isTomorrow('+1 day');
+		$this->assertTrue($result);
+		$result = $this->Time->isTomorrow('+1 days');
+		$this->assertTrue($result);
+		$result = $this->Time->isTomorrow('+0 day');
+		$this->assertFalse($result);
+		$result = $this->Time->isTomorrow('-1 day');
+		$this->assertFalse($result);
+	}
+
+/**
+ * testWasWithinLast method
+ *
+ * @return void
+ */
+	public function testWasWithinLast() {
+		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
+		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 week'));
+		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
+		$this->assertTrue($this->Time->wasWithinLast('1 second', '-1 second'));
+		$this->assertTrue($this->Time->wasWithinLast('1 minute', '-1 minute'));
+		$this->assertTrue($this->Time->wasWithinLast('1 year', '-1 year'));
+		$this->assertTrue($this->Time->wasWithinLast('1 month', '-1 month'));
+		$this->assertTrue($this->Time->wasWithinLast('1 day', '-1 day'));
+
+		$this->assertTrue($this->Time->wasWithinLast('1 week', '-1 day'));
+		$this->assertTrue($this->Time->wasWithinLast('2 week', '-1 week'));
+		$this->assertFalse($this->Time->wasWithinLast('1 second', '-1 year'));
+		$this->assertTrue($this->Time->wasWithinLast('10 minutes', '-1 second'));
+		$this->assertTrue($this->Time->wasWithinLast('23 minutes', '-1 minute'));
+		$this->assertFalse($this->Time->wasWithinLast('0 year', '-1 year'));
+		$this->assertTrue($this->Time->wasWithinLast('13 month', '-1 month'));
+		$this->assertTrue($this->Time->wasWithinLast('2 days', '-1 day'));
+
+		$this->assertFalse($this->Time->wasWithinLast('1 week', '-2 weeks'));
+		$this->assertFalse($this->Time->wasWithinLast('1 second', '-2 seconds'));
+		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
+		$this->assertFalse($this->Time->wasWithinLast('1 hour', '-2 hours'));
+		$this->assertFalse($this->Time->wasWithinLast('1 month', '-2 months'));
+		$this->assertFalse($this->Time->wasWithinLast('1 year', '-2 years'));
+
+		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 weeks'));
+		$this->assertFalse($this->Time->wasWithinLast('1 day', '-2 days'));
+		$this->assertFalse($this->Time->wasWithinLast('0 days', '-2 days'));
+		$this->assertTrue($this->Time->wasWithinLast('1 hour', '-20 seconds'));
+		$this->assertTrue($this->Time->wasWithinLast('1 year', '-60 minutes -30 seconds'));
+		$this->assertTrue($this->Time->wasWithinLast('3 years', '-2 months'));
+		$this->assertTrue($this->Time->wasWithinLast('5 months', '-4 months'));
+
+		$this->assertTrue($this->Time->wasWithinLast('5 ', '-3 days'));
+		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 hour'));
+		$this->assertTrue($this->Time->wasWithinLast('1   ', '-1 minute'));
+		$this->assertTrue($this->Time->wasWithinLast('1   ', '-23 hours -59 minutes -59 seconds'));
+	}
+
+/**
+ * testWasWithinLast method
+ *
+ * @return void
+ */
+	public function testIsWithinNext() {
+		$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
+		$this->assertFalse($this->Time->isWithinNext('1 week', '-1 week'));
+		$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
+		$this->assertFalse($this->Time->isWithinNext('1 second', '-1 second'));
+		$this->assertFalse($this->Time->isWithinNext('1 minute', '-1 minute'));
+		$this->assertFalse($this->Time->isWithinNext('1 year', '-1 year'));
+		$this->assertFalse($this->Time->isWithinNext('1 month', '-1 month'));
+		$this->assertFalse($this->Time->isWithinNext('1 day', '-1 day'));
+
+		$this->assertFalse($this->Time->isWithinNext('1 week', '-1 day'));
+		$this->assertFalse($this->Time->isWithinNext('2 week', '-1 week'));
+		$this->assertFalse($this->Time->isWithinNext('1 second', '-1 year'));
+		$this->assertFalse($this->Time->isWithinNext('10 minutes', '-1 second'));
+		$this->assertFalse($this->Time->isWithinNext('23 minutes', '-1 minute'));
+		$this->assertFalse($this->Time->isWithinNext('0 year', '-1 year'));
+		$this->assertFalse($this->Time->isWithinNext('13 month', '-1 month'));
+		$this->assertFalse($this->Time->isWithinNext('2 days', '-1 day'));
+
+		$this->assertFalse($this->Time->isWithinNext('1 week', '-2 weeks'));
+		$this->assertFalse($this->Time->isWithinNext('1 second', '-2 seconds'));
+		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
+		$this->assertFalse($this->Time->isWithinNext('1 hour', '-2 hours'));
+		$this->assertFalse($this->Time->isWithinNext('1 month', '-2 months'));
+		$this->assertFalse($this->Time->isWithinNext('1 year', '-2 years'));
+
+		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 weeks'));
+		$this->assertFalse($this->Time->isWithinNext('1 day', '-2 days'));
+		$this->assertFalse($this->Time->isWithinNext('0 days', '-2 days'));
+		$this->assertFalse($this->Time->isWithinNext('1 hour', '-20 seconds'));
+		$this->assertFalse($this->Time->isWithinNext('1 year', '-60 minutes -30 seconds'));
+		$this->assertFalse($this->Time->isWithinNext('3 years', '-2 months'));
+		$this->assertFalse($this->Time->isWithinNext('5 months', '-4 months'));
+
+		$this->assertFalse($this->Time->isWithinNext('5 ', '-3 days'));
+		$this->assertFalse($this->Time->isWithinNext('1   ', '-1 hour'));
+		$this->assertFalse($this->Time->isWithinNext('1   ', '-1 minute'));
+		$this->assertFalse($this->Time->isWithinNext('1   ', '-23 hours -59 minutes -59 seconds'));
+
+		$this->assertTrue($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 59 seconds'));
+		$this->assertFalse($this->Time->isWithinNext('7 days', '6 days, 23 hours, 59 minutes, 61 seconds'));
+	}
+
+/**
+ * test formatting dates taking in account preferred i18n locale file
+ *
+ * @return void
+ */
+	public function testFormat() {
+		$time = strtotime('Thu Jan 14 13:59:28 2010');
+
+		$result = $this->Time->format($time);
+		$expected = '1/14/10, 1:59 PM';
+		$this->assertEquals($expected, $result);
+
+		$result = $this->Time->format($time, \IntlDateFormatter::FULL);
+		$expected = 'Thursday, January 14, 2010 at 1:59:28 PM GMT';
+
+		$result = $this->Time->format('invalid date', null, 'Date invalid');
+		$expected = 'Date invalid';
+		$this->assertEquals($expected, $result);
+	}
+
 }